This article is a mirror article of machine translation, please click here to jump to the original article.

View: 17968|Reply: 0

[Source] C# where usage analysis

[Copy link]
Posted on 7/12/2016 11:35:43 AM | | |

The where clause is used to specify type constraints that can serve as variables for type parameters defined in generic declarations.
1. Interface constraints.
For example, you can declare a generic class MyGenericClass, so that the type parameter T can implement the IComparable<T> interface:

public class MyGenericClass<T> where T:IComparable { }

2. Base class constraint: Indicates that a type must use the specified class as the base class (or the class itself) to be used as a type parameter for that generic type.
Once such a constraint is used, it must appear before all other constraints on the type of parameter.
class MyClassy<T, U>
where T : class
where U : struct
{
}

The 3.where clause can also include constructor constraints.
You can use the new operator to create an instance of a type parameter; But the type argument must be constrained by the constructor constraint new() for this. The new() constraint lets the compiler know that any type of argument provided must have an accessible parameterless (or default) constructor. For example:
public class MyGenericClass <T> where T: IComparable, new()
{
// The following line is not possible without new() constraint:
T item = new T();
}
The new() constraint appears at the end of the where clause.

4. For multiple type parameters, each type parameter uses a where clause,
For example:
interface MyI { }
class Dictionary<TKey,TVal>
where TKey: IComparable, IEnumerable
where TVal: MyI
{
public void Add(TKey key, TVal val)
{
}
}

5. You can also attach constraints to type parameters of generic methods, such as:

public bool MyMethod<T>(T t) where T : IMyInterface { }

Note that the syntax for describing type parameter constraints is the same for both delegates and methods:

delegate T MyDelegate<T>() where T : new()





Generic Where

Generic Where can qualify type parameters. There are several ways.

·where T : struct restricts the type parameter T must inherit from System.ValueType.

·where T: class restricts type The parameter T must be a reference type, that is, it cannot be inherited from System.ValueType.

where T : new() restricts the type parameter T must have a default constructor

·where T : NameOfClass restricts the type parameter T must inherit from a class or implement an interface.

These qualifiers can be combined, such as: public class Point where T : class, IComparable, new()




Previous:c# NHibernate help class
Next:c# http listens to threads with HttpListener
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com