This question already has answers here:
What is the difference between String and string in C#?
(66 answers)
Closed 9 years ago.
What is the difference between string.Empty and String.Empty in C#? Also note the colors are different. They both reference or are of type class System.String
It is the same. string is an alias of class System.String.
some common aliases:
object ==> System.Object
string ==> System.String
bool ==> System.Boolean
int ==> System.Int32
float ==> System.Single
double ==> System.Double
decimal ==> System.Decimal
char ==> System.Char
String.Empty and string.Empty are same. String is the BCL class name. string is the C#...shortcut if you will. Same as with Int32 and int.
Both are SAME except that string is treated as a keyword with Blue color (default) and System.String is a Class with Green Color (default) in the VS editor.
Underlying implementation are all the same. In other words points to the same.
In C#, the string keyword is an alias for String. Therefore, String and string are equivalent, and you can use whichever naming convention you prefer. The String class provides many methods for safely creating, manipulating, and comparing strings. In addition, the C# language overloads some operators to simplify common string operations
Related
This question already has answers here:
What's the # in front of a string in C#?
(9 answers)
Closed 5 years ago.
Sometimes I find that many people use #string for locate a directory than use string even I though that they are same.
for example :
I have variable that called direct
string direct = "C:\\Users";
And then,I type :
System.IO.Directory.CreateDirectory(#direct);
I think it's same with
System.IO.Directory.CreateDirectory(direct);
But,what's difference between #direct and direct?
The # prefix allows you to use a reserved keyword as a variable name.
The following is an error:
string string = "C:\\Junk";
but the following is allowed (although a very bad idea):
string #string = "C:\\Junk";
The # prefix can also be used as a verbatim string literal:
string thefolderpath = #"C:\Junk";
If I had to guess, I would say the original programmer decided to use #string, #int etc. for temporary variables, as a style choice. A style choice, it must be said, akin to wearing socks with sandals.
This question already has answers here:
Use the long reserved word as a variable name in C#
(5 answers)
Closed 6 years ago.
So I have this simple problem that I'm struggling with. Consider this code:
namespace Foo
{
public class Bar
{
public void Test(string object)
{
}
}
}
This function throws a syntax error because object is a keyword in C#. Is there a way to solve that? In my real code I have a framework that uses function's signature to create an API and I should really use object name as parameter.
Use # before parameter name #object to use keyword as identifier
public void Test(string #object)
From C# Language Specification 2.4.2 Identifiers:
The rules for identifiers given in this section correspond exactly to those recommended by the Unicode Standard Annex
31, except that underscore is allowed as an initial character (as is
traditional in the C programming language), Unicode escape sequences
are permitted in identifiers, and the “#” character is allowed as a
prefix to enable keywords to be used as identifiers.
The # symbol allows you to use reserved word. For example:
int #class = 15;
The above works, when the below wouldn't:
int class = 15;
Here are a list of aliases in C# (compliments of What is the difference between String and string in C#?):
object: System.Object
string: System.String
bool: System.Boolean
byte: System.Byte
sbyte: System.SByte
short: System.Int16
ushort: System.UInt16
int: System.Int32
uint: System.UInt32
long: System.Int64
ulong: System.UInt64
float: System.Single
double: System.Double
decimal: System.Decimal
char: System.Char
I can see bool through char being lowercase aliases, because they are primitive types.
Why are object and string not capitalized, since they are complex types? Is this an oversight by the developers, or is there a necessary reason for them to be lowercase? Or is this an opinionated question?
You end up with things like string.Format() instead of String.Format(), which just look funky and make me think string is a variable.
Because all keywords (reserved identifiers) are lowercase.
In C#, there are no "primitive types" and "complex types". There are classes and structs, (reference types and value types, respectively) among others. Both can include methods (e.g. char.IsDigit('a')). So your objections aren't really valid. But there is still the question: why?
I'm not sure if there's a good source for this, but I think the lowercase aliases are done to match the other C# keywords, which are themselves modeled on C/C++ keywords.
Regarding your last comment:
You end up with things like string.Format() instead of String.Format(), which just look funky and make me think string is a variable.
With C# 6, this becomes a moot point as you can do:
using static System.String;
...
var x = Format(...);
Or going further, you can do away with string.Format altogether and use $ instead.
Answer is simple. If you want to use it like a class use String, if you want to use it like a keyword use string. Developers wanted to make us feel like we are using a primitive type. Because in C# nothing is primitive.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the difference between String and string
When I run:
char c1 = 'a';
Console.WriteLine(c1);
and when I run:
Char c2 = 'a';
Console.WriteLine(c2);
I get exactly the same result, a.
I wanted to know what is the difference between the two forms, and why are there two forms?
The result is exactly the same. Both represent the same type, so the resulting executables are completely identical.
The char keyword is an alias in the C# language for the type System.Char in the framework.
You can always use the char keyword. To use Char you need a using System; at the top of the file to include the System namespace (or use System.Char to specify the namespace).
In most situations you can use either a keyword or the framework type, but not everywhere. For example as backing type in an enum, you can only use the keyword:
enum Test : int { } // works
enum Test : Int32 {} // doesn't work
(I use int in the example, as You can't use a char as backing type for an enum.)
Related: Difference between byte vs Byte data types in C#
As far as I know, C# char type keyword is simply an alias for System.Char, so they refer to the same type.
The keyword char is an alias of the System.Char type in C#.
This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
In C# what is the difference between String and string
String vs string in C#
Hi,
Can anyone tell me the difference between the two lines of code below:
public const String sample = "Sample";
public const string sample2 = "Sample2";
Both "String" and "string" are of System.String.
Thanks in advance.
These data types are exactly the same, as string is just an alias for the class String. If you have a look, there are similar capitalized and non capitalized versions of int, float and similar classes.
Have a look here for a more detailed answer.
There are the same
string is an alias in the C# for .Net System.String, like int is for Int32, long is for int64 and etc. C# string replaced by System.String during compilation
No difference what so ever - in fact you can write the following code:
String sample = "Sample";
string sample2 = sample;
Both maps to the same IL string type
String vs string in C#
String stands for System.String and it is a .NET Framework type. string is an alias in the C# language for System.String. Both of them are compiled to System.String in IL (Intermediate Language), so there is no difference. Choose what you like and use that. If you code in C#, I'd prefer string as it's a C# type alias and well-know by C# programmers.
String is CTS type but string is c# string object.
You can use String to any of dot net language.
both are the same
string -> c# type which gets converted to
String -> .net type
String is the .NET class for the CLR built-in string type. string is the C# language identifier that maps to the CLR String type. They are the same thing.
"string" is actually an alias for System.String. They're the same.
Try:
typeof(string) == typeof(String) == typeof(System.String)
Nothing really, in C# the type keywords actually are synonyms for the types. So int = System.Int32 short = System.Int16 and string = System.String.
They have the keywords because they are easier to remember and programmers coming from other languages like c/c++ would also be familiar with these types.
Anyway, look at the C# keyword reference and you can find these things out. This was taken from the string keyword reference.
The string type represents a string of Unicode characters. string is an alias for String in the .NET Framework. Strings are immutable--the contents of a string object cannot be changed after the object is created.