Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

csharp

What is the difference between string and System.String in C#?

Tarun Telang

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

In this shot, we’ll learn the difference between string and System.String in C#.

A string represents a sequence of characters. There are many other ways to represent strings, but we’ll focus on System.String in this shot.

Below are the key differences between string and System.String in C#:

string keyword

  • string is a keyword in C#.

  • string keyword is an alias for the String class. It doesn’t require usage of any using statements.

  • string keyword is used to create a String object. Syntax of string:

string str1 = "Hello World!";
  • C# has a few overloaded operators for performing operations on strings. For example, the [] operator is used to access characters of a string, and the + operator is used to perform concatenation of strings.

System.String class

  • String is a class in C#.

  • String class is part of System namespace. To use it, we need to provide using System statement.

  • String class is also used to create a String object. Syntax of String:

String str2 = "Hello World!";
  • String class contains many useful methods for instantiating, processing, and comparing strings, such as Equals, Contains, StartsWith, EndsWith, IndexOf more.

String class and string keyword are equivalent and refer to the same thing.

Example

The following code snippet will further clarify the differences between string and String.

using System; // for using String class

class HelloWorld
{
    static void Main()
    {
        // not initialized, has value null
        string str1; 
        // initialized to a literal
        str1 = "Hello World!"; 
            
        String str2 = "Hello World!";         
        // copy of existing string
        String str3 = String.Copy(str2); 
        string str4 = str1; 

        System.Console.WriteLine(str1 == str2); // displays 'True'
        System.Console.WriteLine(string.Equals(str3, str4)); // dispalys 'True'
    }
}
Usage of string keyword and String class in C#

Explanation

  • Line 8: We initialize a string type variable called str1. It’s value is null by default.

  • Line 10: We assign the value, "Hello World!", to the str1 string.

  • Line 12: We use the System.String class to define a string instance.

We can use either of the below syntaxes to declare string variables:

string str1 = "Hello World!";

String str2 = "Hello World!"; 

Both variables refer to an instance of the System.String class with string value "Hello World!".

  • Line 14: We create a copy of an instance by explicitly calling Copy static method of System.String on an existing string instance that has already been assigned a value.

  • Line 15: str3 is assigned to str4. Both the variables refer to an instance of the System.String class with string value "Hello World!".

The equality operator == returns True if both the strings are equal. If not, it returns False.

Equals(String, String) performs the equality check and returns true if two string instances have the same value. If not, it returns false.

RELATED TAGS

csharp

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring