InterviewSolution
Saved Bookmarks
| 1. |
What is Generics? |
|
Answer» Generics are the most powerful features introduced in C# 2.0. It is a type-safe data structure that allows us to write codes that works for any data types. For example, by using a generic type parameter T you can write a single class that other CLIENT code can use without incurring the COST or risk of runtime casts or boxing operations // DECLARE the generic class. public class GenericList<T> { public void Add(T INPUT) { } } class TestGenericList { private class ExampleClass { } static void Main() { // Declare a list of type int. GenericList<int> list1 = new GenericList<int>(); list1.Add(1); // Declare a list of type string. GenericList<string> list2 = new GenericList<string>(); list2.Add(""); // Declare a list of type ExampleClass. GenericList<ExampleClass> list3 = new GenericList<ExampleClass>(); list3.Add(new ExampleClass()); } } |
|