How do you restrict the type which can be used in custom
generic?



How do you restrict the type which can be used in custom generic?..

Answer / 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

More C Sharp Interview Questions

What kind of the information stored inside the assembly?

0 Answers   CitiusTech,


What types of object can I throw as exceptions?

0 Answers  


Why singleton class is sealed?

0 Answers  


Is a structure a class?

0 Answers  


How to implement delegates in c#.net

0 Answers  






What is the difference between namespace and class in c#?

0 Answers  


Why is c# good for games?

0 Answers  


What is string literal in c#?

0 Answers  


Why do we use anonymous method in c#?

0 Answers  


How do you sort a list in c#?

0 Answers  


Where CANNOT Destructors be implemented ?

3 Answers  


What is executescalar in c#?

0 Answers  


Categories