How do you restrict the type which can be used in custom
generic?
Answer Posted / kishore.a
To restrict a type in Generic just specify the constraint
immediately following the method header, prior to the curly
braces of the method block:
For EX: you can restrict a type parameter to implement
IComparable
public class ConsoleTreeControl
{
// Generic method Show<T>
public static void Show<T>(BinaryTree<T> tree, int indent)
where T : IComparable
{
//Your Code
}
}
So this cannot implement IComparable interface.
| Is This Answer Correct ? | 6 Yes | 2 No |
Post New Answer View All Answers
Could you explain the difference between func vs action vs predicate?
What is multithreading with .net?
What you mean by inner exception in c#?
In which situation(s), the use of "Delegate" is a good idea?
Why do we use interface in c#?
What is a bool in c#?
What is datetime minvalue in c#?
What is the usage of Enumeration in C# Programming and is it good to use or not ?
Which debugging tools you can use in the .NET ssSDK?
What is the use of parse in c#?
What is keywords in c#?
Is string a class in c#?
Why do we use static class in c#?
What is constructors, explain with syntax
What is a static in c#?