Generic learning notes

Source: Internet
Author: User

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>;

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.