The Basic Concepts of Generics in C#

If you've been programming in C++ then you are familiar with Templates (often called a function). A Template is used to create the content of a class without specifying the data type of arguments. Templates can increase the usefulness of the source code. With C# you will become familiar with the concept of generics that are similar to C++ Templates.

1. Generic concept

Generics have existed since version 2.0 of C# and the Common Language Runtime (CLR). Generics provide the .NET framework the new concept of parameterized types, classes as well as the function definition will not need to specify the parameters of the data type, all the most common form of treatment, from which users can develop in layers, with the function of a certain type of real data. Thus we see that inheritance concepts are strongly applied here, to help you save substantial costs in development, for example to build 1 function for a quick sort array without the center value of each element in the array is what type, then when you use a new type declaration for that element. So you can use the quick-sort function with the integer array, the array and the array of real characters.

At this point you might still not be able to figure out Generics, let's start with a simple example to initially envision Generics and the benefits of them:

The example above shows that the GenericList class is created with the data type T General, to use new T's we specify the new data type. As an example, in list1, T is equivalent to the type int; list2 list3 T is a string and T is a certain layers.

2. Summary of Generic certain characteristics

Generic definition of a data manipulation with the most common data types to maximize code reuse in the program, creating the type of data security, provides the highest performance.

Application of the most popular generic collection class is created (roughly translated: class data set).

The. NET Framework contains the collection framework available in the System.Collections.Generic namespace.

With that you can create generic interfaces, classes, methods, events and delegates.

Generics can be restricted to allow access to the method (method) on the particular type of data.


In the example above, we define 1 Genericlist class for a linked list with each node's data type T. Inside the Node was created with T, and create the Next pointer to the link list, the Data to save the data, with the constructor empty list, add a new element to the list. In the main function we initialize the linked list data type is int, and for each node loop (0 -> 9) to add a value to the linked list, and finally use foreach to find all the elements and print out a list to the Console.

3. Conclusion

See the example shown in the class definitions that are working with Genericlist abstract data type T, and can replace any type of data. To do this we define the process to ensure the properties, the method works properly on various types of data, minimizing the errors that arise. Study and work a lot with the following Generics to ensure your ability to write generalized code, an ability to do abstract thinking will increase your lot.

In the first part of the article content related to Yield Return, to learn more about the content you can see in the article below:

Yield Return | CSharp Programming

From my Site: