In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "how to customize generic classes in C #". The content is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to customize generic classes in C#".
Generic is a new element of Framework 2.0. it is called "generic" in Chinese and is characterized by a class with angle brackets, such as List.
< T>C # custom generic classes are the most widely used in Collection. In fact, one of the reasons for generics is to solve the problem of boxing and unboxing of elements in the original collection class (if you don't know the concept of boxing and unboxing, please search Baidu). Because of the use of generics, all elements in the collection belong to the same class, which eliminates the hidden danger of different types at the compilation stage-- if the type is wrong, the compilation error.
Only C # custom generic classes are discussed here. The basic customizations are as follows:
Public class MyGeneric
< T>... {private T member; public void Method (T obj)... {}}
Here, a generic class is defined, where T, as a class, can be used in the defined class. Of course, there is no problem defining multiple generic classes.
Public class MyGeneric
< TKey, TValue>... {private TKey key; private TValue value; public void Method (TKey k, TValue v)... {}}
Initialization of generics: generics need to be initialized. After using T doc = default (T), the system initializes generics automatically.
Limitation: if we know that the generic class T, which is going to be passed in, must have some properties, then we can use the MyGeneric
< T>Use these properties of T in. This is achieved through interface.
/ / first define an interface public interface IDocument. {string Title... {get;} string Content. {get;}} / / Let the paradigm class T implement this interface public class MyGeneric.
< T>Where T: IDocument... {public void Method (T v)... {Console.WriteLine (v.Title);}} / / the class passed in must also implement interface public class Document: IDocument. {. } / / use this generic MyGeneric
< Document>Doc = new MyGeneric
< Document>()
Generic methods: we can also define generic methods
Void Swap
< T>(ref T x, ref T y). {T temp = x; x = y; y = temp;}
Generic Generic Delegate: now that you can define generic methods, you can also define generic proxies
Public delegate void delegateSample
< T>(ref T x, ref T y) private void Swap (ref T x, ref T y). {T temp = x; x = y; y = temp;} / / call public void Run (). {int iJournal j; I = 3; j = 5; delegateSample
< int>Sample = new delegateSample
< int>(Swap); sample (I, j);}
Set nullable value types: in general, variables of value types are non-empty. But, Nullable,
< T>Can solve this problem.
Nullable
< int>X; / / this sets a nullable integer variable x x = 4; x + = 3; if (x.HasValue) / / uses the HasValue attribute to check whether x is empty. {Console.WriteLine ("x =" + x.ToString ());} x = null; / / nullable value
Use ArraySegment
< T>To get part of the array. If you want to use some elements of an array, it's a good idea to use ArraySegment to delineate it directly.
Int [] arr =. {1, 2, 3, 4, 5, 6, 7, 8, 9}; / / * the parameter is the transfer array, the second parameter is the offset of the starting segment in the array, and the third parameter is the number of consecutive ArraySegment.
< int>Segment = new ArraySegment
< int>(arr, 2, 3); / / (array, offset, count) for (int I = segment.Offset; I < = segment.Offset + segment.Count; iArray +). {Console.WriteLine (segment.Array [I]); / / use the Array attribute to access the passed array}
In the example, you can access different segments by setting the Offset property and the Count property to different values.
The above is all the content of the article "how to customize generic classes in C #". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.