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


Please Help Members By Posting Answers For Below Questions

How to rotate an Image in C#?

594


What is meaning of type safety in c#?

479


What is data set in c#?

508


What is the difference between values and reference types?

535


What does int32 mean in c#?

478






Do while loops yes or no c#?

532


How do you mark a method obsolete?

601


What is the difference between const and readonly in c#.net?

511


What is the difference between array and list in c#?

493


What is datatable in c#?

506


Can we override main method in c#?

496


what is the difference between interface and multiple interface?

550


What is an argument in c#?

485


What is interface inheritance in c#?

526


Which control cannot be placed in mdi?

541