Example:
using System;
using System.Collections.Generic;
class GenericDemo
{
public static void Main()
{
List
//Add elements to generic integer list
//Correct entry as per generic
lstInteger.Add(1);
lstInteger.Add(2);
lstInteger.Add(3);
lstInteger.Add(4);
lstInteger.Add(5);
//Code exception due to following wrong entry
lstInteger.Add(‘Kuldeep’);
}
}
Because of type-safety principle, in above example it will throw error at last lstInteger.Add(‘Kuldeep’) statement. Current declaration for generic is an integer and at last statement you'r trying to insert is a string type. So it will raise exception.
No comments:
Post a Comment