In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "the example explanation of C# generic editor". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
C # generic programming example:
Using System; using System.Collections.Generic; using System.Text; namespace GenericTest {class Program {static void Main (string [] args) {/ / use string,int to instantiate the Test class Test t = new Test ("SHY520", 22); / / call the method t.SetValue () in the generic class }} / * * / define a generic class that has two type parameters, namely, public class Test S / Type parameters / Type parameters of the generic class 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 C# generic programming example is not very appropriate, the purpose is to let you who are beginners of C# generics understand the definition of generics and instantiation methods, as above, we defined a generic class, then how to achieve generic class inheritance? Here you need to meet any of the following two points:
1. In C# generic class inheritance, the type parameters of the parent class have been instantiated. In this case, the subclass does not have to be a C# 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 {} / / the correct way to write it is public class TestChild: Test {} public class TestChild: Test {} public class TestChild: Test {}
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 [] GetElements ();} public interface IDictionary {void Add (K key, V value);} / / the type parameters of the generic interface are either instantiated / / or derived from the type parameters declared by the implementation class class List: IList, IDictionary {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 type parameter T, return value type T / / generic delegate supports applying type parameter delegate string GenericDelete (T value) to return values and parameters; class test {static string F (int I) {return "SHY520";} static string G (string s) {return "SHY520" } static void Main (string [] args) {GenericDelete G1 = G; GenericDelete G2 = new GenericDelete (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 (10) for the type parameters of the generic method. } / / overload 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 k, V v) where K: v } class Child: Parent {public override T TEST (T t, S s) {return t;} "example explanation of C# generics" is here. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.