The example of this paper is analyzed. NET base custom generics. Share to everyone for your reference. The specific analysis is as follows:
Generics are used very frequently in. NET, and in console applications, the default is to introduce the System.Collection.Generics namespace, which provides the generic:list<t> and dictionary<t that we often use And believe that they are strong. There is also a simple generic type that we often use: system.nullable<t> We can:
System.nullable<int> Nullableint;
Declares a nullable int type, and since the C # syntax simplifies this, we usually do not write this way:
Int? Nullableint
The following is an overview of how to customize generics.
Defining generic Classes
Creating a generic class requires the use of the angle bracket syntax in the class definition:
Copy Code code as follows:
Class mygenericclass<t>
{
...
}
T can be any identifier, just follow the naming rules.
Types can be used for return types of class members, method parameter types, and so on, such as:
Copy Code code as follows:
Class Mygenericclass<t1, T2, t3>
{
Private T1 T1object;
Public MyGenericClass (T1 item)
{
T1object = Item;
}
Public T1 T1object
{
Get
{
return t1object;
}
}
}
Note If you cannot assume what type is provided. The following code cannot be executed:
Copy Code code as follows:
Class Mygenericclass<t1, T2, t3>
{
Private T1 T1object;
Public MyGenericClass ()
{
T1object = new T1 ();
}
}
Because we don't know if T1 has a public default constructor.
Default keyword
If we define a generic field, we want to initialize it in the constructor, but we do not know whether it is a reference type or a value type, then default is useful:
Copy Code code as follows:
Public MyGenericClass ()
{
T1object = Default (T1);
}
If the value type is assigned a value of 0, the reference type is assigned null.
Constraint type
When defining generics we can constrain the type by implementing the WHERE keyword:
Copy Code code as follows:
Class mygenericclass<t1> where T:constraint1,constraint
{
...
}
Constraint defines a constraint, with multiple constraints separated by commas, if there are multiple types:
Copy Code code as follows:
Class Mygenericclass<t1, t2> where t1:constraint1 where t2:constraint
{
...
}
Some of the available constraints are given below
Constraint description
Where T:struct uses structural constraints, type T must be a value type
Where T:CALSS class constraint specifies that type T must be a reference type
Where t:interface specifies that the type T must implement an interface or implement an interface
Where t:base-class specifies that the type T must be a base class or derive from the basis class
where t:new () specifies that the type T must have a default constructor
Below the combination of the above knowledge to give an example: (PS do not see the code more actually very simple patience to look down)
Define four classes animal, Cow, chicken, and Supercow first
Copy Code code as follows:
#region Animal virtual base class has a Name property feed method and a virtual method makeanoise
The virtual base class has a Name property feed method and a virtual method makeanoise
Public abstract class Animal
{
protected string name;
public string Name
{
Get
{
return name;
}
Set
{
name = value;
}
}
Public Animal ()
{
Name = "The animal with no name";
}
Public Animal (String newName)
{
name = NewName;
}
public void Feed ()
{
Console.WriteLine ("{0} has been fed.", name);
}
public abstract void makeanoise ();
}
#endregion
//cow Animal Subclass, Implementing virtual methods
public class Cow:animal
{
public Cow ( String Name:
Base (name)
{
}
public override void Makeanoise ()
{
Console.WriteLine ("{0} says ' Moo! '", name);
}
}
//chicken class, Animal subclass
public class Chicken:animal
{
public Chicken (string name)
: Base (name)
{}
public override void Makeanoise ()
{
Console.WriteLine ("{0} says ' Cluck '", name);
}
}
Cow, there's a way to do it fly
Class Supercow:cow
{
Public Supercow (string name): Base (name)
{
}
public void Fly ()
{
Console.WriteLine ("{0} is flying!", name);
}
public override void Makeanoise ()
{
Console.WriteLine ("{0} says ' I am supercow! '", name);
}
}
Once the class is ready, we can begin to define our generics:
Copy Code code as follows:
Inherits the iterator interface, which facilitates the use of a foreach constraint for its type animal and its subclasses
public class farm<t>:ienumerable<t> where t:animal
{
Private list<t> animals = new list<t> ();
Public list<t> Animals
{
Get
{
return animals;
}
}
Iterators
Public ienumerator<t> GetEnumerator ()
{
Return animals. GetEnumerator ();
}
IEnumerator Ienumerable.getenumerator ()
{
Return animals. GetEnumerator ();
}
Perform all animal makeanoise ()
public void makenoises ()
{
foreach (T Animal in animals)
{
Animal. Makeanoise ();
}
}
Perform all animal feeds ()
public void Feedtheanimals ()
{
foreach (T Animal in animals)
{
Animal. Feed ();
}
}
Get the cow in animals
Public farm<cow> getcows ()
{
farm<cow> cowfarm = new farm<cow> ();
foreach (T Animal in animals)
{
if (animal is Cow)
{
COWFARM.ANIMALS.ADD (animal as Cow);
}
}
return cowfarm;
}
}
The generic definition is OK, we call it by writing code:
Copy Code code as follows:
Class Program
{
static void Main (string[] args)
{
farm<animal> Farm = new farm<animal> ();
Farm. Animals.add (New Cow ("Jack"));
Farm. Animals.add (New Chicken ("Vera"));
Farm. Animals.add (New Chicken ("Sally"));
Farm. Animals.add (New Supercow ("Kevin"));
Farm. Makenoises ();
Farm<cow> dairyfarm = Farm. Getcows ();
Dairyfarm.feedtheanimals ();
foreach (Cow Cow in Dairyfarm)
{
if (Cow is Supercow)
{
(Cow as Supercow). Fly ();
}
}
Console.readkey ();
}
}
I hope this article will help you with your. NET programming.