Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Why do we need Generics in Object Oriented Programming explained through C# program?

    • 0
    • 7
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 258
    Comment on it
    using System;
    using System.Collections;
    
    namespace DrawbacksOfNonGeneric
    {
    	public class Person {
    		
    		//Properties
    		public string Name { get; set;}
    		public int Age { get; set;}
    		//Constructors
    		public Person(){}
    		public Person(string name, int age){
    			Name = name;
    			Age = age;
    		}
    	}	
    
    	public class PersonCollection : IEnumerable{
    		private ArrayList personList = new ArrayList();
    
    		public Person GetPerson(int pos ){
    			return(Person)personList [pos];
    		}
    		public void SetPerson(Person p){
    			personList.Add (p);
    		}
    		public void ClearPersonList(){
    			personList.Clear ();
    		}
    		IEnumerator IEnumerable.GetEnumerator(){
    			return personList.GetEnumerator();
    		}
    
    	}
    	class MainClass{
    		public static void Main (string[] args){
    			//ISSUE NUMBER 1 : BOXING and UNBOXING...
    			ArrayList list = new ArrayList ();//ArrayList can hold any type of data as all types are Object by default  .
    			list.Add(10);// This is autoboxing ..This call is equivalen to list.Add(new Int32(10));
    			list.Add(31);// This is autoboxing ..This call is equivalen to list.Add(new Int32(10));
    			list.Add(1);// This is autoboxing ..This call is equivalen to list.Add(new Int32(10));
    			list.Add(20);// This is autoboxing ..This call is equivalen to list.Add(new Int32(10));
    
    			/* 
    			   Explicit Boxing in C#...Object o = (Object)10;
    			   This Explicit Boxing operation has a performance issue  because when ever boxing occurs the value type (initially stored on stack)
    			   is transferred onto the Heap and it is transfered back to stack when we get the value from object type.
    			*/
    
    			//ISSUE NUMBER 2 : CASTING VALUE BACK TO SPECIFIC TYPE...
    			int i = (int)list[0];//UnBoxing: This is required as non-generic type collections such as ArrayList can hold allvalues of 'Object' type
    			int j = (int)list[1];
    			Console.WriteLine (i + j);	
    			// ISSUE NUMBER 3 : If you want to make a collection which could contain just one type then, prior to generics you could do it like this...
    			PersonCollection pc = new PersonCollection();
    			pc.SetPerson (new Person ("Bipin", 20));
    			pc.SetPerson (new Person ("XYZ", 10));
    			pc.SetPerson (new Person ("Abc", 30));
    //			pc.SetPerson (10);// Setting any other type in SetPerson is an Compile Time error
    			foreach (Person p in pc) {
    				Console.WriteLine ("{0} is {1} years old", p.Name, p.Age);
    			}
    		}
    	}
    }
    

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: