Studies show that good grammar leads to more promotions and higher salaries. Write with Grammarly

C# Error CS0165 – Use of unassigned local variable ‘name’

C# Compiler Error

CS0165 – Use of unassigned local variable ‘name’

Reason for the Error

You will receive this error when the C# compiler detects that you have used an uninitialized variables with-in your program.

For example, the below code snippet results in the error code CS0165 because the variable val is not initialized.

using System;

namespace ConsoleApp2
{
    class Program
    {
        public static void Main()
        {
            int val;
            Console.WriteLine(val);
                 
        }

    }
    
}
C# Error CS0165 – Use of unassigned local variable 'name'

Error CS0165 Use of unassigned local variable ‘val’ ConsoleApp2 C:\Users\Senthil\source\repos\ConsoleApp1\ConsoleApp2\Program.cs 10 Active

Solution

C# doesnot allow you to use uninitialized variables. You will need to initialize the variable to a default value before you want to use them in your C# code.