Tagged Questions
900
votes
31answers
146k views
What's the difference between String and string?
In C#, what is the difference between String and string? (note the case)
Example:
string s = "Hello, World";
String S = "Hello, World";
Also, what are the guidelines for the use of each?
264
votes
0answers
56k views
String vs string in C# [duplicate]
Possible Duplicate:
In C# what is the difference between String and string
In C# the string keyword (highlighted in Visual Studio as a data type) is just a shortcut to the String class ...
429
votes
22answers
257k views
.NET String to byte Array C#
How do I convert a string to a byte array in .NET (C#)?
Update: Also please explain why encoding should be taken into consideration. Can't I simply get what bytes the string has been stored in? Why ...
49
votes
8answers
5k views
What's the @ in front of a string in C#?
This is a .NET question for C# (or possibly VB.net), but I am trying to figure out what's the difference between the following declarations:
string hello = "hello";
vs.
string hello_alias = ...
259
votes
26answers
162k views
C# String enums
I have the following enumeration:
public enum AuthenticationMethod
{
FORMS = 1,
WINDOWSAUTHENTICATION = 2,
SINGLESIGNON = 3
}
The problem however is that I need the word "FORMS" when I ...
98
votes
8answers
43k views
In C#, why is String a reference type that behaves like a value type?
A String is a reference type even though it has most of the characteristics of a value type such as being immutable and having == overloaded to compare the text rather than making sure they reference ...
184
votes
19answers
110k views
How would you count occurences of a string within a string (C#)?
I am doing something where I realised I wanted to count how many /s I could find in a string, and then it struck me, that there were about several ways to do it, but couldn't decide on what the best ...
54
votes
13answers
18k views
Why .NET String is immutable? [duplicate]
As we all know, String is immutable. What are the reasons for String being immutable and the introduction of StringBuilder class as mutable?
71
votes
10answers
42k views
What's the best string concatenation method using C#?
What's the most efficient way to concatenate strings?
59
votes
29answers
19k views
C# String output: format or concat?
Let's say that you want to output or concat strings, what style do you prefer:
var p = new { FirstName = "Bill", LastName = "Gates" };
Console.WriteLine("{0} {1}", p.FirstName, ...
74
votes
8answers
9k views
Are string.Equals() and == operator really same?
Are they really same? Today, I ran into this problem. Here is the dump from the Immediate Window:
?s
"Category"
?tvi.Header
"Category"
?s == tvi.Header
false
?s.Equals(tvi.Header)
true
?s == ...
97
votes
12answers
65k views
How do I replace multiple spaces with a single space in C#?
How can I replace multiple spaces in a string with only one space in C#?
Example:
1 2 3 4 5
would be:
1 2 3 4 5
21
votes
19answers
27k views
c# evaluating string “3*(4+2)” yield int 18 [duplicate]
Is there a function the the .Net framework that can evaluate a numering expression contained in a string and return the numeric result? IE:
string mystring = "3*(2+4)";
int result = ...
230
votes
4answers
52k views
How to escape brackets (curly braces) in a format string in .Net
How can brackets be escaped in a C# format string so, something like:
String val = "1,2,3"
String.Format(" foo {{0}}", val);
doesn't throw a parse exception but actually outputs the string "foo ...
86
votes
9answers
64k views
Differences in string compare methods in C#
Comparing string in C# is pretty simple. In fact there are several ways to do it. I have listed some in the block below. What I am curious about are the differences between them and when one should ...