VisualBuilder
  Home > Dotnet > Tutorials > Generic Classes and Methods - .NET Tutorial
Tell a friend
Link to us
Total Members
      Members: 84606
     
Sitemap Forum Chat
Home
.NET Tutorial Home
1 . Overview of .Net Framework
2 . .Net Components:
3 . CLR (Common Language Runtime)
4 . Steps to Run a program in CLR
5 . Assembly
6 . GAC (Global Assembly CACHE)
7 . Strong Name in .Net Assembly
8 . ILDASM: Intermediate Language Disassembler in .Net
9 . Custom Attributes in .Net
10 . Application Domain in .Net
11 . Code Access Security in .Net
12 . Type System in .Net -1
13 . Type System in .Net -2
14 . Reflection: Inspection of a Type\'s Metadata
15 . Introduction to Generics
16 . Generic Classes and Methods
17 . Generic Methods in .Net 2.0
18 . Overview of Inheritance in Generics classes
19 . Connection pooling in .Net Applications
20 . Deployment in .Net
 
Dotnet Group Home
Dotnet Discussion (9)
Dotnet Members (2825)
Dotnet Resources
Dotnet Source Code (0)
Dotnet Articles (0)
Dotnet Blogs
Dotnet Jobs
Dotnet Components (2)
Dotnet Books
Dotnet Websites (43)
Dotnet News (186)
Dotnet Q & A (3)
- Dotnet Ask Question
- Dotnet Questions
- Dotnet Unanswered Questions
 
GROUPS
.NET
ASP.NET
.NET
C#
ASP
Visual Basic
Java
Java
JSP
EJB
Other
Delphi
C++
Ajax
UML
JavaScript
PHP
Web Design
Web Hosting
SQL Server
Oracle
Project Management
More Groups

 
LEARNING CENTER
TUTORIALS
.NET
.NET Tutorial
ASP Tutorial
ASP.NET Database Tutorial
ASP.NET Development Tips
ASP.Net Security,Internationalisation And Deployment
ASP.NET Server Controls Tips
ASP.NET Tutorial
C Sharp Tutorial
Web Development
Flex Tutorial
HTML Tutorial
Learn AJAX Tutorial
PHP Tutorial
Software Development
Database Tutorial
SQL Tutorial
UML Tutorial
Java
Ant Tutorial
EJB 3 Tutorial
Hibernate Tutorial
Java Tutorial
Java Web Component Tutorial
Java XML Tutorial
JDBC Tutorial
JDK1.5 Tutorial
JSF Tutorial
JSP And J2EE Design Tutorial
JSP Tutorial
Spring Tutorial
Struts Tutorial

RESOURCES
Q & A (432 )
Source Code (3217 )
Articles (11 )
Components (1589 )
News (880 )
Websites (1207 )

SUBMISSIONS
Submit Article
Submit Website
Submit News
Submit Source Code
Submit Component

COMMUNITY
Members Directory
Discussion Forum
Chat

SITE
About Us
Sitemap
Search
Contact Us
Link To Us
Feedback
Tell a Friend
Partners
Advertise


Dotnet Tutorial
 Generic Classes and Methods
  << Prev: Introduction to Generics Next: Generic Methods in .Net 2.0 >>

CLR Support for Generics:


 


CLR supports and recognize generics. The important concept behind this is for instance, if you define a generic type MyList<T>, only one definition of that type is present in MSIL. When the program executes, different classes are dynamically created, one for each type for the parameterized type. If you use MyList<int> and MyList<double>, then two classes are created on the fly when your program executes.


 


Generic Class in .Net 2.0


 


Demonstrate: Example of Generic Class


 


//******************************MyList.cs************************


#region Using directives


using System;


using System.Collections.Generic;


using System.Text;


#endregion


 


namespace CLRSupportExample


{


public class MyList<T>


{


private static int objCount = 0;


  public MyList () {


    objCount++;


  }


public int Count {


  get


  { return objCount;}


  }


}


}


 


//****************************Program.cs*************************


 


#region Using directives


using System;


using System.Collections.Generic;


using System.Text;


#endregion


 


namespace CLRSupportExample


{


class SampleClass {}


class Program


{


static void Main (string[] args)


{


    MyList<int> myIntList = new MyList<int>();


    MyList<int> myIntList2 = new MyList<int>();


    MyList<double> myDoubleList= new MyList<double>();


    MyList<SampleClass> mySampleList= new MyList<SampleClass>();


 


  Console.WriteLine(myIntList.Count);


  Console.WriteLine(myIntList2.Count);


  Console.WriteLine(myDoubleList.Count);


  Console.WriteLine(mySampleList.Count);


  Console.WriteLine(new


  MyList<SampleClass>().Count);


  Console.ReadLine();


    }


}


}


 


Here we have generic class named MyList. Within the MyList class, I have a static field, objCount. I am incrementing this within the constructor so I can find out how many objects of that type is created by the user of my class. The Count property returns the number of instances of the same type as the instance on which it is called.


In the Main() method, I am creating two instances of MyList<int>, one instance of MyList<double>, and two instances of MyList<SampleClass>, where SampleClass is a class I have defined.


 


Output:


2


2


1


1


2


 


  << Prev: Introduction to Generics Next: Generic Methods in .Net 2.0 >>
Dotnet Tutorial Home
Give feedback and win a prize.

 
   Printer Friendly
   Email to a friend
   Add to my Favourites    
  Download PDF version
   Report Bad Submissions
   Submit Feedback
 
  Delicious   Digg   Technorati   Blink   Furl   Reddit   Newsvine   Google Click each image to add
this page to each site.
 
 
Welcome Guest Signup
MEMBER'S PANEL
EMAIL
PASSWORD
Forgot your password?
New User? Click Here!
 
Resend Activation Email!
 
SEARCH
 
 
LINKS
MSN
Video Surveillance
VoIP Internettelefonie AT
Gift to Pakistan
 
ADVERTISEMENT
 
PARTNER LIST

More
 
 
 

Home | Login | About Us | Contact Us | Privacy Policy | Advertising