Start learning free

LESSON 12 OF 347 · VARIABLES

Variable Reassignment in C#: Changing a Variable's Value

Lesson Read the full explanation

You can always change the value stored in a variable with the assignment operator(=):

// Initializing with 5:
int myVar = 5;

// Assigning the value 10:
myVar = 10;

Console.WriteLine(myVar); // Output: 10

First, this code creates a variable named myVar and initializes it with 5.

Then, the code assigns 10 to myVar. Now, when myVar appears again in the code, the program will treat it as if it is 10.


Assigning the Value of One Variable to Another:

You can also assign the value of some variable to another variable using the assignment operator:

int myVar = 5;
int myNum = 10;

myVar = myNum;

Console.WriteLine(myVar); // Output: 10

First, this code creates a variable named myVar and initializes it with 5 and another variable named myNum and initializes it with 10.

Then, the contents of myNum (which is 10) is assigned to the variable myVar. Now, myVar also has the value of 10.


Assignment
Follow the Coding Tutorial and let's reassign some variables.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    C# · VARIABLES

    Variable Reassignment in C#, explained

    Assigning a new value

    A variable is a named box, and its contents can change. After a variable is declared, you can store a new value in it with the assignment operator =:

    // Initializing with 5:
    int myVar = 5;
    
    // Assigning the value 10:
    myVar = 10;
    
    Console.WriteLine(myVar);   // 10
    

    The first line creates myVar and initializes it with 5. The assignment on the third line replaces that value. From then on, every use of myVar sees 10; the 5 is gone.

    Declaring vs assigning

    This is the most important detail in the lesson. The type is written only once, when the variable is declared. A later assignment has just the name, the = and the new value:

    int score = 1;    // declaration: type + name + value
    score = 2;        // assignment: name + value
    

    If you write the type again, C# thinks you are creating a second variable with the same name and refuses to compile:

    int score = 1;
    int score = 2;   // error CS0128: 'score' is already defined in this scope
    

    The type stays the same

    A variable keeps its declared type for as long as it exists. You can store a different value in it, but only a value of the same type:

    int age = 24;
    age = 34;        // fine, still an int
    age = "thirty";  // error: cannot convert type 'string' to 'int'
    

    Copying one variable into another

    The right-hand side of an assignment can be any expression, including another variable:

    int myVar = 5;
    int myNum = 10;
    
    myVar = myNum;
    
    Console.WriteLine(myVar);   // 10
    

    C# reads the current value of myNum and stores a copy in myVar. The two variables are then independent. If you later assign a new value to myNum, myVar does not change. The same holds for strings: after string s2 = s1; s1 = "bye";, the variable s2 still holds the old text.

    Using the old value in the new one

    The right-hand side is evaluated first, using the values the variables have at that moment. This lets a variable be updated based on itself:

    int years = 24;
    years = years + 10;   // 34
    years += 1;           // 35, shorthand for years = years + 1
    

    Reassignment is how programs keep track of things that change while they run: a score that goes up, a total that grows, a status that moves from one state to the next. Variables that never change can be marked as constants, which the next lesson covers.

    Order matters: swapping two values

    Because each assignment overwrites the old value, swapping two variables needs a third one to hold a value temporarily:

    int a = 1;
    int b = 2;
    int temp = a;   // remember a's value
    a = b;          // a is now 2
    b = temp;       // b gets the remembered 1
    Console.WriteLine(a + " " + b);   // 2 1
    

    Without temp, the line a = b; would destroy the only copy of the original a. Every time you reassign, ask whether the old value is still needed.

    The exercise, step by step

    Mircea starts as a Junior Developer aged 24 who codes in Java. The program prints that, and your job is to write the code that makes the "ten years later" lines print a different story, without changing the language. The editor marks the spot where your code goes.

    1. Reassign role. Write role = "Senior Developer"; with no string in front, because the variable already exists.
    2. Reassign age. Today's age is 24, so ten years later is 34: age = 34;.
    3. Leave language alone. The lesson says he never changed it, and the last line prints it as before.
    string role = "Junior Developer";
    int age = 24;
    string language = "Java";
    
    // Today
    Console.WriteLine("His age is " +  age);
    Console.WriteLine("He is a " + role);
    Console.WriteLine("He codes in " + language);
    
    // ---------------------------------------
    // Write your code here:
    
    role = "Senior Developer";
    age = 34;
    
    // ---------------------------------------
    
    // Ten years later:
    Console.WriteLine("Ten years later, his age is " +  age);
    Console.WriteLine("He is a " + role);
    Console.WriteLine("He still codes in " + language);
    

    The output shows both stories:

    His age is 24
    He is a Junior Developer
    He codes in Java
    Ten years later, his age is 34
    He is a Senior Developer
    He still codes in Java
    

    The first three lines run before your assignments, so they still print the old values. The program runs top to bottom, and a variable holds whatever was assigned most recently at that point.

    Common mistakes

    • Repeating the type. string role = "Senior Developer"; in the marked area is a second declaration of role and does not compile.
    • Assigning the wrong type. age = "34"; puts text into an int.
    • Putting the assignment in the wrong place. Placed after the last prints, it changes the variables when it is too late.
    • Using == instead of =. age == 34 asks whether the two are equal and changes nothing.

    Key takeaways

    • Declare a variable once, with its type; after that, assign with just name = value.
    • The new value must match the declared type.
    • Assigning one variable to another copies the value.
    • The right side is evaluated first, so x = x + 1 works.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free