Search code examples
c#staticprimitive

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?


Solution

  • 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.