In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the application methods of C# type parameter constraints". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what are the application methods of C# type parameter constraints?"
Why C# type parameter constraints are used: if you want to check an item in the generic list to determine whether it is valid, or compare it with some other item, the compiler must guarantee to some extent that the operator or method it needs to call will be supported by any type of parameter that the client code may specify. This guarantee is achieved by applying one or more constraints to the generic class definition. For example, a base class constraint tells the compiler that only objects of this type or objects derived from this type can be used as type parameters. Once the compiler has this guarantee, it can allow methods of that type to be called in a generic class. Constraints are applied using the context keyword where. The following code example demonstrates the functionality that can be added to a GenericList class by applying a base class constraint.
When defining a generic class, you can impose restrictions on the type types that client code can use for type parameters when instantiating the class. If client code tries to instantiate a class with a type that is not allowed by a constraint, a compile-time error is generated. These restrictions are called constraints. Constraints are specified using the where context keyword. Six types of constraints are listed below:
◆ T: structure
The type parameter must be a value type. You can specify any value type except Nullable.
◆ T: class
The type parameter must be a reference type, including any class, interface, delegate, or array type.
◆ T:new ()
Type parameters must have a common constructor with no parameters. When used with other constraints, the new () constraint must be * * specified.
◆ T:
The type parameter must be the specified base class or derive from the specified base class.
◆ T:
The type parameter must be the specified interface or implement the specified interface. Multiple interface constraints can be specified. Constraint interfaces can also be generic.
◆ T:U
The type parameter provided for T must be a parameter provided for U or derived from a parameter supplied for U. This is called a naked type constraint.
C # type parameter constraint code
Public class Employee {private string name; private int id; public Employee (string s, int I) {name = s; id = I;} public string Name {get {return name;} set {name = value;}} public int ID {get {return id;} set {id = value Public class GenericList where T: Employee {private class Node {private Node next; private T data; public Node (T t) {next = null; data = t;} public Node Next {get {return next;} set {next = value }} public T Data {get {return data;} set {data = value;} private Node head; public GenericList () / constructor {head = null;} public void AddHead (T t) {Node n = new Node (t) N.Next = head; head = n;} public IEnumerator GetEnumerator () {Node current = head; while (current! = null) {yield return current.Data; current = current.Next;}} public T FindFirstOccurrence (string s) {Node current = head T = null; while (current! = null) {/ / The constraint enables access to the Name property. If (current.Data.Name = = s) {t = current.Data; break;} else {current = current.Next;}} return t;}}
Constraints enable generic classes to use the Employee.Name property because all items of type T are guaranteed to be Employee objects or objects inherited from Employee.
You can apply multiple constraints to the same type parameter, and the constraint itself can be a generic type, as follows:
C # type parameter constraint code
Class EmployeeList where T: Employee, IEmployee, System.IComparable, new () {/ /...}
With constraint type parameters, you can increase the number of allowed operations and method calls supported by all types in the constraint type and its inheritance hierarchy. Therefore, when designing a generic class or method, if you want to perform any operation on a generic member other than a simple assignment or call any method that System.Object does not support, you will need to apply constraints to that type parameter.
When applying the where T: class constraint, it is recommended that you do not use the = = and! = operators for type parameters, because these operators only test reference identity, not value equality. This is true even if these operators are overloaded in a type used as a parameter. The following code illustrates this; even if the String class overloads the = = operator, the output is false.
C # type parameter constraint code
Public static void OpTest (T s, T t) where T: class {System.Console.WriteLine (s = = t);} static void Main () {string S1 = "foo"; System.Text.StringBuilder sb = new System.Text.StringBuilder ("foo"); string S2 = sb.ToString (); OpTest (S1, S2);}
The reason for this is that the compiler only knows that T is a reference type at compile time, so you must use a default operator that is valid for all reference types. If you need to test for value equality, the recommended approach is to apply the where T: IComparable constraint at the same time and implement the interface in any class that will be used to construct a generic class.
C # unbound type parameters
Unconstrained type parameters, such as T in the public class SampleClass {}, are called unbound type parameters. Unbound type parameters have the following rules:
The! = and = = operators cannot be used because there is no guarantee that specific type parameters will support these operators.
You can convert them back and forth to and from System.Object, or explicitly to any interface type.
You can compare them to null. When comparing an unbound parameter to null, the comparison will always return false if the type parameter is a value type.
C # naked type constraint
The generic type parameter used as a constraint is called a naked type constraint. Naked type constraints are useful when a member function that has its own type parameter needs to constrain that parameter to a type parameter that contains the type, as shown in the following example:
C # type parameter constraint code
Class List {void Add (List items) where U: t {/ *... * /}}
In the above example, T is a naked type constraint in the context of the Add method and an unbound type parameter in the context of the List class.
Naked type constraints can also be used in generic class definitions. Note that the naked type constraint must also have been declared in angle brackets along with any other type parameter:
C # type parameter constraint code
/ / naked type constraint public class SampleClass where T: V {}
The naked type constraint of a generic class is very limited because the compiler does not make any assumptions other than that a naked type constraint is derived from System.Object. In cases where you want to force an inheritance relationship between two type parameters, you can use naked type constraints on generic classes.
At this point, I believe you have a deeper understanding of "what are the application methods of C# type parameter constraints?" 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.