VuForia how to play online video on iOS on iOS

2023-01-20   ES  

http://www.cnblogs.com/kid-li/archive/2006/11/29/577045.html

generic: use the parameterization type to operate a variety of data types on the same code. Use the “parameterization type” to abstract the type to achieve flexible reuse.

Example code:

class Program

    {

        static void Main(string[] args)

        {

            int obj = 2;

            Test<int> test = new Test<int>(obj);

            Console.WriteLine(“int:” + test.obj);

            string obj2 = “hello world”;

            Test<string> test1 = new Test<string>(obj2);

            Console.WriteLine(“String:” + test1.obj);

            Console.Read();

        }

    }

    class Test<T>

    {

        public T obj;

        public Test(T obj)

        {

            this.obj = obj;

        }

}

output results are:

    int:2

String:hello world

program analysis:

1、  Testis a generic class. Tis an instantiated model type.If T is instantaneous to int, then the member variable OBJ is int type. If T is instantized into string, then OBJ is String type.

2. According to different types, the above procedure shows different values.

C# generic mechanism:

C#generic capacity is supported by CLR when runtime: C#generic code uses a special placeholder to represent the model type when compiled to IL code and metadata, and supports generic operations with proper IL instructions. The real generative instantiated work occurs in the “On-Demand” method, which occurs in JIT compilation.

Look at the metadata of the Main function in the code just now

.method private hidebysig static void  Main(string[] args) cil managed

{

  .entrypoint

  // Code size       79 (0x4f)

  .maxstack  2

  .locals init ([0] int32 obj,

           [1] class CSharpStudy1.Test`1<int32> test,

           [2] string obj2,

           [3] class CSharpStudy1.Test`1<string> test1)

  IL_0000:  nop

  IL_0001:  ldc.i4.2

  IL_0002:  stloc.0

  IL_0003:  ldloc.0

  IL_0004:  newobj     instance void class CSharpStudy1.Test`1<int32>::.ctor(!0)

  IL_0009:  stloc.1

  IL_000a:  ldstr      “int:”

  IL_000f:  ldloc.1

  IL_0010:  ldfld      !0 class CSharpStudy1.Test`1<int32>::obj

  IL_0015:  box        [mscorlib]System.Int32

  IL_001a:  call       string [mscorlib]System.String::Concat(object,

                                                              object)

  IL_001f:  call       void [mscorlib]System.Console::WriteLine(string)

  IL_0024:  nop

  IL_0025:  ldstr      “hello world”

  IL_002a:  stloc.2

  IL_002b:  ldloc.2

  IL_002c:  newobj     instance void class CSharpStudy1.Test`1<string>::.ctor(!0)

  IL_0031:  stloc.3

  IL_0032:  ldstr      “String:”

  IL_0037:  ldloc.3

  IL_0038:  ldfld      !0 class CSharpStudy1.Test`1<string>::obj

  IL_003d:  call       string [mscorlib]System.String::Concat(string,

                                                              string)

  IL_0042:  call       void [mscorlib]System.Console::WriteLine(string)

  IL_0047:  nop

  IL_0048:  call       int32 [mscorlib]System.Console::Read()

  IL_004d:  pop

  IL_004e:  ret

} // end of method Program::Main

Take a look at the metadata of the constructor in the test class

.method public hidebysig specialname rtspecialname

        instance void  .ctor(!T obj) cil managed

