As is usually done, imagine you want to pass different types
of values to various methods of a class to primarily accomplish the same
purpose. You may be tempted to overloaded a method in various versions as
follows:

using System;
public class Generator
{
// Display the value of an integer
public void Show(int value)
{
Console.WriteLine(value);
}
// Display the value of a double-precesion value
public void Show(double value)
{
Console.WriteLine(value);
}
// Display the value of a character
public void Show(char value)
{
Console.WriteLine(value);
}
}

public class Exercise
{
static int Main()
{
var exo = new Generator();
// Call the version of the method that displays an integer
exo.Show(246);
// Call the version of the method that displays a character
exo.Show('G');
// Call the version of the method that displays a decimal
exo.Show(355.65);
return 0;
}
}

This would produce:

246
G
355.65
Press any key to continue . . .

We passed a constant value directly to the method when we
called it. Remember that you can also first declare a variable, assign it a
value, and then pass that variable to the method. Here are examples:

public class Exercise
{
static int Main()
{
var exo = new Generator();
// Call the version of the method that displays an integer
var Value1 = 246;
exo.Show(Value1);
// Call the version of the method that displays a character
var Value2 = 'G';
exo.Show(Value2);
// Call the version of the method that displays a decimal
var Value3 = 355.65;
exo.Show(Value3);
return 0;
}
}

Although this is based on the concept of method overloading,
another way you can solve this type of problem is to create one method that
doesn't know the type of value that would be passed to it but the method is
equipped to process the value appropriately. Based on the above program, you can
create one method that takes an argument and it displays its value. To do this,
at the time you are defining the method, you only let it know that it would
receive an argument but you don't specify the type of value that it will
process. Such a method is referred to as generic.

Practical
Learning: Introducing Generics

Start Microsoft Visual C# and create a new Console Application named
CommercialStore1

To create a new class, on the main menu, click Project -> Add Class...

A generic method is a method that can process a value whose
type is known only when the variable is accessed. To create a generic method, on
the right side of the name of the method, type the <> operator. Inside of this
operator, enter a letter or a name, which is referred to as parameter type. Here
is an example:

public class Generator
{
public void Show<TypeOfValue>()
{
}
}

One of the ways you can use the parameter type is to pass an
argument to the method. You do this by preceding the name of the argument with
the parametertype. Here is an example:

In the body of the method, you can process the argument as
you see fit. At a minimum, and based on our earlier program, you can simply
display the value by passing it to the Console.WriteLine() method. Here
is an example:

As mentioned earlier, one of the particularities of a
generic method is that, at the time it is defined, the method doesn't know the
type of the parameter. This means that, when calling the method, you must make
sure you clearly specify the type of value that will be processed. You can do
this by directly passing (a constant of) the type of value that the method will
process. Here are different examples of calling our Show() method:

using System;
public class Generator
{
public void Show<TypeOfValue>(TypeOfValue value)
{
Console.WriteLine(value);
}
}
public class Exercise
{
static int Main()
{
var exo = new Generator();
// Call the version of the function that displays an integer
var Value1 = 246;
exo.Show(Value1);
// Call the version of the function that displays a character
var Value2 = 'G';
exo.Show(Value2);
// Call the version of the function that displays a decimal
var Value3 = 355.65;
exo.Show(Value3);
return 0;
}
}

When complied and executed, this program would produce:

246
G
355.65
Press any key to continue . . .

As an alternative, you can type the name of the method,
followed by angle brackets. Inside of the brackets, enter the data type of the
value that will be processed. After the angle brackets, open the parentheses
and, inside of them, type the constant value that will be processed. Here are
examples:

using System;
public class Generator
{
public void Show<TypeOfValue>(TypeOfValue value)
{
Console.WriteLine(value);
}
}
public class Program
{
static int Main()
{
var exo = new Generator();
// Call the version of the function that displays an integer
var Value1 = 246;
exo.Show<int>(Value1);
// Call the version of the function that displays a character
var Value2 = 'G';
exo.Show<char>(Value2);
// Call the version of the function that displays a decimal
var Value3 = 355.65;
exo.Show<double>(Value3);
return 0;
}
}

You can also declare the value as a constant before passing
it to the method.

Practical
Learning: Calling a Generic Method

To specify the parameter type of a generic method when calling it,
change the Main() method as follows:

As seen above, you can pass different arguments to a method.
You can also pass different parameter types, in any appropriate order of your
choice, to a method. To pass two parameter types to a method, inside its <>
operator, enter the names of two parameter types separated by a comma. Here is
an example:

If you want to use the parameter types, you can pass an
argument for each to the method. Remember that each parameter type represents a
data type; so you can use it as the type of an argument. Here are examples:

Notice that the arguments can be of the same type or
different types. It is up to you to determine the type of a particular argument
when calling the method.

Generic Classes

Introduction

Like a method, a class can be created as a generic. When a
class is created as generic, it is asked to process a value wihtout knowing what
type that value is. This means that the class will known the type of value only
when it is called.

To create a generic class, on the right side of the name of
the class, type the <> operator and enter a name for the parameter type. Here is
an example:

