Generics Ashima Wadhwa What are generics Generics were

  • Slides: 15
Download presentation
Generics Ashima Wadhwa

Generics Ashima Wadhwa

What are generics • Generics were added by C# 2. 0 • the term

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 –

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

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

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 Console.

using System; using System. Collections. Generic; using System. Linq; using System. Text; namespace Console. Application 1 { class My. Stack<T> { T[] Stack. Array; int Stack. Pointer = 0; public void Push(T x) { if ( !Is. Stack. Full) Stack. Array[Stack. Pointer++]=x; } public T Pop() { return (!Is. Stackempty) ? Stack. Array[--Stack. Pointer] : Stack. Array[0]; }

const int Maxstack =10; bool Is. Stack. Full { get { return Stack. Pointer

const int Maxstack =10; bool Is. Stack. Full { get { return Stack. Pointer >= Maxstack; } } bool Is. Stackempty { get { return Stack. Pointer <=0; } } public My. Stack() { Stack. Array = new T[Maxstack]; } public void Print() { for(int i = Stack. Pointer-1 ; i>=0; i--) Console. Write. Line(" Value: {0}", Stack. Array[i]); } }

class Program { static void Main() { My. Stack<int> Stack. Int = new My.

class Program { static void Main() { My. Stack<int> Stack. Int = new My. Stack<int>(); My. Stack<string> Stack. String = new My. Stack<string>(); Stack. Int. Push(3); Stack. Int. Push(63); Stack. Int. Push(73); Stack. Int. Push(34); Stack. Int. Push(30); Stack. Int. Print(); } } } Stack. String. Push("IN GIBS"); Stack. String. Push("learning C#"); Stack. String. Push("We MCA 3 Students"); Stack. String. Print();

using System; using System. Collections. Generic; using System. Linq; using System. Text; namespace Console.

using System; using System. Collections. Generic; using System. Linq; using System. Text; namespace Console. Application 2 { class simple { static public void Reverse. And. Print<T>(T[] arr) { Array. Reverse(arr); foreach (T item in arr) Console. Write. Line("{0}", item. To. String()); Console. Write. Line(" "); } }

class Program { static void Main() { var int. Array = new int[] {

class Program { static void Main() { var int. Array = new int[] { 1, 3, 2, 4, 9 }; var string. Array = new string[] { "first" , "second", "third" }; var doublearray = new double[] { 8. 78 , 8. 96, 2. 34 }; simple. Reverse. And. Print<int>(int. Array); simple. Reverse. And. Print<string>(string. Array); simple. Reverse. And. Print<double>(doublearray); } }

interface IMy. Ifc<T> { T Return. It(T invalue); } class Simple : IMy. Ifc<int>,

interface IMy. Ifc<T> { T Return. It(T invalue); } class Simple : IMy. Ifc<int>, IMy. Ifc<string> { public int Return. It(int invalue) { return invalue; } public string Return. It(string invalue) { return invalue; } } class Program { static void Main() { Simple trivial = new Simple(); Console. Write. Line("{0}", trivial. Return. It(5)); Console. Write. Line("{0}", trivial. Return. It("Hi there")); } }

Queries?

Queries?