tags:

views:

160

answers:

5

Hi.

Is there any performance differences between string.Format and String.Format ? As far as i understand string is a shortcut for System.String and there's no difference between them. Am i right?

Thanks!

+11  A: 

Those are exactly the same. "string" is just an alias for "System.String" in C#

Philippe Leybaert
+3  A: 

string and String both resolve to System.String. So no there is no difference at all.

Mark
+1  A: 

Yes, string is an alias for System.String in C#
Test it yourself:

 public static void Main(string[] args)
 {
  string s1 = "abc";
  System.String s2 = "xyz";
  Type t1 = s1.GetType();
  Type t2 = s2.GetType();

  System.Console.WriteLine( t1.Equals(t2) );
  return ;
 }
VolkerK
+1  A: 

Yes, there's absolutely no difference from CLR's point of view: string vs String is purely C# syntactic sugar.

Anton Gogolev
but why is this syntactic sugar necessary/useful? I never understood this. I came from the MS world and for me (although it is clear) was always just a point of confusion among people and also an inconsistency in the programming style somehow. Some of the team using String, others string..
Juri
It's just that primitive types, like string, int or bool, usually have a corresponding language keyword. You wouldn't want to write Int32 every time you want an int...
Thomas Levesque
+1  A: 

In C#, string is simply an alias for the CLR type System.String, so there is no performance difference. It is not clear why MS decided to include this alias, particularly as the other aliased types (int, long, etc.) are value types rather than reference types.

In my team we decided that static methods of the string class should be referenced as e.g. String.ToUpper() rather than string.ToUpper(), in order to be more consistent with other CLR languages.

Ultimately which version you use is up to you but you should decide which you will use in what context in order to improve consistency.

Matt Howells