public class Exercise<TypeOfValue>
{
}

This parameter type is just a representative of a data type.
As a data type, you can use it to declare a variable in the body of the class.
Here is an example:

public class Exercise<TypeOfValue>
{
public TypeOfValue value;
}

After declaring such a variable, you can use it in your
application. For example, you can access it outside of the class using the
period operator. Inside of the class, one way you can use the variable is to
display its value using one of the methods of the class. Here is an example:

After creating a generic class, you can use it. One way to
do this, as we have learned in previous lessons, consists of declaring a
variable for it. In previous lessons, to declare a variable of a class, we would
write:

Exercise exo = new Exercise();

If the class is generic, on the right side, type the <>
operator. Inside of this operator, enter the data type that will be processed as
the parameter type of the generic class. Here is an example:

You can also declare a variable of a generic type using the
var keyword. To do this, use var on the left side of the variable name, omit the
<> operator and the data type. This would be done as follows:

We saw that you could declare a variable of a parameter type
in the generic class. Another way you can use it is to pass it as an argument to
a method and make the argument a parameter type. As seen previously, you can use
the argument as you see fit. For example, you can display its value to the
console. Here is an example:

Besides, or as opposed to, passing a parameter type, you can
create a method that returns a parameter type. Once again, you can primarily
observe the rules we reviewed for returning a value from a method. Here is an
example:

You can create a property that is of the parameter type of
the generic class. There is no significant rule to follow when creating the
property, except that you should remember that, at the time you are creating the
property, the class doesn't know the type of the parameter. Here is an example:

As done for generic methods, when creating a generic class,
you can specify more than one parameter type. To do this, in the <> operator,
after the first generic type, enter a comma and another generic type. Here is an
example:

public class Generator<T, V>
{
}

If you know for sure that the parameters will be of the same
type, you can use one method to process both. Otherwise, you can declare the
necessary members for each type. You can also create a method that would take
many arguments with each argument of a particular type. Here are examples:

If a generic class has more than one parameter type, they
don't have to be of the same type. At the time you are creating the class, you
may not specify their types but you can anticipate that they would be different.
It is when you declare the variable that you would need to determine their
precise types.

Even if the parameters are of primitive types, you can first
declare the variables and pass them to the class.

A Class as a Parameter Type

So far, in our examples, we treated the parameter type as a
primitive data type. A parameter type can also be a formal class, either one
that you created yourself or one that exists as part of the C# language. When
creating the generic class, you must follow all the rules we have reviewed so
far for generic classess. Here is such a simple class:

As mentioned already, the class that would be processed by
the generic one must have been previously created so it can be used as a
parameter. When declaring a variable of the generic class, make sure you enter
the name of the normal class in place of the parameter type. Everything else is
as we have done so far. Here is an example:

In the same way, you can create a generic class that takes more than one
parameter

Generic Classes and Inheritance

Introduction

Consider the following geometric figures:

Square

Rectangle

Trapezoid

Parallelogram

Notice that these are geometric figures with each having
four sides. From what we know so far, we can create a base class to prepare it
for inheritance. If the class is very general, we can make it a generic one. We
can set a data type as an unknown type, anticipating that the dimensions of the
figure can be considered as integer or double-precision types. Here is an
example:

If you have a generic class that can serve as a foundation
for another class, you can derive one class from the generic one. To do this,
use the formula we apply when deriving a class but follow the name of each class
with <>. Inside of the <> operator, enter the same identifier to indicate that
the class is a generic type that is based on another generic class. Here is an
example:

public class Square<T> : Quadrilateral<T>
{
}

In the body of the new class, you can use the parameter type
as you see fit. For example, you can declare some member variables of that type.
You can create methods that return the parameter type or you can pass arguments
of the parameter type. When implementing the methods of the new class, use the
member variables of the parameter and the argument(s) based on the parameter
type as you see fit. You can then declare a variable of the class and use it as
we done so far for other generic classes. Here is an example:

Geometric Figure: Plate
Description: A quadrilateral is a geometric figure with four sides
A square is a quadrilateral with four equal sides
Side: 15
Press any key to continue . . .

Generic Classes and Interfaces

As done for a generic class, you can create a generic
interface that would serve as the base for generic classes. To proceed, when
creating the interface, follow its name with a <> declaration and, inside
of the <> operator, enter an identifier for the parameter type. Here is an
example:

You may be tempted to derive just any type of class from it.
One of the features of generics is that you can create a class that must
implement the functionality of a certain abstract class of your choice. For
example, when creating a generic class, you can oblige it to implement the
functionality of a certain interface or you can make sure that the class is
derived from a specific base class. This would make sure that the generic class
surely contains some useful functionality.

To create a constraint on a generic
class, after the <TypeName> operator, type where
TypeName : followed by the rule that the class must follow. For example, you
may want the generic class to implement the functionality of a pre-defined
class. You can create the generic class as follows:

After creating the class, you must implement the virtual
members of the where class/interface, using the rules of generic classes,
the way we have done it so far. When declaring a variable for the generic class,
in its <> operator, you must enter an object of the base class. Here is an
example: