Skip navigation

Using parameterized types!

I got the idea to do this from the way the Content loader works in XNA. It seems awfully verbose to have to write the Type name so many times in the first way.. (way#1 below)

With “my way”, you don’t need to cast and you only specify the type once, <between angle brackets>.

This supports the idea that you work like the framework you work in. That is, you are inclined to provide “services” to yourself like the services your framework provides to you. Since the use of a parameterized content loader is SO useful, a parameterized enum naturally follows.

using System;

public class Enums<T>
  public static T Parse( string val )
    return (T)Enum.Parse( typeof( T ), val );

enum People

class Program

  static void Main()
    // The "Normal" way.  Verbose.
    People po = (People)Enum.Parse( typeof( People ), "Athlete" );
    Console.WriteLine( po + " as int " + (int)po + " type " + po.GetType() );

    // my new way, the Enums class is defined above.
    People po2 = Enums.Parse<People>( "Athlete" );
    Console.WriteLine( po2 + " as int " + (int)po2 + " type " + po2.GetType() );


Leave a Reply

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

You are commenting using your 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: