In 2005, with C# and .NET Framework 2.0, Microsoft introduced a feature named generics that enables your types to be more reusable, by allowing a programmer to pass types as parameters similar to how you can pass objects as parameters.
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 namedData
of theobject
typeThing
has a method namedProcess
that accepts an input parameter of thestring
type, and returns astring
value
Note
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)...