.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.

 

Advertisements

One thought on “.Net Interview Questions – Can you explain Generics?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s