Generic Class in C#

| Dua Ali

In this blog, a tutorial is demonstrated to learn that what are Generic classes in C#? Generic classes in C# have type parameters. This becomes part of the class definition itself. In this demo, a generic class of type T is used for the parameters. The letter T denotes a type that is only known based on the calling of the method.

To implement an example for better understanding, Open Visual Studio. Select the project type as console application.

GenericClassCSharp

Give project a name then click on Next then on Create.

GenericClassCSharp

Here, a simple class is created named ‘DemoClass’ in which we can see that the parameters are not any specific data type, instead a generic type T is used which allows you to create the object of this class with any data type.

GenericClassCSharp

In the Main class, we have created an object of our previous class with string data type. Using this object, a method is called by passing two strings in the parameters.

GenericClassCSharp

The function runs successfully and returns the result as we had provided matching strings.

GenericClassCSharp

Here, we have tried with non-matching strings using the same object of type string.

GenericClassCSharp

As the two strings were not matching, we have got the following output.

GenericClassCSharp

The generic class allows us to create object of different types using the same class. Here we have created an object using integer data type with same class. The same method worked right with integer type parameters as shown in the console window.

GenericClassCSharp

Join us next time, as we continue our journey of learning canvas apps.Click here to learn more about Imperium's Power Apps Services. We hope this information was useful, and we look forward to sharing more insights into the Power Platform world.


M
Chief Architect, Founder, and CEO - a Microsoft recognized Power Platform solution architect.

About The Blog

Stay updated with what is happening in the Microsoft Business Applications world and initiatives Imperium is taking to ease digital transformation for customers.


More About Us

We provide guidance and strategic oversight to C-Suite and IT Directors for on-going implementations. Feel free to give us a call.

1 331 250 27 17
Send A Message

Ready to Start?

Get a personalized consultation for your project.

Book a Meeting