By 22/07/2015 13 Comments

C# Generics

  • Generics is an important feature of C#.
  • Using generics we can write classes, structures, interfaces, delegates and methods as type safe, it means that can work with any data type.

OR

  • We can write classes, structures, interfaces  and methods that have a particular data type such as int, string, double etc.
  • In C# the term of generics is parameterized type which means it is a type parameter(s) that receives a particular data type at the time of object creation.
  • Generics can be applied on class, interface, method, structure and delegate and that are called generic class, generic interface and generic method etc.
  • The most common use of generics is to create collection classes.

Syntax Of A Generic Class:

class ClassName <parameter-type> {
}

STATEMENT DESCRIPTION
class ClassName It can be any valid class name.
<parameter-type> This is called a type parameter, also multiple type param can be specified. This can be any valid letter such as <T>. This will convert in actual data type when an object of this class will be created, such as int, string etc.


Example – 1, A generic class with an int type object:

using System;
namespace csharpAdvance
{
    // Declaration of a generic class with type parameter .
    class Calculator<T>
    {
        // Declara a field of type T.
        T Number;

        // Declara a constructor of type T.
        public Calculator(T number)
        {
            this.Number = number;
        }

        // Declara a method with return type T.
        public T Square()
        {
            return this.Number;
        }
    } 

    class Program
    {
        static void Main(string[] args)
        {
            // Object of calculator is being created.
            // Note: we have specified the type of a class which is an .
            // This int is recieved by  in above class.
            Calculator<int> calculator = new Calculator<int>(6);

            // Square() method is being called.
            int result = calculator.Square() * calculator.Square();

            Console.WriteLine("Square of "+calculator.Square()+" : " + result);

            Console.ReadKey();
        }
    }
    /*  The Output will be:
     
        Square of 6 : 36
     */
}


Example – 2, A generic class with string type object:

using System;
namespace csharpAdvance
{
    // Declaration of a generic class with type parameter .
    class Website<T>
    {
        // Declara a field of type T.
        T WebsiteName;

        // Declara a constructor of type T.
        public Website(T websiteName)
        {
            this.WebsiteName = websiteName;
        }

        // Declara a method with return type T.
        public T GetWebsiteName()
        {
            return this.WebsiteName;
        }
    } 

    class Program
    {
        static void Main(string[] args)
        {
            // Object of calculator is being created.
            // Note: we have specified the type of a class which is an .
            // This string is recieved by  in above class.

            Website<string>website = new Website<string>("Tutorialstown.com");

            // Website name is being displayed.
            Console.WriteLine("Website name: {0} " , website.GetWebsiteName());
            Console.ReadKey();
        }
    }
    /*  The Output will be:
     
        Website name: Tutorialstown.com
     */
}


Example – 3, Generic interface:

using System;
namespace csharpAdvance
{
    // Declare a generic interface.
    interface Website<T>
    {
        T GetWebsiteName();
        T GetProtocol(T protocol);
    }

    // Generic class MyWebsite implements a generic interface Website.
    class MyWebsite<T> : Website<T>
    {
        T WebsiteName;

        public MyWebsite(T websiteName)
        {
            this.WebsiteName = websiteName;
        }

        public T GetProtocol(T protocol)
        {
            return protocol;
        }

        public T GetWebsiteName()
        {
            return this.WebsiteName;
        }
    } 

    class Program
    {
        static void Main(string[] args)
        {
            // Object of calculator is being created.
            // Note: we have specified the type of a class which is an .
            // This string is recieved by  in above class.

            MyWebsite<string> myWebsite = new MyWebsite<string>("Tutorialstown.com");

            // Website information is being displayed.
            Console.WriteLine("My website protocol: {0} ", myWebsite.GetProtocol("Https"));
            Console.WriteLine("My website name: {0} ", myWebsite.GetWebsiteName());
            Console.ReadKey();
        }
    }
    /*  The Output will be:
     
        My website protocol: Https
        My website name: Tutorialstown.com
     */
}


Advantages Of Generics:

  • Using generics we can write our own classes, structures, interfaces and methods which can be maximum re-usable later along with a particular data type (type safe).
  • Generics makes classes and interfaces more generalized, secure and type safe.
  • Any data type can be used with generic classes and interfaces freely.






Posted in: C# Advance, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .