Reputation: 9530
In the "C# Coding Standard" by Juval Lowy available from www.idesign.net, the recomendation is made to use the C# predefined types instead of the aliases in the System
namespace, e.g.:
object
NOT Object
string
NOT String
int
NOT Int32
What is the benefit of this? How do they differ? I have followed this advise in my own coding but never knew how they differed.
Upvotes: 17
Views: 4449
Reputation: 12465
The Entity Framework code generator uses predefined types, so if you want to be able to implement the Visual Studio 2017 coding style rules fully you will need to choose predefined types (int instead of Int32, etc). Otherwise your generated code will not be in compliance.
(Options->Text Editor->C#->Code Style->General->predefined type preference)
Upvotes: 0
Reputation: 50306
I always use the aliases when specifying the type in a parameter, property or method signature or field (so: almost everywhere) except when calling a static member on such a type.
String.Format("{0}", 1);
Int32.Parse("123");
String.IsNullOrEmpty(value);
Upvotes: 3
Reputation: 71565
Here's another compiler-based difference:
public enum MyEnum : Byte {Value1, Value2} //does not compile
public enum MyEnum : byte {Value1, Value2} //ok
Upvotes: 1
Reputation: 59443
In addition to what Jon said here is another difference.
var x = (Int32)-y; // Does not compile.
var x = (int)-y; // Negates the value of y and casts as an int.
This is because of a grammar disambiguation rule defined in §7.6.6 of the C# Programming Language specification.
Upvotes: 8
Reputation: 1062600
The main time they are unexpectedly different is when someone is stupid enough to call a type (or property /field/etc) String
(for example), since string
always refers to global::System.String
, where-as String
could be YourNamespace.String
.
The closest you can get to the C# alias is @string
, which tends to stick out like a sore thumb.
I prefer the C# aliases.
btw, here's a fun way to mess with anyone using dynamic
too much:
using dynamic = System.Object;
Upvotes: 19
Reputation: 3652
I think using the 'blue' int
, string
, etc.. might be a little more intuitive to read. Otherwise, I use the class when calling a static method on it i.e. Int32.TryParse()
Upvotes: 7
Reputation: 65126
The only difference is that they're nicer to read (this of course is a matter of opinion). The compiled result is exactly the same bytecode.
Upvotes: 0
Reputation: 1500065
They don't really differ. Personally I use the aliases too, but Jeff Richter advocates the exact opposite. The generated code will be exactly the same. Use whichever you find most readable (and try to be consistent).
One thing most people agree on: when writing an API, use the type name rather than the alias, so:
int ReadInt32()
rather than
int ReadInt()
the int
part doesn't matter here - it's not part of the name, and can be displayed appropriately to any consumer using any language... but the method name should be language-neutral, which means using the type name.
One place where you have to use the alias is when specifying the underlying type for an enum:
enum Foo : byte // Valid
enum Foo : System.Byte // Invalid
Upvotes: 13