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 writeline and write in c#?
What does question mark mean in c#?
what is a structure in c#
Why do we need events in c#?
Is hashset serializable c#?
What is the purpose of namespace in c#?
Explain the difference between and xml documentation tag?
What is response redirect in c#?
What is default boolean value in c#?
What are the drawbacks of extending an interface as opposed to extending a class?
Why do we need escape characters?
What is a class c#?
What is a boolean c#?
What do you mean by string objects are immutable?
Explain publishers and subscribers in events.