0
0
C Sharp (C#)programming~5 mins

String type and immutability in C Sharp (C#)

Choose your learning style9 modes available
Introduction

Strings hold text in programs. They cannot be changed once created, which helps avoid mistakes.

When you need to store names, messages, or any text.
When you want to make sure text does not change accidentally.
When you want to compare or display text safely.
When you want to build new text from old text without changing the original.
Syntax
C Sharp (C#)
string name = "Hello";

Strings are written inside double quotes.

Once a string is created, you cannot change its characters directly.

Examples
Adding strings creates a new string without changing the original.
C Sharp (C#)
string greeting = "Hi";
string newGreeting = greeting + " there!";
Methods like ToUpper() return a new string, original stays the same.
C Sharp (C#)
string original = "cat";
string upper = original.ToUpper();
Sample Program

This program shows that adding text creates a new string. The original string stays "Hello".

C Sharp (C#)
using System;

class Program
{
    static void Main()
    {
        string text = "Hello";
        string changedText = text + " World";

        Console.WriteLine(text);
        Console.WriteLine(changedText);
    }
}
OutputSuccess
Important Notes

Strings are immutable, so operations create new strings instead of changing old ones.

This helps keep your data safe and your program predictable.

Summary

Strings hold text and are written in double quotes.

Strings cannot be changed after creation (immutable).

Any change to a string creates a new string, leaving the original unchanged.