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: public class Exercise<TypeOfValue> { public TypeOfValue value; public void Show() { Console.WriteLine("Value: {0}\n", value); } }
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: using System; public class Exercise<TypeOfValue> { public TypeOfValue value; public void Show() { Console.WriteLine("Value: {0}\n", value); } } public class Program { static int Main() { Exercise<int> exo = new Exercise<int>(); return 0; } } After declaring the variable, you can then access the member(s) of the class using the period operator. Here are examples: using System; public class Exercise<TypeOfValue> { public TypeOfValue value; public void Show() { Console.WriteLine("Value: {0}\n", value); } } public class Program { static int Main() { Exercise<int> exo = new Exercise<int>(); int iValue = 246; exo.value = iValue; exo.Show(); return 0; } }
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: using System; public class Exercise<TypeOfValue> { public void Show(TypeOfValue value) { Console.WriteLine("Value: {0}\n", value); } } public class Program { static int Main() { Exercise<int> exo = new Exercise<int>(); int iValue = 246; exo.Show(iValue); return 0; } } In the same way, you can pass the parameter type to a constructor of the class. Here is an example: public class Exercise<TypeOfValue> { private TypeOfValue val; public Exercise(TypeOfValue v) { val = v; } }
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: using System; public class Exercise<TypeOfValue> { private TypeOfValue val; public Exercise(TypeOfValue v) { val = v; } public TypeOfValue GetValue() { return val; } } public class Program { static int Main() { Exercise<double> exo = new Exercise<double>(35.65); Console.WriteLine("Value: {0}\n", exo.GetValue()); return 0; } }
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: using System; public class Exercise<TypeOfValue> { private TypeOfValue val; public TypeOfValue Value { get { return val; } set { val = value; } } public void Show() { Console.WriteLine("Value: {0}\n", val); } } public class Program { static int Main() { Exercise<int> exo = new Exercise<int>(); exo.Value = 246; exo.Show(); return 0; } } |
|
||
Previous | Copyright © 2006-2016, FunctionX, Inc. | Next |
|