Generics and other members

1. Which members the C# compiler allows to define?

2. How to define generic events, fields, properties, indexers, constructors, and operators?

The C# compiler doesn’t allow defining generic events, fields, properties, indexers, constructors, and operators.

Example 1:

    internal class CommonType

    {

        delegate T CallbackMethod<T>(T param);

 

        //It’s not possible to define generic version of event:

        //event callbackMethod<T> testEvent;

        event CallbackMethod<T> testEvent;

 

        //It’s not possible to define constructors

        T _field;

 

        //It’s not possible to define constructors

        public CommonType(T param){ }

 

        public T Method<T>(T param)

        {

            return param;

        }

 

        /*

            public class Foo {

              public T Thing<T> {

                get { return field; }

                set { field = value; }

              }

            }

 

           

            Foo f = new Foo();

            f.Thing<int> = 7;

            f.Thing<string> = "Hello, Crash";  

        

         * First a Foo object has to be allocated on the heap. No problem, right?

         * Well, unfortunately, yes. How big is a Foo? It has a generic property.

         * We must reserve space for the backing store for the generic property (it’s part of the Foo class). But we don’t know how much to reserve. Even if the compiler had read and understood every possible use of the generic Thing property and worked out how many instantiations there were, even in the code we will write ten years in the future, we could still use reflection to bind a type to the property at run-time.

         */

 

        //It’s not possible to define properties and indexers

        public T this[int index]

        {

            get { return _field; }

        }

 

        public T Property

        {

            set

            {

                _field = value;

            }

            get

            {

                return _field;

            }

        }

    }

To make defining generic events, fields, properties, indexers, constructors, and operators possible we can define a generic type.

Example 2:

       

    internal class GenericType<T>

    {

        delegate T callbackMethod<T>(T param);

 

        //It’s not possible to define generic version of event:

        //event callbackMethod<T> testEvent;

        event callbackMethod<T> testEvent;

 

        T _field;

 

        public GenericType(T param)

        {

 

        }

 

        public T this[T index]

        {

            get { return _field; }

        }

 

        public T Property

        {

            set

            {

                _field = value;

            }

            get

            {

                return _field;

            }

        }

 

        ~GenericType()

        {

 

        }

 

        public static GenericType<T> operator +(GenericType<T> p1, GenericType<T> p2)

        {

            return p1 + p2;

        }

    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s