Upload
diana-mills
View
220
Download
0
Embed Size (px)
Citation preview
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 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.
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
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.
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.
using System;using System.Collections.Generic;using System.Linq;using System.Text;
namespace ConsoleApplication1{ class MyStack<T> { T[] StackArray; int StackPointer = 0;
public void Push(T x) { if ( !IsStackFull) StackArray[StackPointer++]=x; } public T Pop() { return (!IsStackempty) ? StackArray[--StackPointer] : StackArray[0]; }
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]); } }
class Program { static void Main() { MyStack<int> StackInt = new MyStack<int>(); MyStack<string> StackString = new MyStack<string>();
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(); }
}}
using System;using System.Collections.Generic;using System.Linq;using System.Text;
namespace ConsoleApplication2{ class simple { static public void ReverseAndPrint<T>(T[] arr) { Array.Reverse(arr); foreach (T item in arr) Console.WriteLine("{0}", item.ToString()); Console.WriteLine(" "); } }
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<int>(intArray); simple.ReverseAndPrint<string>(stringArray); simple.ReverseAndPrint<double>(doublearray);
} }
interface IMyIfc<T>{ T ReturnIt(T invalue);}class Simple : IMyIfc<int>, IMyIfc<string>{ 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"));}}
Queries?