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

What are managed providers?

547


What are Uses of CLR

535


State two different types of access modifiers.

562


Hello! How to do this: "Create manifest utility intended for creating update content files. Application should take a set of files as input parameter and generate XML based manifest file as output one." I use C# and vs.net 2003. It's urgent! Help please, thanks. Mayana

1503


What is generic in c#?

461






What are the methods in c#?

530


What is a Jagged Array in C#?

552


Can we change static variable value in c#?

492


Why do we need static class in c#?

575


How many types of collections are there in c#?

484


What is the default value of string in c#?

558


Explain what is the smallest unit of execution in .net?

480


How many types of namespaces available in version4?

521


Why singleton pattern is used in c#?

535


Explain lock, monitors, and mutex object in threading.

513