1. Back To Blog

.NET Generics interview questions: - Explain Generics and concept of Generic Collection in .NET?

Generics help to separate logic and data type to increase reusability. In other words you can create a class whose data type can be defined on run time.

For instance below is a simple class "class1" with a "compareme" function created using generics. You can see how the unknown data type is put in greater than and less than symbol. Even the compare me method has the unknown data type attached.

During runtime you can define the datatype as shown in the below code snippet.

Concept of Generic Collection in .NET: -

Array List, Stack and Queues provide collections which are not type safe. This leads 2 problems first it's not type safe and second it leads to boxing and unboxing.

By using generics we can have type safety and also we can avoid boxing and unboxing. Below is a simple code snippet which shows a strong typed list of type integer and string.

See .NET Generics interview questions video on Generics and Generics Collections: -

Shiv Prasad Koirala

Visit us @ www.questpond.com or call us at 022-66752917... read more

We are on Social