Generic List Type Safety

c sharp
image credit:udemy.com

Generic List Type Safety
By creating generic classes, you can create a collection that is type-safe at compile-time.
Generic Collection Type Safety guarantees that no other irrelevant form of data can be input to generic collection.Generics is more artistic style in programming that not only throws away the type unsafe data, but also helps the developer to create his own type.
The use of Generic collection is excellent if we are type safe freaks. Whenever there is some type mismatch in the elements of the collection, the IDE immediately shows the error. So in many cases, we don’t need to run the whole program to check for the type mismatch errors .

Lets see how our List provides Generic List Type Safety by writing a program.

using System;
using System.Collections.Generic;

class Program
{
static void Main(string[] args)
{
List integersList = new List();

integersList.Add(7);
integersList.Add(8);
integersList.Add(6);

for (int i = 0; i < integersList.Count; i++) { Console.WriteLine("integersList: {0}", integersList[i]); } } }

Syed Adeel Ahmed
Syed Adeel Ahmed
Analyst, Programmer, Educationist and Blogger at Technofranchise
Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.

Published by

Syed Adeel Ahmed

Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.

Leave a Reply