Open and closed generics types

            1. What does open type mean?

            2. What does closed type mean?

            3. What does arity mean?

            4. How can we use type constructors?

A type without specified generic type parameters is called an open type. The C# compiler can compile code without defined type parameters. However it is unable to create instance of open types.

 

When type parameters are specified for a type with generic type parameters it is called a closed type and the CLR allows creating instances of closed types, if only they were specified completely.

             

class Program

{

        [Flags]

        internal enum CarOptions

        {

            SunRoof = 0x01,

            Spoiler = 0x02,

            FogLights = 0x04,

            TintedWindows = 0x08,

        }

 

        internal sealed class StringDictionary<TValue> : Dictionary<string, TValue>

        {

            static StringDictionary()

            {

                if (!typeof(TValue).IsEnum)

                    throw new ArgumentException("The type must be an enumerated");

            }

 

        }

 

        static void Main(string[] args)

        {

            object obj = CreateInstace(typeof(Dictionary<,>));

            obj = CreateInstace(typeof(StringDictionary<>));

            obj = CreateInstace(typeof(StringDictionary<Object>));

            obj = CreateInstace(typeof(StringDictionary<CarOptions>));

            Console.ReadKey();

        }

 

        private static Object CreateInstace(Type t)

        {

            Object result = null;

            try

            {

                result = Activator.CreateInstance(t);

 

                Trace.WriteLine(String.Format("Instance of {0} is created", t.ToString()));

            }

            catch(Exception ex)

            {

                Trace.WriteLine(ex.ToString());

            }

            return result;

        }

    }

Output:

            System.ArgumentException: Cannot create an instance of System.Collections.Generic.Dictionary`2[TKey,TValue] because Type.ContainsGenericParameters is true.

            System.ArgumentException: Cannot create an instance of OpenClosedTypes.Program+StringDictionary`1[TValue] because Type.ContainsGenericParameters is true.

            A first chance exception of type ‘System.ArgumentException’ occurred in OpenClosedTypes.exe

            A first chance exception of type ‘System.Reflection.TargetInvocationException’ occurred in mscorlib.dll

            System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. —> System.TypeInitializationException: The type initializer for ‘StringDictionary`1’ threw an exception. —> System.ArgumentException: The type must be enumerable

Instance of OpenClosedTypes.Program+StringDictionary`1 [OpenClosedTypes.Program+CarOptions] is created

The number of type parameters required by the type is called arity.

The C# has a feature is called constraints, which allows to constraint type parameters, but it has limitations, so we can use the type constructor to check type parameters before type will be created.

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