Functions – C# Programming Interview Questions – Real

Only Real C# programming interview questions with answers for freshers and experienced asked in Technical interviews.

Q – Does below code compile? If not, what is the issue?

class A
{
    public void Y(out int val)
    {
        Console.WriteLine("Value:"+val);        
        val = 10;
        Console.WriteLine("Value assigned:"+val);
    }
}
class Program
    {
        static void Main(string[] args)
        {
            A a = new A();          
            int y;
            a.Y(out y);
            Console.WriteLine("Value of y:" + y);

        }
    }

Answer: There is a compiler error, as in method Y(out int val),val is being read(at line: Console.WriteLine(“Value:”+val);) before assignment to “out” variable i.e. val =10. we can’t read value of out variable before assignment but later.


Related Posts