{

  // Code size       17 (0x11)

  .maxstack  8

  IL_0000:  ldarg.0

  IL_0001:  call       instance void [mscorlib]System.Object::.ctor()

  IL_0006:  nop

  IL_0007:  nop

  IL_0008:  ldarg.0

  IL_0009:  ldarg.1

  IL_000a:  stfld      !0 class ConsoleCSharpTest1.Test`1<!T>::obj

  IL_000f:  nop

  IL_0010:  ret

} // end of method Test`1::.ctor

1. In the first round of compilation, the compiler only generates the IL code and metadata of the “generic version” for the test <t> type -not instantiated. Essence For example: <! T> displayed in Test type metadata

2, JIT compilation, when the JIT compiler encounters Test <TST> for the first time, it will replace the “model version” IL code and T in the metadata to instantiate the “model version” IL code. For example: <int> displayed in the main function

3, the CLR is the generic type of all types of parameters in “reference type” to generate the same code; but if the type parameter is “value type”, for each different “value type”, the CLR will produce a copy for it Independent code. Because an instantiated type of reference type is the same in memory, it is the same in memory, but when an instantiated value type is different, the size of the distribution in the memory is different.

c# generic characteristics:

1. If the parameters of the institutionalized generic type are the same, the JIT editor will reuse this type, so the dynamic generic capacity of C# avoids the problem of code expansion that C ++ static templates may cause.

2, C#generic types carry abundant metadata, so the generic types of C#can be applied to powerful reflex technology.

3, C #’s generic type uses the constraint method of “base class, interface, constructor, value type/reference type” to achieve “display constraints” of type parameters. High flexibility of hidden constraints based on “signature”

c# generic inheritance:

C# can not only declare the generic types (including classes and structures) separately, but also contains genetic types in the base class. However, if the base class is genetic class, its type is either instantiated or derived from the type parameters of the subclass (the same type of generic type) statement. See the following type

class C<U,V>

class D:C<string,int>

class E<U,V>:C<U,V>

class F<U,V>:C<string,int>

class G:C<U,V>  //illegal

EType CType provides U and V, which means that the above mentioned from the sub -category

FType inherited in C <string, Int>, I personally think that it can be regarded as F to inherit a non -generic class

GType is illegal, because GType is not generic, C is a generic type, G cannot provide a generation for C generics

member of generic type:

member of genetic types can use the type parameters in the generic type statement. But if there is no constraint on the type parameter, it can only be used in this type.Inherited public members. As shown below:

 

generic interface:

The type parameters of the

generic interface are either instantiated or derived from the type parameters of the implementation claim statement

genetic commission:

generic commission support supports the use of parameter types on the commission return value and parameters. These parameter types can also be attached to legal constraints

delegate bool MyDelegate<T>(T value);

class MyClass

{

    static bool F(int i){…}

    static bool G(string s){…}

    static void Main()

    {

        MyDelegate<string> p2 = G;

        MyDelegate<int> p1 = new MyDelegate<int>(F);

    }

}

generic method:

The

1、C#generic mechanism only supports the “type parameter containing type parameters on the method statement” -the generic method.

The

2、C#generic mechanism does not support the type parameters on the statement of other members (including attributes, events, indexes, constructors, and dataginses) except the method except the method, but these members themselves can be included in the generic types and and and and of. Use generic type parameters.

3, generic methods can be included in the generic type or in non -generic types.

generic method declaration: as follows

public static int FunctionName<T>(T value){…}

Acturation of generic methods:

public void Function1<T>(T a);

public void Function1<U>(U a);

This cannot constitute the heavy load of generic methods. Because the compiler cannot determine the generic type Tand u are different, so you cannot determine whether these two methods are different

public void Function1<T>(int x);

public void Function1(int x);

This can constitute a heavy load

public void Function1<T>(T t) where T:A;

public void Function1<T>(T t) where T:B;

This can not constitute a re -load of generic methods. Because the compiler cannot determine A in the constraint conditionsand b are different, so you cannot determine whether these two methods are different

generic method rewriting:

During the rewriting process, the constraints of abstract methods in the abstract class are inherited by default. as follows:

abstract class Base

{

    public abstract T F<T,U>(T t,U u) where U:T;

    public abstract T G<T>(T t) where T:IComparable;

}

class MyClass:Base

{

    public override X F<X,Y>(X x,Y y){…}

    public override T G<T>(T t) where T:IComparable{}

}

For MyClassTwo rewriting methods

The

Fmethod is legal, and the constraints are inherited by default

The

Gmethod is illegal, specifying any constraints is redundant

generic constraint:

Any assumption of

1、C#generic requirements for “type parameters of all generic or generic methods” must be based on “explicit constraints” to maintain the type of type of type required by C#.

2, “Expressing constraints” by where

The expression ofclause can specify “base class constraints”, “interface constraints”, “constructor constraints”, and “value type/reference type constraints”. There are four constraints.

3, “Expressing constraints” is not necessary. If there is no specified “explicit constraint”, the model type parameters will only access system.ObjectPublic method. For example: in the starting example, the defined OBJ member variable. For example, we add a TEST1 in that example that we start, and the two public methods Func1 and Func2 are defined in it, as shown below: below:





The following analyzes these constraints:

Base -based constraint:

The variables of

class A

    {

        public void Func1()

        { }

    }

    class B

    {

        public void Func2()

        { }

    }

    class C<S, T>

        where S : A

        where T : B

    {

        public C(S s,T t)

        {

            //Scan call Func1method

The variables of

            s.Func1();

            //Tcan call Func2method

            t.Func2();

        }

    }

interface constraint:

The object of

interface IA<T>

    {

        T Func1();

    }

    interface IB

    {

        void Func2();

    }

    interface IC<T>

    {

        T Func3();

    }

    class MyClass<T, V>

        where T : IA<T>

        where V : IBIC<V>

    {

        public MyClass(T t,V v)

        {

            //Tcan call Func1

The object of

            t.Func1();

            //Vcan call Func2and func3

            v.Func2();

            v.Func3();

        }

    }

Constructors constraints:

class A

        {

            public A()

            { }

        }

        class B

        {

            public B(int i)

            { }

        }

        class C<T> where T : new()

        {

            T t;

            public C()

            {

                t = new T();

            }

        }

        class D

        {

            public void Func()

            {

                C<A> c = new C<A>();

                C<B> d = new C<B>();

            }

        }

D Object Error: The Type B Must have a Public Parameterless Constructor in order to use it as parameter ‘in the Gene or Method C <T>

    Note: C#Now only support the constraints of non -parameter constructors

    At this time, because we are BTypes are written into a constructor, so that the system will no longer automatically create a non -parameter constructor for B, but if we add a non -ginseng constructor in type B, then the instance of the object D will not be the instance of the object D, the instance of the object D will not be Will report an error. The definition of type B is as follows:

        class B

        {

            public B()

            { }

            public B(int i)

            { }

        }

value type/Quote Type:

public struct A { }

        public class B { }

        public class C<T> where T : struct

        {

        }

        C<A> c1 = new C<A>();

        C<B> c2 = new C<B>();

    c2Object Error when compiling: The Type ‘B’ Must be a Non-NON-NON-NON-NON-NON-NON-NON-NON-NON-NON-NON-NONPE In Order to use It as Parameter ‘T’ in The Gene or MetHor ‘C <T>’

Summary:

The generic ability of

1、C#is supported by CLR at runtime. It is different from the static template supported by C ++ when compiling, and it is also different from the simple generic support supported by the Java at the compiler level.

The generic support of

2、C#includes four types of generic types, structures, interfaces, entrustment, and method members.

The generic type of

3、C#adopts the constraint method of “base class, interface, constructor, value type/reference type” to achieve “explicit constraints” of type parameters. It does not support the signature hidden constraints like C ++ templates.

source

Related Posts

Linux, the previous N IPPAN with the highest number of visits according to the visits log

Front -end HTML framework (frameset) usage detailed analysis

OpenGL Linux installation and configuration one -key compilation+operation

RN open source component React-Native-Video usage

VuForia how to play online video on iOS on iOS

Random Posts

JS DateTime type to the front end iOS is not compatible

1-8 Linux system

html -mobile terminal

spring cloud netflix service found: Eureka (1) register and run WYX

C ++ BFS Seeking the shortest path