Open In App

Difference between String and string in C#

Improve
Improve
Like Article
Like
Save
Share
Report

String is an alias for System.String class and instead of writing System.String one can use String which is a shorthand for System.String class and is defined in the .NET base class library. The size of the String object in memory is 2GB and this is an immutable object, we can not modify the characters in a string once declared, but we can delete it entirely

Syntax:

String variable = "my string";

Example:

C#




// C# program to illustrate String
using System;
  
class GFG{
  
public static void Main()
{
      
    // Declare String variable
    String b = "Welcome to GeeksforGeeks";
  
    // Display the result
    Console.WriteLine(a);
}
}


Output:

Geeks

A string is a sequence of Unicode characters from U+0000 to U+FFFF. Or we can say that a string represents the text. It is a keyword and it is immutable which means we can not modify the characters in a string once declared, but we can delete it entirely. We can create a string variable  using the following syntax: 

Syntax:

string variable = "my string";

Example:

C#




// C# program to illustrate string
using System;
  
class GFG{
      
public static void Main()
{
      
    // Declare string variable
    string a = "GeeksforGeeks";
  
    // Display the result
    Console.WriteLine(a);
}
}


Output:

GeeksforGeeks

Difference between String and string

String

string

It is a class used to access string variables and formatting methods. It is a keyword used to create a string variable
We have to import String from the System.String module. There is no need to import any module for string
It is a data type It is a keyword
It contains different types of methods, properties, etc. It is just an alias of the System.String


Last Updated : 23 Nov, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads