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
What is default value of enum c#?
What are the 2 broad classifications of data types available in c#?
If I have more than one version of one assemblies, then how'll I use old version (how/where to specify version number?)in my application?
What is Web.config?
What is delegate in c#?
1. Describe page life cycle?
What is the default value of object in c#?
What is xml comments in c#?
Explain about generics in c#.net?
what is generics? can u explain 5 to 6 examples on generics that covers class,method,structure,list,delegates?
What is native image generator (ngen.exe)?
What are the three types of predicates?
Explain the difference between user control and custom control. Also, explain their use.
What is an interface class in c#?
What is a bool in c#?