dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online Membersbaskar
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Resources » .NET programming » General

What is Generics Classes? Explanation with a sample code.


Posted Date:     Category: General    
Author: Member Level: Gold    Points: 10


This article will explain about Generics classes. How you can use Generics classes in asp.net. The main use of Generics classes is with has tables, stack, queue, linked list and other collections. A basic sample code is provided in this article. Learn shat is Generics Classes? Generics Classes with a sample code.



 


What is Generics Classes? Sample code for Generic Classes


Generic classes are available in System.Collections.Generic namespace.
Syntax:
public class List


Among the methods of that class is this one:
public Add(T item)

T is the placeholder for the type that an instance of the generic class
System.Collections.Generic.List will store.

In defining an instance of the generic class, one
specifies the actual type that the instance will store:

List myListOfStrings = new List();

Then one can use the Add() method of the generic class instance like so:

myListOfStrings.Add("Hello, World");

A generic interface may be implemented by a generic type or a nongeneric type.
Example:

public interface IMyGenericInterface
{
void MyMethod();
}


A sample code to understand Generic Class.


public class Printer
{
public void Print(T argument)
{
if(typeof(T) == typeof(string))
{
Console.WriteLine(argument);
}
else
{
Console.WriteLine(argument.ToString());
}
}
static void Main(string[] arguments)
{
Printer printer = new Printer();
printer.Print("Hello, World");
Console.WriteLine("Done");
Console.ReadKey();
}
}





Did you like this resource? Share it with your friends and show your love!


Responses to "What is Generics Classes? Explanation with a sample code."

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: How to update items in InfoPath dropdown from SharePoint list ?
    Previous Resource: Solution for Windowless ActiveX control issue in windows 7 machine
    Return to Resources
    Post New Resource
    Category: General


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    Generic class in asp.net  .  Sample code  .  Generic class  .  



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.