What is the difference between String.Format and string.Format (and other static members of primitive data types)?
Asked Answered
T

8

26

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?

Tergum answered 22/10, 2010 at 21:9 Comment(0)
W
48

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.

Waistcoat answered 22/10, 2010 at 21:10 Comment(2)
Cool, I like this. Thanks for the link!Tergum
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.Cloraclorinda
E
13

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.

Emilia answered 22/10, 2010 at 21:12 Comment(0)
I
8

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 (PascalCased 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).
Ignoramus answered 22/10, 2010 at 21:22 Comment(0)
M
5

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

Mistletoe answered 22/10, 2010 at 21:10 Comment(0)
E
3

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

Elastin answered 22/10, 2010 at 21:10 Comment(0)
F
3

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.

Forum answered 22/10, 2010 at 21:13 Comment(0)
I
3

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!

Isoclinal answered 22/10, 2010 at 21:46 Comment(0)
B
0

String.Format() requires "using Systems;" but string.Format() doesn't.

Buskin answered 13/2, 2020 at 14:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.