In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to understand the definition, inheritance, methods and constraints of C # generics". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. let's study and learn how to understand the definition, inheritance, methods and constraints of C # generics.
C # generics parameterize the type and abstract the type as a parameter, so that we can better reuse the code in practice, and it provides stronger type safety and higher efficiency, but in terms of constraints, it only supports displayed constraints, so it is not so good in terms of flexibility. I think it provides more efficiency because generics are instantiated in the mode of "on-demand", that is, on-demand instantiation, which occurs at JIT (Just In Time) compilation time.
Here's how to define a C# generic class. It's simple, you just need to realize that the type has been parameterized here:
Using System; using System.Collections.Generic; using System.Text; namespace GenericTest {class Program {static void Main (string [] args) {/ / use string,int to instantiate Test
< T,S>Test-like
< string, int>T = new Test
< string, int>("SHY520", 22); / / call the method t.SetValue () in the generic class;}} / * /
< summary>/ define a generic class that has two type parameters, which are TMagens / http://pw.cnblogs.com /
< /summary>/ / /
< typeparam name="T">Type parameter
< /typeparam>/ / /
< typeparam name="S">Type parameter
< /typeparam>Public class Test
< T,S>{/ / generic class type parameters can be used for class member private T name; private S age; public Test (T Name,S Age) {this.name = Name; this.age = Age;} public void SetValue () {Console.WriteLine (name.ToString ()); Console.WriteLine (age.ToString ());}
The above example is not very appropriate, the purpose is to let the beginners of generics understand the definition of generics and instantiation methods, as above, we defined a generic class, so how to achieve C # generic class inheritance? Here you need to meet any of the following two points:
1. In generic class inheritance, the type parameters of the parent class have been instantiated. In this case, the subclass does not have to be a generic class.
2. The type parameters of the parent class are not instantiated, but come from the subclass, that is, both the parent class and the subclass are generic classes, and they have the same type parameters.
/ / if you write in this way, you will obviously fail to find the error public class TestChild: Test of type TMagne S.
< T, S>{} / / the correct way to write it is public class TestChild: Test
< string, int>{} public class TestChild
< T, S>: Test
< T, S>{} public class TestChild
< T, S>: Test
< String, int>{}
Then let's take a look at the generic interface, whose creation and inheritance rules are the same as the generic classes mentioned above. Take a look at the following code:
Public interface IList
< T>{T [] GetElements ();} public interface IDictionary
< K,V>{void Add (K key, V value);} / / the type parameter of the generic interface is either instantiated / / or comes from the type parameter class List declared by the implementation class
< T>: IList
< T>, IDictionary
< int, T>{public T [] GetElements () {return null;} public void Add (int index, T value) {}}
Let's take a look at the C # generic delegate. First, we define a delegate with a type parameter of T, and then use the delegate to call the method in the class:
Using System; using System.Collections.Generic; using System.Text; namespace GenericTest {/ / defines a delegate with a type parameter of T, and the return value type T / / generic delegate supports the application of type parameter delegate string GenericDelete on return values and parameters
< T>(t value); class test {static string F (int I) {return "SHY520";} static string G (string s) {return "SHY520";} static void Main (string [] args) {GenericDelete
< string>G1 = G; GenericDelete
< int>G2 = new GenericDelete
< int>(F);}
Let's take a look at C # generic methods. The generic mechanism of C # only supports the inclusion of type parameters on method declarations, that is, generic methods. In particular, generics do not support the use of type parameters on class / interface members other than methods, but these members can be included in generic types and can use type parameters of generic types. It is also important to say that generic methods can exist in either generic or non-generic types. Let's take a look at the declaration, invocation, overloading and overriding of generic types, respectively.
Using System; using System.Collections.Generic; using System.Text; namespace GenericTest {class GenericClass {/ / declare a generic method public T getvalue
< T>(t) {return t;} / call a generic method / / Note: when calling a generic method, instantiate public int useMethod () {return this.getvalue for the type parameters of the generic method
< int>(10);} / / overloading the getvalue method public int getvalue (int I) {return I;} / the following demonstration overrides / / Note that when the generic method is overridden, the constraint is inherited by default, and there is no need to reassign the constraint relationship abstract class Parent {public abstract K TEST
< K, V>(K k, V v) where K: v;} class Child: Parent {public override T TEST
< T, S>(t t, S s) {return t;}
* Let's take a look at the constraints in C # generics:
Generics in C # only support displayed constraints, because this ensures the type safety required by C #, but the displayed constraints are not necessary. If unconstrained, generic type parameters can only access public methods in System.Object types. " Explicit constraints are expressed by the where clause, and four constraints can be specified: base class constraint, interface constraint, constructor constraint, and value type / reference type constraint.
1. Base class constraints:
Class A {public void F1 () {}} class B {public void F2 () {}} class C
< S,T>Where S: a / / S inherits from A where T: B / T inherits from B {/ / can call F1 on a variable of type S, / / can call F2} on a variable of type T
2. Interface constraints
Interface IPrintable {void Print ();} interface IComparable
< T>{int CompareTo (T v);} interface IKeyProvider
< T>{T GetKey ();} class Dictionary
< K,V>Where K: IComparable
< K>Where V: IPrintable, IKeyProvider
< K>{/ / CompareTo can be called on a variable of type K, / / Print and GetKey can be called on a variable of type V
3. Constructor constraint
Class A {public A () {}} class B {public B (int I) {}} class C
< T>Where T: new () {/ / you can use T t=new T ();} C
< A>C=new C
< A>(); / / Yes, does A have a parameter constructor C
< B>C=new C
< B>(); / / error, B has no parameter constructor
4. Value / reference type constraint
Public struct A {} public class B {} class C
< T>Where T: struct {/ / T is a value type} C here.
< A>C=new C
< A>(); / / Yes, An is a value type C
< B>C=new C
< B>(); / / error, B is a reference type thank you for reading, the above is the content of "how to understand the definition, inheritance, methods and constraints of C# generics". After the study of this article, I believe you have a deeper understanding of the definition, inheritance, methods and constraints of C# generics, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.