Generic structure
Generic structures and generic classes are almost always,only the generic structure has no inherited attributes.. NETa generic structure provided by the platform is(Nullable Types) Nullablle<t>.introduction of nullable Types,The main purpose is to solve the difference between numbers in a database language and numbers in a programming language .(the number in the database can be empty,numbers cannot be empty in a programming language).becausenullable<t>too cumbersome to use,so a special syntax is introduced.,Use a”?” Operator.Example:
Int? X1;
Nullable<int> x2;
X1 and x2 are two ways of defining equivalence .
Non-null types can be converted to nullable types . ( always successful and can be converted implicitly ).
Nullable types can be converted to non-empty types . the value of the nullable type is NULL throws an exception when it is . ( need to show conversions )
If you do not display conversions , We can use the "??" Operator . Case :
Int? X1=getnullabletype ();
int y1=x1?? 0;
This way, when x1 is null , It is assigned to y1 a 0.
Generic methods
First, consider the case of declaring a generic method :
declaring a generic method
Public T getvalue<t> (T T)
{
return t;
}
invokes a generic method . Note : when calling a generic method , parameter instantiation of the reflection method
public int Usermethod ()
{
Return this.getvalue<int> (10);
}
overloaded getValue method
public int GetValue (int i)
{
return i;
}
At the time of invocation : int res=getvalue<int> (3);
Generic inference: When calling a generic method, theC # compiler is smart enough to infer the correct type based on the type of parameter passed in, and it allows the type specification to be omitted completely, as follows:
int Res=getvalue (3);
Constraints on the reflection parameters in a generic method :
public class Myclass
{
public void mymethod<t> (T t) where t:icomparable<t>
{
Action
}
}
You cannot provide method-level constraints for generic parameters at the class level. Case :
public class Myclass<t>
{
public void mymethod<x> (x x, T T)
where t:icomparable<t>
where x:icomparable<x>
{
Action
}
}
The correct approach is to :
public class Myclass<t>
{
public void mymethod<x> (x x, T T)
where x:icomparable<x>
{
Action
}
}
Override of generic parameter virtual method : The subclass method must redefine the method-specific generic parameter, as follows :
public class BaseClass
{
public virtual void method<t> (T t)
{
}
}
public class Myclass:baseclass
{
public override void Method<t> (T t)
{
}
}
A generic method in a subclass cannot repeat the constraints of a base-class generic method, for example :
public class BaseClass
{
public virtual void method<t> (T-T) where t:new ()
{
}
}
public class Myclass:baseclass
{
correct wording
public override void Method<t> (T t)
{
}
wrong wording
public override void Method<t> (T t) where t:new ()
//{
//}
}
The subclass method invokes the base class implementation of the virtual method: it must specify the type argument to use in place of the generic base method type. You can explicitly specify it yourself, or you can rely on type inference ( if possible ) with the following code:
public class BaseClass
{
public virtual void method<t> (T-T) where t:new ()
{
}
}
public class Myclass:baseclass
{
public override void Method<t> (T t) where t:new ()
{
Base. Method<t> (T);
Base. Method (t);
}
}
Generic delegate
Generic delegates also declare generic types when defined (<T>)
A delegate encapsulates a method that also needs to declare a generic type . because the signature of the method must be consistent with the definition of the delegate .
public delegate void gendelegate<t> (T input);
gendelegate<t> gd = DoWork;
private static void dowork<t> (T input)
{
Do-what
}
The generic delegate gendelegateis defined in the instance, and the method encapsulated by the delegate accepts a parameter, does not return any value, and the parameter type is specified by the generic type T .
To summarize, it is very simple, the big deal is to give ordinary classes, methods, delegates to increase the declaration of generic types into generic classes, generic methods and generic delegates.
In this way, a generic type can be used for a member inside a class that can be used in a method's parameters and can be used to delegate encapsulated methods.
C # Advanced Programming 30-day----generic structure, generic method, generic delegate