In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "what are the general constraints of C # generics". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the general constraints of C# generics"?
General constraints in C # generic constraints use C # generics, and the compiler compiles generic code to IL, regardless of the type arguments that the client will use. Therefore, generic code can try to use methods, properties, or members of generic type parameters that are not compatible with specific types of arguments used by the client. This is unacceptable because it amounts to a lack of type safety. In C #, you need to tell the compiler client which constraints the specified types must follow so that they can be used instead of generic type parameters. There are three types of constraints. Derived constraints instruct the compiler that general type parameters are derived from base types such as interfaces or specific base classes. The default constructor constraint instructs the compiler general type parameters to expose the default public constructor (public constructor with no arguments). Reference / value type constraints constrain general type parameters to reference types or value types. Generic types can take advantage of multiple constraints, and you can even have IntelliSense reflect these constraints when using generic type parameters, for example, suggesting methods or members in a base type.
It is important to note that although C# generic constraints are optional, they are usually essential when developing generic types. Without them, the compiler would take a more conservative type-safe approach and allow access to Object-level functionality only in general type parameters. Constraints are part of generic type metadata so that client compilers can also take advantage of them. The client compiler only allows client developers to use types that comply with these constraints, thus implementing type safety.
Examples of the application of general constraints in C# generic constraints:
The following example details the need and usage of C# generic constraints. Suppose you want to add an index function or a key search function to the linked list
Public class LinkedList {T Find (K key) {...} public T this [K key] {get {return Find (key);}
This allows the client to write the following code:
LinkedList list = new LinkedList (); list.AddHead (123, "AAA"); list.AddHead (456, "BBB"); string item = list [456]; Debug.Assert (item = = "BBB")
To implement the search, you need to scan the list, compare the key of each node with the key you are looking for, and return the items of the node for which the key matches. The problem is that the following implementation of Find () cannot be compiled:
T Find (K key) {Node current = masked head; while (current.NextNode! = null) {if (current.Key = = key) / / Will not compile break; else current = current.NextNode;} return current.Item;}
The reason is that the compiler will refuse to compile to the following line:
If (current.Key = = key)
The above line will not compile because the compiler does not know whether K (or the actual type provided by the client) supports the = = operator. For example, by default, the structure does not provide such an implementation. You can try to overcome the limitations of the = = operator by using the IComparable interface:
Public interface IComparable {int CompareTo (object obj);}
If the object you are comparing to is equal to the object that implements the interface, CompareTo () returns 0; therefore, the Find () method can use it as follows:
If (current.Key.CompareTo (key) = = 0)
Unfortunately, this cannot be compiled either, because the compiler cannot know whether K (or the actual type provided by the client) is derived from IComparable.
You can explicitly cast to IComparable to force the compiler to compile comparison lines, unless doing so requires sacrificing type safety:
If ((IComparable) (current.Key)) .Compareto (key) = = 0)
If the client uses a type that is not derived from IComparable, it results in a run-time exception. In addition, when the key type used is a value type rather than a key type parameter, you can box the key, which may have some performance implications.
At this point, I believe you have a deeper understanding of "what are the general constraints of C# generics". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.