O'Reilly logo

C# 7 and .NET Core Cookbook by Dirk Strauss

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

  1. Declaring a generic class is actually very easy. All that we need to do is create the class with the generic type parameter <T>:
        public class PerformAction<T>         {         }
The generic type parameter is basically a placeholder for a specific type that will need to be defined when the class of variable is instantiated. This means that the generic class PerformAction<T> can never just be used without specifying the type argument inside angle brackets when instantiating the class.
  1. Next, create a private variable of the generic type parameter T. This will hold the value we pass to the generic class:
        public class PerformAction<T>         {           private T _value;         }
  1. We now need to add a constructor to the generic class. The constructor will ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required