.Net Interview Questions – Can you explain Generics?

In this post we are going to continue with entries dedicated to analyze the most common questions that come up in a .net interview. Today I will try to answer a question that appear in almost all interviews and is: Can you explain Generics?

Normally the first thought we have speaking about generics is about the generics collections and it is normal because most of the collections in .net use generics but there are not the concept itself.

Generics in a nutshell

Generics is an availability expose by the .net framework that allow us to split the behavior and the data type. Let see an example to show it clearly how it works. Suppose that we have a class that exposed a method to compare two strings variables and return if they are equals or not.

class check
{
    public static bool Compare(string a, string b)
    {
         return a.Equals(b);
    }
}

As you can see this is a really simple class but if we take a closer watch we will quickly realize a problem. That method only allow us to compare strings. If we want to compare another data type we should create an overload of this method as many times as data type we want to compare. We can see that this code is not clean and the maintenance is really complex.

class check
{
    public static bool Compare(string a, string b)
    {
        return a.Equals(b);
    }
    public static bool Compare(int a, int b)
    {
        return a.Equals(b);
    }

    public static bool Compare(DateTime a, DateTime b)
    {
        return a.Equals(b);
    }
}

There is another way to do it more “generic”

We can change the data type of the variables as object. This is a valid option but it will generate an overloading during the boxing and unboxing which is unnecessary. To solve this kind of problems we want to split the behavior and the data type. The first step is define a generic data type to use it in our method. We will use < and > to define our new generic data type. As a non written rule we are going to use the letter T to name our new generic data type but be aware that we can use whatever descriptor we want as you can see in the following code.

class check
{
    public static bool Compare&lt;UNKNOWTYPE&gt;(UNKNOWTYPE a, UNKNOWTYPE b)
    {
        return a.Equals(b);
    }
}

Now that we already have our generic method lets see how to use it.

	Console.WriteLine(&quot;comparing numbers: &quot; + check.Compare&lt;int&gt;(1,2));
	Console.WriteLine(&quot;comparing strings: &quot; + check.Compare&lt;string&gt;(&quot;hello&quot;, &quot;hello&quot;));

Now our method is flexible enough to work with all data type. Something important to highlight is that the data type used to call our method should be the same for the parameters because we define it in that way. If you want to can define several data types I let you make this test.

So next time someone ask to explain generics without doubt you have to explain that allow us to split the behavior and the data type and this technique can be use at class or method level.

 

.Net Interview Questions – Difference between var and dynamic

This will be the first entry about common questions in a .net interview. The main idea is explain clearly the most common concepts every .net developer should have. I know most of the questions in an interview make no sense but be aware when an Interviewer is asking is because he is expecting an specific answer from our side.

Difference between var and dynamic

For those are not familiar with those keywords, var and dynamics allow us to do not define the data type when we are declaring a variable. In this way a variable declaration looks like this.

var x = &quot;string&quot;;
dynamic y = &quot;string&quot;;

If we make a comparison between those variables the result will be that both variables are strings and both have the word “string”.

Both give us the result of the same object type, then what there are two keywords that make the same for? They appear to make the same but they do  not.  How is that possible? Well it is easy. The main difference  between those keywords is when they  join the variable with the data type and in my opinion this is the biggest advantage of dynamics over var.

When we declare a variable using the keyword var we must initialize this variable right away. Why? Well the data type is going to be joined to the variable while we are coding. It means when we initialize the variable this will be joined to the data type  we are using to initialize it. Even if you have the impression that you are not defining a data type you are using one so your code is still strongly typed and you wont be able to store in that variable something else that you stored during the initialization.

In the other hand when we declare a variable using dynamic it is not necessary initialize the variable to make it works. Why? Because dynamic is going to join the data type during the execution using reflection.  As you can imagine one of the strongest point of dynamic is become the variable a weakly typed. This allow us to make the following usage.

dynamic y = &quot;string&quot;;
Console.WriteLine(&quot;It is: {0}&quot;, y);
y = 1;
Console.WriteLine(&quot;It is: {0}&quot;, y);

The biggest advantage of dynamic is the flexibility as we could see before anyway a disadvantage, as every weakly typed language is that we are more plausible to make a mistake and do not know what is store in this variable. Because is using reflection we wont know the issue until running time.

The advantage of var is that allow us to reduce the space during the initialization or we might do not know which is the data type of a LINQ execution. In those cases var allow us to declare quickly a variable with out many problems.

In my opinion what the interviewer is looking with this question is to know how much we know of the .net framework or if we just repeat because some one tells you you have to do it in that way.