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.

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...