Generic Class
Let us have a look at the Generic class in C#. Generic classes in C# have type parameters. The generic class introduces a type parameter. This becomes part of the class definition itself. The generic class of Type T is used in the following demo. The letter T denotes a type that is only known based on the calling location.
Open Visual Studio. Select the project type as console application.
Step 1
Click on File -> New -> Project.

Step 2
Select Visual C# from left the hand pane. Choose Console Application in the right side. Name the Project “Generic Class”. Define the desired location to save the project in your hard drive. Click OK.

Step 3
Write the following code in the application.
- public class Myclass<T>
- {
- public void Compareme(T v1, T v2)
- {
- if (v1.Equals(v2))
- {
- Console.Write("The value is matching");
- }
- else
- {
- Console.Write("The value is not matching");
- }
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Myclass<string> objmyint = new Myclass<string>();
- objmyint.Compareme("Amit","Amit");
- Console.ReadLine();
- }
- }
When we run the preceding code we get the following output.

Let us now change the string to something else and check the output. In this scenario I have made my second string parameter “amit”.
- public class Myclass<T>
- {
- public void Compareme(T v1, T v2)
- {
- if (v1.Equals(v2))
- {
- Console.Write("The value is matching");
- }
- else
- {
- Console.Write("The value is not matching");
- }
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Myclass<string> objmyint = new Myclass<string>();
- objmyint.Compareme("Amit","amit");
- Console.ReadLine();
- }
- }
Let us see the output now.


Tony YangPosted Nov 25, 2015, 2:38 AM
Nice article
Ankit BansalPosted Oct 1, 2015, 12:40 AM
Nice .. thanks for sharing..
Santhakumar MunuswamyPosted Sep 30, 2015, 3:43 PM
Good One
Shridhar SharmaPosted Sep 30, 2015, 12:33 PM
good one
Raja TPosted Sep 30, 2015, 9:37 AM
Nice, Thanks for sharing
Nilesh JadavPosted Sep 30, 2015, 9:19 AM
Thank you for the share
Shakti SaxenaPosted Sep 30, 2015, 5:41 AM
goodone
Humayun Kabir MamunPosted Sep 30, 2015, 4:52 AM
Nice...
Suresh BhartiPosted Sep 30, 2015, 3:57 AM
Nice share
Lalit RaghavPosted Sep 30, 2015, 2:59 AM
Nice Article
Sibeesh VenuPosted Sep 30, 2015, 2:24 AM
Nice Share
Sujeet SumanPosted Sep 30, 2015, 1:12 AM
Nice Article...................
Rajeesh MenothPosted Sep 30, 2015, 12:32 AM
Thanks For Sharing..
RakeshPosted Sep 30, 2015, 12:17 AM
Good Explain Gopal
Karthikeyan KPosted Sep 30, 2015, 12:12 AM
Good one...thanks for sharing
Harshad PansuriyaPosted Sep 29, 2015, 11:57 PM
Nice One
Mohammed IbrahimPosted Sep 29, 2015, 8:39 PM
nice
Pankaj Kumar ChoudharyPosted Sep 29, 2015, 7:54 PM
Nice Explain Gopal.........