Example of a generic class and generic method written by C #

Source: Internet
Author: User

Using System;
Using System.Collections.Generic;
Using System.Text;

Namespace Genericity
{
Class Program
{
Generic Class 1:
public class Genericityhello<t>
{
public void SayHello (T-t)
{
Console.WriteLine (T.tostring ());
}
}

Generic class 2:
public class Genericityhello1<t>
{
Private T TT;

Public T TT
{
Get
{
return TT;
}
Set
{
tt = value;
}
}

public void SayHello ()
{
Console.WriteLine (TT. ToString ());
}
}

public class 中文版
{
public override string ToString ()
{
return "hello!";
}
}

public class Chinese
{
public override string ToString ()
{
Return "Hello!"
}
}

Generic method:
public class CLASS3
{
Public Class3 ()
{
}

public void swap<t> (ref t A, ref T B)
{
T C;
c = A;
A = b;
b = C;
}

}

static void Main (string[] args)
{
genericityhello<english> gh1 = new genericityhello<english> ();
中文版 en = new 中文版 ();
Gh1. SayHello (en);

genericityhello1<chinese> GH2 = new genericityhello1<chinese> ();
Chinese cn = new Chinese ();
Gh2.tt = CN;
Gh2. SayHello ();

To test the generic method:
Console.WriteLine ("Exchange 2 objects:");
string S1 = "Sssssssss-1";
String s2 = "Sssssssss-2";
CLASS3 CLS3 = new CLASS3 ();
Cls3. Swap (ref s1, ref S2);
Console.WriteLine ("S1={0}, S2={1}", S1, S2);

Console.ReadLine ();
}
}
}

Computer Learning Network http://www.why100000.com

Zhang Qing 2007.5.30

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.