Explain generic constraints and when should we use them? ( .NET generic’s interview questions with answers)

“Generic’s helps to decouple logic from the data type. So that we attach any data type with any logic for high reusability.”

 

If you have not understood the above statement please see these C# generic video which explain the same in detail

But many times some logic’s are very specific to specific data types.

public class CompareNumeric
{
        public bool Compareme(UNNKOWDATATYPE v1, UNNKOWDATATYPE v2)
        {
            if (v1>v2)
            {return true;}
            else
{return false;}
        }
}

For example above is a simple generic class which does comparison if one number is greater than other number. Now the greater and less than comparison is very specific to numeric data types. These kind of comparison’s cannot be done on non-numeric types like string.

So if some uses the classes with “int” type its perfectly valid.

CompareNumeric obj = new CompareNumeric();
bool boolgreater = obj.Compare(10,20);

If someone uses it with “double” data type again perfectly valid.

CompareNumeric obj = new CompareNumeric();
bool boolgreater = obj.Compare(100.23,20.45);

But using string data type with this logic will lead undesirable results. So we would like to restrict or put a constraint on what kind of types can be attached to a generic class this is achieved by using “generic constraints”.

CompareNumeric obj = new CompareNumeric();
bool boolgreater = obj.Compare(“interview”,”interviewer”);

Generic type can be restricted by specifying data type using the “WHERE” keyword after the generic class as shown in the below code. Now if any client tries to attach “string” data type with the below class it will not allow, thus avoiding undesirable results.

public class CompareNumeric where UNNKOWDATATYPE : int, double
{

}

One of the favorite C# interview questions revolves around C# collections and generic collection. You can see the video for the same below created by www.questpond.com.

Advertisements

About c# and .NET Interview questions

This blog is for developers who want to crack .NET and C# interviews. It has all tips and tricks needed to crack .NET interviews , C# interview , SQL Server interview , Java interview , WCF Interview , Silverlight interview , WPF interview , LINQ interview , Entity framework Interview. Do not forget to watch our Learn step by step video series. Learn MVC in 16 hours:- https://www.youtube.com/watch?v=Lp7nSImO5vk Learn AngularJS Step by Step:- https://www.youtube.com/watch?v=0kmdjqgO9IY Learn Design Pattern in 8 hours:- https://www.youtube.com/watch?v=YDobmucohqk Learn C# and .NET in 60 days:- https://www.youtube.com/watch?v=yh2SrzCkNQA Learn MSBI in 32 hours:- https://www.youtube.com/watch?v=mGPJx3ocFgg Learn SharePoint Step by Step in 8 hours:- https://youtu.be/C2fW76SwJNU
This entry was posted in Uncategorized and tagged , , , , , . Bookmark the permalink.

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