Introduction to generics in C #

Source: Internet
Author: User

1. What is generic?

Generic is a feature of programming languages. Allow programmers to write in strongly typed programming languages

 
Some variable parts are defined in the Code, which must be specified before use. Different programming languages have different support for generics from their compilers and runtime environments. A data type that parameterizes types to achieve code reuse and improve the efficiency of software development. A generic class is a reference type and a heap object. It mainly introduces the concept of a type parameter.

Generics are classes, structures, interfaces, and methods with placeholders (type parameters, these Placeholders are one or more types of placeholders stored or used by classes, structures, interfaces, and methods. A generic collection class can use a type parameter as a placeholder for the type of the object it stores. A type parameter is used as a parameter type of its field type and its method. The generic method can use its type parameter as the type of its return value or the type of a specific parameter.
 

2. Application of generics

You can use generic types to maximize code reuse, protect type security, and improve performance.
The most common use of generics is to create a collection class.
The. NET Framework class library contains several new Generic collection classes in the System. Collections. Generic namespace. Use these classes as much as possible to replace common classes, such as the ArrayList in the System. Collections namespace.
You can create your own generic interfaces, generic classes, generic methods, generic events, and generic delegation.
You can restrict generic classes to access specific data types.
Information about types used in generic data types can be obtained through reflection at runtime.
 

3. Generic Type

There are several Collection types:

(1). List: This is the generic type that most applications use. It corresponds to the ArrayList set.

(2). Dictionary, which is also a set of commonly used generic types corresponding to Hashtable.

(3). Collection corresponds to CollectionBase

(4). ReadOnlyCollection corresponds to ReadOnlyCollectionBase, which is a read-only set.

(5). Queue, Stack, and SortedList correspond to non-generic classes with the same name as them.

 

4. Simple examples of generics

This class is an operation class of the Person class. You can freely add or delete the Person class. if you want to write an operation class with the same function as other classes, I believe you only need to replace the Person class. however, after learning about generics, you can use them like this.

List <Person> persons = new List <Person> ();

Persons. Add (new Person ());

Person person = persons [0];

For example, to replace the Person class with the Employee class, you only need to write it like this.

List <Employee> employees = new List <Employee> ();

Employees. Add (new Employee ());

Employee employee = employees [0];

List is a generic class defined in C #. Now we can define it ourselves.

 

5. Use generic and non-generic instances

Class Program
{
Static void Main (string [] args)
{
# Region use non-generic ArrayList
/* ArrayList array = new ArrayList ();
Array. Add (10); // int Is the value type, and the Add method only accepts the reference type (object). Therefore, the value type is boxed (object (10 ))
Array. Add ("Liu Neng ");
Array. Add (DateTime. Now); // DateTime is a value type, while the Add method only accepts the reference type (object). Therefore, the value type is boxed.
Person p = new Person ();
P. Name = "Teng Xiang ";
P. Age = 20;
Array. Add (p );

// Double mianji = Math. PI * (int) array [0] * (int) array [0];
Double mianji = Math. PI * Convert. ToInt32 (array [0]) * Convert. ToInt32 (array [0]);
Int32 num = 10;
Console. WriteLine (num + num );
Console. WriteLine (mianji );

// Foreach (object obj in array)
//{
// Console. WriteLine (obj );
//}*/
# Endregion

 

# Region uses a generic List. list can be used wherever Arrays can be used, even if you are a multi-dimensional array.
List <string> list = new List <string> ();
List. Add ("10 ");

# Endregion
Console. ReadKey ();
}
}
Class Person
{
Public string Name {get; set ;}
Public int Age {get; set ;}
}

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.