Presentation is loading. Please wait.

Presentation is loading. Please wait.

Generics Ashima Wadhwa. What are generics Generics were added by C# 2.0 the term generics means parameterized types. Using generics, you can define a.

Similar presentations


Presentation on theme: "Generics Ashima Wadhwa. What are generics Generics were added by C# 2.0 the term generics means parameterized types. Using generics, you can define a."— Presentation transcript:

1 Generics Ashima Wadhwa

2 What are generics Generics were added by C# 2.0 the term generics means parameterized types. Using generics, you can define a solution once, independently of any specific type of data, and then apply that solution to a wide variety of data types without any additional effort.

3 Parameterized types Parameterized types are important because they enable you to create – classes, structures, interfaces, methods, and delegates in which the type of data upon which they operate is specified as a parameter. A class, structure, interface, method, or delegate that operates on a parameterized type is called a generic. For eg. generic class or generic method

4 What was used before generics C# 1.0 had the ability to create generalized code by operating through references of type object. Since Object is the base class of all other classes, an object reference can refer to any type of object. Thus, in pre-generics code, generalized code used object references to operate on a variety of different kinds of objects.

5 Need for generics Generalized code using object references did not provide type safety because casts were needed to convert between the object type and the actual type of the data. This was a potential source of errors because it was possible to accidentally use an incorrect cast. Generics avoid this problem by providing the type safety that was lacking. Generics also streamline the process because it is no longer necessary to employ casts to translate between object and the type of data that is actually being operated upon. Thus, generics expand your ability to re-use code, and let you do so safely and easily.

6 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class MyStack { T[] StackArray; int StackPointer = 0; public void Push(T x) { if ( !IsStackFull) StackArray[StackPointer++]=x; } public T Pop() { return (!IsStackempty) ? StackArray[--StackPointer] : StackArray[0]; }

7 const int Maxstack =10; bool IsStackFull { get { return StackPointer >= Maxstack; } bool IsStackempty { get { return StackPointer <=0; } public MyStack() { StackArray = new T[Maxstack]; } public void Print() { for(int i = StackPointer-1 ; i>=0; i--) Console.WriteLine(" Value:{0}",StackArray[i]); }

8 class Program { static void Main() { MyStack StackInt = new MyStack (); MyStack StackString = new MyStack (); StackInt.Push(3); StackInt.Push(63); StackInt.Push(73); StackInt.Push(34); StackInt.Push(30); StackInt.Print(); StackString.Push("IN GIBS"); StackString.Push("learning C#"); StackString.Push("We MCA3 Students"); StackString.Print(); }

9

10 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication2 { class simple { static public void ReverseAndPrint (T[] arr) { Array.Reverse(arr); foreach (T item in arr) Console.WriteLine("{0}", item.ToString()); Console.WriteLine(" "); }

11 class Program { static void Main() { var intArray = new int[] { 1, 3, 2, 4, 9 }; var stringArray = new string[] { "first","second", "third" }; var doublearray = new double[] { 8.78,8.96,2.34 }; simple.ReverseAndPrint (intArray); simple.ReverseAndPrint (stringArray); simple.ReverseAndPrint (doublearray); }

12

13 interface IMyIfc { T ReturnIt(T invalue); } class Simple : IMyIfc, IMyIfc { public int ReturnIt(int invalue) { return invalue; } public string ReturnIt(string invalue) { return invalue; } class Program { static void Main() { Simple trivial = new Simple(); Console.WriteLine("{0}", trivial.ReturnIt(5)); Console.WriteLine("{0}",trivial.ReturnIt("Hi there")); }

14

15 Queries?


Download ppt "Generics Ashima Wadhwa. What are generics Generics were added by C# 2.0 the term generics means parameterized types. Using generics, you can define a."

Similar presentations


Ads by Google