Making a generic type

First, let's see an example of a non-generic type, so that you can understand the problem that generics is designed to solve.

In the PacktLibrary project, add a new class named Thing, as shown in the following code, and note the following:

  • Thing has a field named Data of the object type
  • Thing has a method named Process that accepts an input parameter of the string type, and returns a string value

If we wanted the Thing type to be flexible in .NET Framework 1.0, we would have to use the object type for the field.

using System;namespace Packt.CS7{   public class Thing   {      public object Data = default(object);      public string Process(string input)      {         if (Data == input)         {            return Data.ToString() + Data.ToString();         }         else         {

Get C# 7.1 and .NET Core 2.0 – Modern Cross-Platform Development - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.