C # Generics

Generics were firstly added to v 2.0 of the C# language and the common language runtime (CLR). Generics represent to the .NET Framework the concept of type parameters, which make it possible to design classes and methods that defer the specification of one or more types until the class or method is declared and instantiated by client code. For example, by using a generic type parameter T you can write a single class that other client code can use without incurring the cost or risk of runtime casts or boxing operations, as shown here:

// declare the generic class. 
public class GenericList<T>
{
    void Add(T input) { }
}
class TestGenericList
{
    private class ExampleClass { }
    static void Main()
    {
        // Declare a list of type int.
        GenericList<int> list1 = new GenericList<int>();

        // Declare a list of type string.
        GenericList<string> list2 = new GenericList<string>();

        // Declare a list of type ExampleClass.
        GenericList<ExampleClass> list3 = new GenericList<ExampleClass>();
    }
}

Features of Generics

·         Generic types help to maximize code reuse, type safety, and performance.
·         Common use of generics is to create collection classes. The .NET Framework class library contains several new generic collection classes in the System.Collections.Generic namespace. These should be used whenever possible instead of classes such as ArrayList in the System.Collections namespace.
·         You can make your own generic interfaces, classes, methods, events and delegates.
·         Generic classes may be constrained to enable access to methods on particular data types.
·         You can get information on the types that are used in a generic data type may be obtained at run-time by using reflection.


You write the specifications for the class or the method, with substitute parameters for data types. When the compiler encounters a constructor for the class or a function call for the method, it generates code to handle the specific data type. A simple example would help understanding the concept:
using System;
using System.Collections.Generic;

namespace GenericApplication
{
   public class MyGenericArray<T>
   {
      private T[] array;
      public MyGenericArray(int size)
      {
         array = new T[size + 1];
      }
      
      public T getItem(int index)
      {
         return array[index];
      }
      
      public void setItem(int index, T value)
      {
         array[index] = value;
      }
   }
   
   class Tester
   {
      static void Main(string[] args)
      {
         
         //declaring an int array
         MyGenericArray<int> intArray = new MyGenericArray<int>(5);
         
         //setting values
         for (int c = 0; c < 5; c++)
         {
            intArray.setItem(c, c*5);
         }
         
         //retrieving the values
         for (int c = 0; c < 5; c++)
         {
            Console.Write(intArray.getItem(c) + " ");
         }
         
         Console.WriteLine();
         
         //declaring a character array
         MyGenericArray<char> charArray = new MyGenericArray<char>(5);
         
         //setting values
         for (int c = 0; c < 5; c++)
         {
            charArray.setItem(c, (char)(c+97));
         }
         
         //retrieving the values
         for (int c = 0; c< 5; c++)
         {
            Console.Write(charArray.getItem(c) + " ");
         }
         Console.WriteLine();
         
         Console.ReadKey();
      }
   }
}
When the above code is compiled and executed, it produces the following result:
0 5 10 15 20
a b c d e 
 
We will be discuss more about Generics in our next post.

how to display word by a console application in C#


"Welcome to C#.net" in a console application

Here, we will create a small console application. This application will show “Welcome to C#.net” into the console display.

Steps:
  1. Start Microsoft Visual Studio 2008
  2. Click File > New > Project
You will see the following window


                                                Figure: New Project Creation Templates

  1. Select Windows > Console Application
  2. Write a name in ‘Name’ box and you can change the destination folder of the project directory by clicking the ‘Browse...’ button
  3. Click ‘OK’ button
  4. You will see the following code
        using System;
     using System.Collections.Generic;
     using System.Linq;
     using System.Text;

     namespace WalkThrough1
     {
            class Program
           {
                 static void Main(string[] args)
                {
                }
           }
     }

  1. Now write the following code snippet inside the main method
    Console.WriteLine("Welcome to C#.net");
     Console.ReadKey();
  1. Then Press F5 or Click on the Debug button
  1. Now You will see the output given below