Generic knowledge, generic
Int-2147483648 ~ 2147483647 2.1 billion
In inheritance, subclasses have all attributes and actions of the parent class, and any parent class can be replaced by subclasses.
Object is the parent class of all classes.
Determine the type during compilation of generics (latency );
Generics: A method is used to satisfy different types of parameters. generics are actually syntactic sugar and functions provided by the compiler. There is no performance loss.
The number of generic parameters can be defined at will.
============================================= ========================================================
Public class GenericClass // define a generic class
{
Public static void show <T> (T tParameter) // The generics are parameters passed in brackets after a method <T>
{
Console. WriteLine ("this is {0}, type is {1}", tParameter, tParameter. GetType ());
}
Public static int Show <T, S> (string s, int t, int d) // The generic data transmission type can be multiple T, S
{
Int num = Convert. ToInt32 (s );
Return num + t + d;
}
Public static void Add <T> (int I, string s, double d)
{
Double dd = I + d;
Console. WriteLine ("I am {0}, I am {1} years old", s, dd );
}
Public static void ShowObject (object obj) // object is a base class of all types, but it may involve packing and unpacking between different types, there is performance loss; there is no unpacking for generic models, and there is no performance loss
{
Console. WriteLine ("this is {0}, type is {1}", obj, obj. GetType ());
}
Public static void ShowT <SPara> (SPara tparameter) // any type can be used. If the input type is correct, the parameters in the brackets are OK.
{
Console. WriteLine ("this is a generic class Show T, parameter = {0}, parameterType = {1}", tparameter, tparameter. GetType ());
}
Public static void ShowTT <SPara, T, S> (SPara tparameter, T t, S) // Input Multiple types
{
Console. writeLine ("this is a generic class ShowTT, parameter = {0}, parameterType = {1}, t = {2}, tType = {3}, s = {4 }, sType = {5} ", tparameter, tparameter. getType (), t, t. getType (), s, s. getType ());
}
}
========================================================== ========================================
Public class GenericConstaint // defines generic Constraints
{
// After where, it means T is not only a class object, but also a new object (with or without parameters)
Public static T Get <T> (T t) where T: class, new () // must be a reference type, string class ....
{
// The default value of the reference type is null.
T tt = new T (); // The constraint is added with new () to get new
Return default (T );
}
Public static T GetQuery <T> (T t) where T: struct // it must be a value type,
{// The default value of the value type is unknown.
Return default (T );
}
Public static void SayHi <T> (T t) where T: People // t must be a subclass of the People or people type.
{
Console. WriteLine ("ID: {0}, Name: {1}", t. Id, t. name );
T. SayHi ();
}
Public interface ISaySometing
{
Void SayHello ();
}
}
Public class People
{
Public string name {set; get ;}
Public int Id {set; get ;}
Public void SayHi ()
{
Console. WriteLine ("good morning ");
}
}
========================================================== ==========================================
Class Program
{
Static void Main (string [] args)
{
GenericClass. show <DateTime> (DateTime. Now );
Int num = GenericClass. Show <string, string> ("1", 1, 1 );
Console. WriteLine (num );
// When a single type is generic, <T> is the parameter type. When multiple parameters are to be input, multiple parameters can exist in brackets.
GenericClass. Add <int> (4, "zhu", 1.0 );
// Object is the parent class of all types
GenericClass. ShowObject ("132"); // The ShowObject method can be of various types ~~~ String
GenericClass. ShowObject (1231); // The ShowObject method can be of various types ~~~ Int
GenericClass. ShowObject (DateTime. Now. Year); // The ShowObject method can be of various types ~~~ DateTime
GenericClass. ShowT <string> ("Zhu kaibin"); // public static void ShowT <SPara> (SPara tparameter) as long as the corresponding T
GenericClass. ShowT <DateTime> (DateTime. Now );
GenericClass. ShowTT <DateTime, int, string> (DateTime. Now, 100, "");
Console. WriteLine ("************ generic constraints ****************");
// After the generic constraint declaration, the type parameter must meet the Constraint
// GenericConstaint. Get <int> (3); // an error is returned. The generic constraint where is a class, class is a reference type, and int is a value type.
// GenericConstaint. get <string> ("132"); // string is the reference type, because the new () constraint is added, but the string type is not constructed with no parameters. An error is reported for all
GenericConstaint. GetQuery <int> (123); // Value Type
// DateTime is a struct, not a reference type
GenericConstaint. GetQuery <DateTime> (DateTime. Now );
People p = new People ()
{
Id = 1,
Name = "I'm people"
};
GenericConstaint. SayHi <People> (p );
Console. ReadLine ();
// The generic model is not packed or unpacked, so there is no performance loss
// The object type will be used for unpacking and packing ~~~~~ Performance Loss
}
}