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.
Related
I like unity and I want to keep my code style guidelines strictly consistent.
So consider this ulong literal:
var x = 0xFFUL;
vs.
var x = 0xffUL;
It might be a stupid question but I hate when my code is not consistent even in these negligible things, so I'd like to know what's hex numbers like for the C# project team...
To date, the most popular format for representing hexadecimal literals is 0 to 9 and A to F.
You can confirm that the use of uppercase characters is most popular by referring to the hexadecimal Wikipedia entry here.
Hence follow the "wisdom of the crowd" and use 0-9, A-F.
I'm actually looking at Microsoft's code base and find inconsistencies there too.
Here it's lowercase while here it's uppercase.
So I believe the true answer is there is no convention about that, and one should pick his favorite naming.
Also searching here I'm not finding anything about hex casing.
I'm designing an application that requires a "location" field. The values for the location are "3241", "4112", "ND" and "TRAVEL", and I am trying to set up an enum that includes those values.
I started with
enum projectLocation {3241,4112,ND,TRAVEL};
but the values 3241 and 4112 indicated a syntax error--identifier expected--for the first value in the enum. If I understand enum correctly, that's because the above statement is looking for the value for the enum's integer indeces of 3241 and 4112. Is this a correct assumption?
I tried overriding that with the following
enum projectLocation {3241=0,4112,ND,TRAVEL};
and
enum projectLocation {3241=0,4112=1,ND=2,TRAVEL=3};
but I'm still getting the same syntax error on the 3241 value. Interestingly though, on both of these statements, there is NO syntax error on 4112, but I get can't find the namespace or name ND and ...TRAVEL
It makes sense that enum will not allow a mix of strings and integers, and I have two other enums that work fine and are only lists of string values, corroborating that theory. Is there a way to force enum to accept the numeric values as strings? I've not been able to find any references in MSDNs C# documentation.
Enums are called as named constants, so basically you give a name for some constant. Names are "identifiers" in c#, where identifier can contain numbers but first character cannot be a number.
You can add _ before that to fix this error.
enum projectLocation
{
_3241=0,
_4112=1,
ND=2,
TRAVEL=3
}
Also note the Fields, Properties Methods or whatever falls under this identifier rule mentioned above.
You can't do it exactly like you're trying to. Here's an alternative:
enum projectLocation {
L3241=3241,
L4112=4112,
ND=2,
TRAVEL=3
}
Starting them with a letter makes them valid enum identifiers, and setting their value equal to their number lets you do things like (projectLocation)3241 and get the expected L3241 value.
If you need the values to be 3241 and 4112 when serialized, include the proper attribute for your serialization approach, e.g. with Json.NET:
enum projectLocation {
[JsonProperty("3241")]
L3241=3241,
[JsonProperty("4112")]
L4112=4112,
ND=2,
TRAVEL=3
}
You would need to do something like this:
enum projectLocation { v3241, v4112, ND, TRAVEL };
or:
enum projectLocation { _3241, _4112, ND, TRAVEL };
My preference would be the use of the underscore.
C# does not allow the first character of member names to start with a number. Consider using the # character as a prefix, or some other character that conveys a meaning useful to a reader of the code as a number in isolation may not be very intuitive to a reader not familiar with the significance of 3241 in your context.
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
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.