C# string (Text)

The string data type is used to store a sequence of characters (text). Strings in C# are immutable, meaning that their values cannot be changed once they are created. Strings are enclosed in double quotes, such as "Hello".

Key Concepts

  • A string represents a sequence of Unicode characters.
  • Strings are immutable in C#, meaning any modification creates a new string.
  • They are often used for representing text, including user input and messages.

Example of Using a string

Code Example

// Declare a string variable
string myString = "Hello, World!";

// Output the string to the console
Console.WriteLine(myString);

Output:

Hello, World!

Code Explanation: The string variable myString is initialized with the text "Hello, World!". It is printed using the Console.WriteLine() method.

Output Explanation: The text "Hello, World!" is displayed on the console, which is the value assigned to myString.