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 finally and finalize block?
4. Describe the process when we send a request URL? And who is responsible for that?
Write the difference between TypeOf and GetType?
Is string a data type in c#?
What is the use of main method in c#?
What is dynamic object in c#?
What is class in oops with example in c#?
Give an example of removing an element from the queue?
Explain the top reason to use c# language?
What is jagged array in c#?
What is the difference between constant and readonly in c#?
What are the Types of JIT and what is econo-JIT
How do I format a string in c#?
Can we inherit abstract class in c#?
Why do you need boxing in c#?