views:

219

answers:

7

As far as I can tell, any static member of a class like String or Int32 can also be accessed from the related primitive data type. So, String.Format is the same as string.Format, and Int32.MaxValue is the same as int.MaxValue.

Is there a difference between these two forms? Is one preferred to the other? Even if they are identical, is one generally considered more readable?

Edit: Since they are identical, is one preferred from a human perspective? Would you rather see String.Format or string.Format when reading someone else's code?

+4  A: 

They're identical. string and int are syntactic aliases for String and Int32 respectively.

munificent
+18  A: 

There's no difference, these are type aliases in C# for .Net framework types, you're calling the same method underneath.

For example:

  • int is an alias for System.Int32
  • string is an alias for System.String

You can find a complete list of these aliases on MSDN here.

Nick Craver
Cool, I like this. Thanks for the link!
Matthew
@Matthew - welcome :)
Nick Craver
When I was learning C# I was a little confused as to why 'string' and 'String' both existed. It helped me to grok things when I realized that if I removed the line 'using System;' that 'String' became an invalid type.
Detmar
+2  A: 

string is not a primative type but simply an alias for String. They are the same thing.

aqwert
+10  A: 

Those are not related primitive data types. They are simply shorthands available in C#. string aliases System.String and int aliases System.Int32. Calls to int.MaxValue are calls to Int32.MaxValue. C# just allows you to type it in shorthand similar to what you would type if you were in another C-like language.

Anthony Pegram
+3  A: 

Notice that if you go into your IDE and hover your mouse over string, you will see that it is defined by System.String

This is an "alias"... one thing is shorthand for another thing. int is an alias for Int32, byte is an alias for Byte, char is an alias for Char, and so on and so forth.

ItzWarty
+5  A: 

Most of the answers have it in general. HOWEVER, there are some cases in which the alias is required. Off the top of my head:

public enum MyEnum:Byte {...} //will not compile

public enum MyEnum:byte {...} //correct

There are a couple other places where you must use the alias; besides that, it's mostly style. The following two rules are generally accepted, perhaps the first more than the second:

  • Use the alias (lowercase keyword) for all usages of that define a variable or member type (declaration, parameters, casting, generic type closure)
  • Use the type name (CamelCased class identifier) for all usages of the type in static context (Calling static methods such as parsers or string manipulation methods, or static properties like MinValue/MaxValue).
KeithS
+1  A: 

A funny proof for their interchangeability is the IDE itself.
Start typing List<String> list = new then look at IntelliSense's suggestion: List<string>. It won't even give you List<String> as one of the choices.

To answer the second part of your question, which is an opinion matter, I prefer int and string over upper-case for many reasons:

  • The bright blue color is more definitive and if you're fast-reading code you cannot mistake the class name for something else
  • The lesser I need to hit SHIFT+key, the faster I type
  • IntelliSense will auto-complete the lower-case names, even if you start with uppercase, so to me it's aesthetically unpleasing to see upper- and lower-case versions of the same class in the same line, or even in the same project
  • The type int is very simple in nature, and I would hate to force my eyes to associate that with having an upper-case letter and two digits in its name (i.e. Int32); it makes it look too specific for no obvious reason

A colleague of mine said specifically using Int32 and String ensures compatibility with future .NET updates; I disagree with whoever says that completely. Prove me wrong, please!

Beemer