Академический Документы
Профессиональный Документы
Культура Документы
Constraints on Type
Parameters (C#
Programming Guide)
07/20/2015 6 minutes to read Contributors
In this article
Why Use Constraints
Constraining Multiple Parameters
Unbounded Type Parameters
Type Parameters as Constraints
See Also
Constraint Description
C#
name = s;
id = i;
}
public int ID
{
get { return id; }
set { id = value; }
}
}
public Node(T t)
{
next = null;
data = t;
}
public T Data
{
get { return data; }
set { data = value; }
}
}
head = n;
}
public T FindFirstOccurrence(string s)
{
Node current = head;
T t = null;
C#
4 of 7 12/13/2017, 2:28 PM
Constraints on Type Parameters (C# Programming Guide) | Microsoft... https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/ge...
C#
Constraining Multiple
Parameters
You can apply constraints to multiple parameters,
and multiple constraints to a single parameter, as
shown in the following example:
C#
class Base { }
class Test<T, U>
where U : struct
where T : Base, new() { }
C#
class List<T>
{
void Add<U>(List<U> items) where U : T {/*...*/
}
C#
7 of 7 12/13/2017, 2:28 PM