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
What is the difference between dll and lib?
Can we override static class in c#?
Define parsing?
What are access modifiers in c#?
What does type safety mean?
What is a collection class in c#?
Value Type and Reference Type Data type in C#?
Explain the process of abstraction with an example?
Name the two classes are required for implementing a windows service?
What are delegates?
What are PE(Portable Executable)?
How do I start a program in c#?
Is typeof c#?
What is the difference between wrapper class and primitive?
What is lock in c#?