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

Explain states of a thread in c#?

0 Answers  


What is type class in c#?

0 Answers  


Why do we use abstract class in c#?

0 Answers  


What are the different approaches of passing parameters to a method?

0 Answers  


What is callback function in c#?

0 Answers  






what is the difference between arraylist and hash table using a simple program?

3 Answers   CGI, Polaris, SunGard,


what are some characteristics of an array?

0 Answers  


What is the full form of GAC? Explain its uses?

0 Answers   PUCIT,


what are advantages and disadvantages of Microsoft-provided data provider classes in ADO.NET?

1 Answers   4Cplus,


What is default parameter in c#?

0 Answers  


Are classes passed by reference in c#?

0 Answers  


What is a partial class in c#?

0 Answers  


Categories