Introduction
Generic collection is the most important concept in .NET, many of the programmers feel that Generic Collections are very complex, but after reading this article you will feel easy and comfortable to use these Generics and Generic collection.
In this article I have explained both Generic and Generic collection using simple samples, I hope this article will be useful in your daily life.
Problem with Array and ArrayList
Array
- Arrays are strongly typed (meaning that you can only put one type of object into it).
- Limited to size (Fixed length).
ArrayList
- ArrayList are strongly typed.
- Data can increase on need basis.
- It will do the boxing and unboxing while processing (decrease the performance).
List (Generic Collection)
- List are strongly typed.
- Data can increase on need basis.
- Don't incur overhead of being converted to and from type object.
A generic collection is strongly typed (you can store one type of objects into it) so that we can eliminate runtime type mismatches, it improves the performance by avoiding boxing and unboxing.
Generic
Generic is the key concept to develop Generic collection.
In the following example we have created function Compare which will accept only integer values to compare, it won’t accept the other types like string, float, etc.
NormalCheck obj = new NormalCheck();
int result = obj.Compare(2, 3);
class NormalCheck
{
public bool Compare(int a, int b)
{
if (a == b)
{
return true;
}
else
{
return false;
}
}
}
Generic Sample
Using Generic method we can define a function and it can accept all types of the object at runtime.
In the following example I am passing UnknowDataType to the class same passed in the Compare function to accept any datatype at runtime.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Generics
{
class Program
{
static void Main(string[] args)
{
// Compare Integer
Check < int > obj1 = new Check < int > ();
bool intResult = obj1.Compare(2, 3);
// Compare String
Check < string > obj2 = new Check < string > ();
bool strResult = obj2.Compare("Ramakrishna", "Ramakrishna");
Console.WriteLine("Integer Comparison: {0}\nString Comparison: {1}", intResult, strResult);
Console.Read();
}
// Generic class to accept all types of data types
class Check < UnknowDataType >
{
// Gerefic function to compare all data types
public bool Compare(UnknowDataType var1, UnknowDataType var2)
{
if (var1.Equals(var2))
{
return true;
}
else
{
return false;
}
}
}
}
}
Output



AMAN KUMARPosted Apr 5, 2023, 8:29 PM
Arraylist are not strongly typed
Md Mehdi ImamPosted May 17, 2022, 4:14 AM
Is arraylist strongly type?
devendra nikamPosted Aug 15, 2020, 12:50 AM
Very good! Thank you to share your knowledge.
Fida MarwatPosted Feb 22, 2020, 11:39 AM
Wow zbrdst sir G
Bhuwan PandeyPosted Apr 10, 2018, 1:57 AM
ArrayList are not strongly typed man.
manohar rayallaPosted Mar 8, 2017, 10:17 PM
Nice info...Good one...
SubashPosted Sep 15, 2016, 6:49 AM
Nice info sir thanks
Ramakrishna BasagallaPosted Apr 25, 2016, 11:57 AM
Thank you very much for all your comments
Rajeesh MenothPosted Dec 17, 2015, 12:02 AM
Good One
Banketeshvar NarayanPosted Dec 17, 2015, 12:01 AM
Good One