Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 874
Matthew Avatar asked Sep 10 '25 15:09

Matthew


2 Answers

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.

like image 158
Nick Craver Avatar answered Sep 12 '25 07:09

Nick Craver


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.

like image 41
Anthony Pegram Avatar answered Sep 12 '25 06:09

Anthony Pegram