Constrain on custom generic type in .NET

No.of Views732
Bookmarked0 times
Downloads 
Votes0
By  pranay rana   On  28 Jan 2011 03:01:23
Tag : .NET Frameworks , General
Generics is most important topic included in C# 2.0. Generics are use full in number of way when we do coding in project. One of the most important use is we want to use collections with type safety and without boxing/unboxing.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

Generics is most important topic included in C# 2.0. Generics are use full in number of way when we do coding in project. One of the most important use is we want to use collections with type safety and without boxing/unboxing. But here I am going to discuss about the type of constrain we apply one the custom type we create using generics.

Reference Type

Constrain ensure that type argument is Reference Type. i.e Class, Interface, Delegates, Array etc. Syntax

       Class A<T> where T : class 

Example

ValidInValid

A<MyClass>

A<InterfaceME>  

 A<float[]>

A<int>

A<float>

Note : Always come first when multiple constrain applied.

Value Type

Constrain ensure that type argument is Value Type. i.e int, float, struct, enum etc. But not nullable types. Syntax

       Class A<T> where T : struct
Example
Valid
InValid
A<int>
A<float>     
A<Myclass>
A<InterfaceME>

Note :

Always come first when multiple constrain applied. 

Constructor Type

 Constrain ensure that type argument must have parameterless constructor to create instance of type argument. i.e any value type; nonstatic, nonabstract with parameterless constructor.

Syntax

       Class A<T> where T : new() 
Note :
Always come last when multiple constrain applied.

Derived Type

Constrain ensure that type argument must derived or implemented form specified type. Syntax

       Class A<T> where T : MyClass
       Class A<T> where T : InterfaceME

Example

class A<T> where T : Stream
ValidInValid
A<MemoryStream> 

A<object>

A<string>

class A<T> where T : IDisposible
ValidInValid
A<DataTable> A<StringBuilder>

Points to remeber

You can specify multiple interface for type argument in this constrain but only one. Because no type can derived from more than once class.

ValidInValid
class A<T> where T :  Stream,IDisposible,IEnumerable<T>class A<T> where T :    Stream,ArrayList,IEnumerable<T>

Note:

Specified class must not be struct, a sealed class or not belonging to special type

System.Object
System.Enum
System.ValueType
System.Delegate

Summary

Its good to focus on Constrain type when you are implementing own custom types. This Constrain also applicable to generic methods with type argument.

 
Sign Up to vote for this article
 
About Author
 
pranay rana
Occupation-CEO
Company-GMind Solusion
Member Type-Senior
Location-India
Joined date-08 Jan 2011
Home Page-http://pranayamr.blogspot.com
Blog Page-http://pranayamr.blogspot.com
Hey, I am Pranay Rana, working as a Senior Software engineer in mid-size company located in ahmedabad. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 4.3 years now. For me def. of programming is : Programming is something that you do once and that get used by multiple for many years You can visit me on my blog - http://pranayamr.blogspot.com/ StackOverFlow - http://stackoverflow.com/users/314488/pranay My CV :- http://careers.stackoverflow.com/pranayamr
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top