Search code examples
c#.netvariableslocal-variablesunassigned-variable

Error 2 Use of unassigned local variable 'Y'


Again i run into an error i don't mean to bug anyone but I'm getting an error on this code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Input_Program
{
    class Program
    {
       private static void Main()
        {

           char Y;
            char N;

           Console.WriteLine("Welcome to my bool program!");
           Console.WriteLine("Input a NON capital y or n when told to.");




            if(Y == 'y')
            {
                Console.WriteLine("Thank you,Please wait.....");
            }
        }
    }
}

Thanks for you answers!


Solution

  • Your variable char Y is not initialized before using. Try to give a default value when declaring.

    EDIT It seems that you want the users to input something, and assign it to the variable Y. Try:

    Y = Console.ReadKey().KeyChar;