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 do constructors do in c#?

0 Answers  


What is arraylist?

0 Answers  


What is default constructor in c#?

0 Answers  


What is the purpose of private constructor in c#?

0 Answers  


What are custom exceptions in C#?

0 Answers   Arigo Infotech,






What is lazy in c#?

0 Answers  


What is cls, cts and clr in net?

0 Answers  


Can abstract class instantiated c#?

0 Answers  


What are the Uses of CLR

0 Answers   TCS,


Is null == null c#?

0 Answers  


What is the CTS, and how does it relate to the CLS?

0 Answers   Arigo Infotech,


Is dll a library?

0 Answers  


Categories