What Great .NET Developers Ought To Know (More .NET Interview Questions and Answers) – Part1

In this series of posts, I will try to answer the questions posted by Scott Hanselman here. This part we will be seeing the first set of questions he has given under Everyone who writes code heading.

Q) Describe the difference between a Thread and a Process?

A Thread is a small unit of code in execution. A process is an instance of program which will have multiple threads running. A process can host multiple threads. Thread will always belong to a process. Communication between multiple processes are difficult but communication between threads is easy.

Continue reading

Advertisements

What is the difference between typeof(Foo) and myFoo.GetType()?

This question is taken from Scott Hanselman‘s What a .NET developer should know post. To understand the difference, let us write a simple program.

class Foo
{
    /* .... */
}

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("typeof(Foo) = " + typeof(Foo).Name);
        Foo f = new Foo();
        Console.WriteLine("f.GetType() = " + f.GetType().Name);
    }
}

Here is the output.

command-prompt

Let us add a subclass for Foo and try again.

class Foo
{
}

class SubFoo : Foo
{
}

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("typeof(Foo) = " + typeof(Foo).Name);
        Foo f = new SubFoo();
        Console.WriteLine("f.GetType() = " + f.GetType().Name);
    }
}

command-prompt1

In general, f.GetType() returns runtime type of the instance. typeof(Foo) gives the compile time type of Foo.