The generic model is proposed in. NEt framwork 2.0. It solves the performance problem of our project. Using it can greatly improve the efficiency and performance of our project.
Now let's talk about the benefits of generics:
1. Improved code reusability and enhanced security;
2. Mainly used in collection;
3. You can create generic classes, methods, interfaces, and delegation by yourself.
See the following simple example:
Create a User generic class:
# Region
// ================================================ ======================================
//
// Copyright (C) CIM Xi'an R & D center
// All rights reserved
//
// Class Name: lele_ B .show
// Module description:
//
// Created by: Cao daiming
// Creation Time: 05/07/2008 11:02:49
//
// Modifier:
// Modification time:
// Personal website: http://caodaiming.cnblogs.com
//
// ================================================ ======================================
# Endregion
Using System;
Using System. Collections. Generic;
Using System. Text;
Namespace Console_ B
{
Public class Show <T>
{
Public void aa (T B)
{
Console. WriteLine (B. ToString () + "----" + B. GetType (). ToString ());
}
}
}
Add <T> to the Show class to become a generic type. The following aa (T B) method can recognize its type, can this improve code reuse,
Let's see how it is called?
Using System;
Using System. Collections. Generic;
Using System. Text;
Namespace Console_ B
{
Class Program
{
Static void Main (string [] args)
{
Show <int> show = new Show <int> ();
Show. aa (22 );
Show <string> show2 = new Show <string> ();
Show2.aa ("caodaiming ");
}
}
}
Didn't I mention code reuse? This is a good description. If you want to use the aa () method, you can input any data type, which is very convenient and
Easy to use
Next let's take a look at the Generic Model List of List <string> list = new List <string> ();
List. Add ("");
List. Add ("c ");
List. Add ("f ");
List. Add ("B ");
List. Sort ();
Foreach (string l in list)
{
Console. WriteLine (l. ToString ());
}
List <int> list2 = new List <int> ();
List2.Add (1 );
List2.Add (4 );
List2.Add (2 );
List2.Add (5 );
List2.Add (6 );
List2.Add (9 );
List2.Sort ();
Foreach (int l in list2)
{
Console. WriteLine (l. ToString ());
}
Here, what type is used for your generic model, and what type of data should be added when you add it, such as List <string>;