Generic Methods

Generic methods have type parameters. These parameters can be used in the method header or body. An open method has type parameters, which are nonspecific. A closed method has type arguments, where specific types are substituted for type parameters. For a generic method, the type parameters are listed after the function name. The type parameter list is enclosed in angle brackets. Each parameter is comma-delimited.

Here is a sample generic method:

using System;

namespace Donis.CSharpBook {

    public class Starter {
        public static void Main() {
            ZClass.MethodA<int>(20);
        }
    }

    public class ZClass{
        public static void MethodA<T>(T param) {
            Console.WriteLine(param.GetType().ToString());
        }
    }
}

When calling a generic method, insert the type arguments to replace type parameters. Inferring a type is an alternate syntax to calling a generic method, where the type arguments are inferred from the actual method parameters. The type argument then can be omitted with type inference. Basically, the generic method is called in the same manner as a non-generic method. The benefit is ease of use. However, type inference can disguise the true nature of the call, which could be relevant to someone maintaining or debugging an application.

In the previous code, ZClass.MethodA is a generic method. The following statement calls MethodA using type inference:

ZClass.MethodA(20);

Here is the syntax of a generic method, where both the parameters and return type can be generic:

attributes accessibility modifiers returntype identifier<type_parameterlist>
    (parameterlist) where type_parameter : constraintlist
    { method body }

The this Reference for Generic Types

Like all types, generic types have a this reference, which is a reference to the current object. The underlying type of the this reference is the same as the surrounding type. If the object is an XClass instance, the this reference is also of the XClass type. The this reference is used implicitly and explicitly to refer to instance members. The underlying type of the this reference to a generic type is the closed constructed type, which is defined at instantiation of the generic type.

The following code displays the type of a this reference. It is a this reference of a generic type, which is Donis.CSharpBook.ZClass`1[System.Int32]. From the output of the short application, you know that the closed constructed type has a single type parameter that is a 32-bit integer:

using System;

namespace Donis.CSharpBook {

    public class Starter {
        public static void Main() {
            ZClass<int> obj = new ZClass<int>();
            obj.MethodA();
        }
    }

    class ZClass<T> {
        public T MethodA() {
           T var = default(T);
           Console.WriteLine(this.GetType().ToString());
           return var;
        }
    }
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.44.182