Wednesday 14 August 2013

.net interview question

Question 1:

public class Person
{
    public string Name { get; set; }
}
public class Program
{
    private static void Main(string[] args)
    {
        var person1 = new Person { Name = "Test" };
        Console.WriteLine(person1.Name);//Output:Test

        Person person2 = person1;
        person2.Name = "Shahrooz";
        Console.WriteLine(person1.Name);//Output:Shahrooz

        person2 = null;
        Console.WriteLine(person1.Name);//Output:???
    }
}
For answer this please check below link. Click here to see Answer


Question 2:

public class MyClass
{
    public void Func(Object a)
    {
        Console.WriteLine("Object");
    }

    public void Func(String a)
    {
        Console.WriteLine("String");
    }
}
public class Program
{
    private static void Main(string[] args)
    {
        MyClass mc = new MyClass();
        mc.Func(null);
    }
}
For answer this please check below link.
Click here to see Answer
Click here to see Answer

6 comments:

  1. what is d answer of second one ?

    ReplyDelete
    Replies
    1. Answer of question no 2 is : "String"

      Delete
    2. as we passed null, Compiler always called the nullable parameter type first. if you have "int?" in parameter then it called those method. If you want to call the "object" method then you have to write "null as object".

      Delete
    3. Also added link below question for more detail.

      Delete