In C#, string is a sequence of Unicode characters or array of characters. The range of Unicode characters will be U+0000 to U+FFFF. The array of characters is also termed as the text. So the string is the representation of the text. A string is an important concept and people gets confused whether the string is a keyword or an object or a class. So let’s clear out this concept.
A string is represented by class System.String. The “string” keyword is an alias for System.String class and instead of writting System.String one can use String which is a shorthand for System.String class. So we can say string and String both can be used as an alias of System.String class. So string is an object of System.String class.
Example:
string s1 = “GeeksforGeeks”; // creating the string using string keyword
String s2 = “GFG”; // creating the string using String class
System.String s3 = “Pro Geek”; // creating the string using String class
The String class is defined in the .NET base class library. In other words a String object is a sequential collection of System.Char objects which represents a string. The maximum size of String object in memory is 2GB or about 1 billion characters. System.String class is immutable, i.e once created its state cannot be altered.
Program: To illustrate how to declare the string and initialize the string. Also, below program show the declaration and intialization of a string in a single line.
// C# program to declare string using // string, String and System.String // and intialzation of string using System; class Geeks { // Main Method static void Main( string [] args) { // declare a string Name using // "System.String" class System.String Name; // intialzation of String Name = "Geek" ; // declare a string id using // using an alias(shorthand) // "String" of System.String // class String id; // intialzation of String id = "33" ; // declare a string mrk using // string keyword string mrk; // intialzation of String mrk = "97" ; // Declaration and intialzation of // the string in a single line string rank = "1" ; // Displaying Result Console.WriteLine( "Name: {0}" , Name); Console.WriteLine( "Id: {0}" , id); Console.WriteLine( "Marks: {0}" , mrk); Console.WriteLine( "Rank: {0}" , rank); } } |
Name: Geek Id: 33 Marks: 97 Rank: 1
String Characteristics:
- It is a reference type.
- It’s immutable( its state cannot be altered).
- It can contain nulls.
- It overloads the operator(==).
Differences between String and System.String :
The string is an alias for System.String. Both String and System.String means same and it will not affect the performance of the application. “string” is keyword in C#. So the main difference comes in the context, how to use these:
- The String is used for the declaration but System.String is used for accessing static string methods.
- The String is used to declare fields, properties etc. that it will use the predefined type System.String. It is the easy way to use.
- The String has to use the System.String class methods, such as String.SubString, String.IndexOf etc. The string is only an alias of System.String.
Note: In .NET, the text is stored as a sequential collection of the Char objects so there is no null-terminating character at the end of a C# string. Therefore a C# string can contain any number of embedded null characters (‘