String vs string in c#

This post will explain what is the difference between string and String in C#. How do you create a string variable in c# ?

Do you use String or string class in .NET ?

Confused with the above text . Well its just that we use either the keyword string or we use String.

Is both String and string same in C# ?

string is just an alias name for the class System.String which means both has the same functionalities .The IL code generated for both of them is also same .

Note that string is a keyword , but String isn’t which lets you create an identifier with the name String like

String String ="Senthil kumar" ;

    4 Comments

  1. MichaelM
    April 15, 2011
    Reply

    It’s possible in next way:

    string @string = “string”;

  2. Ryukk
    April 15, 2011
    Reply

    It’s called CTS (common type specification)

    int, bool, string … is just reserved keywords, they are simply aliases for the predefined structure type in System namespace (Int32, Boolean, String, etc)

  3. Preeti Chauhan Kohli
    May 4, 2011
    Reply

    If you are new to C#, you are probably wondering what the difference is between the string and String types. In the .NET framework, string is simply an alias—shorthand—for the Common Type System (CTS) System.String class which represents a sequence of characters. (string is one of two predefined C# reference types: The other is object.) You can use them interchangeably in your code.

    String x = string.Copy (“x”);
    string y = String.Copy (“y”);
    C# Best Practices —
    Use “String” to refer specifically to the String class.
    Use “string” when referring to an object of the String class.

  4. May 4, 2011
    Reply

    Thanks preeti for the info

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...