| 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.
Give project a name then click on Next then on Create.
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.
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.
The function runs successfully and returns the result as we had provided matching strings.
Here, we have tried with non-matching strings using the same object of type string.
As the two strings were not matching, we have got the following output.
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.
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.