15
Generics Ashima Wadhwa

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

Embed Size (px)

Citation preview

Page 1: 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

Generics

Ashima Wadhwa

Page 2: 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

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.

Page 3: 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

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

Page 4: 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

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.

Page 5: 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

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.

Page 6: 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

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]; }

Page 7: 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

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]); } }

Page 8: 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

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(); }

}}

Page 9: 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
Page 10: 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

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(" "); } }

Page 11: 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

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);

} }

Page 12: 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
Page 13: 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

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"));}}

Page 14: 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
Page 15: 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

Queries?