Generic type checking

Question :

Generic type checking,

Answer :

Is there a way to enforce/limit the types that are passed to primitives? (bool, int, string, etc.)

Now, I know you can limit the generic type parameter to a type or interface implementation via the where clause. However, this doesn’t fit the bill for primitives (AFAIK) because they do not all have a common ground (apart from object before someone says! :P).

So, my current thoughts are to just grit my teeth and do a big switch statement and throw an ArgumentException on failure.


Just to clarify:

The code definition should be like this:

public class MyClass ....  

And instantiation:

MyClass = new MyClass(); // Legal  MyClass = new MyClass(); // Legal  MyClass = new MyClass(); // Illegal  MyClass = new MyClass(); // Illegal (but looks awesome!)  


@Jon Limjap – Good point, and something I was already considering. I’m sure there is a generic method that can be used to determine if the type is of a value or reference type.

This could be useful in instantly removing a lot of the objects I don’t want to deal with (but then you need to worry about the structs that are used such as Size ). Interesting problem no? 🙂

Here it is:

where T: struct  

Taken from

I’m curious. Could this be done in .NET 3.x using extension methods? Create an interface, and implement the interface in the extension methods (which would probably be cleaner than a bit fat switch). Plus if you then need to later extend to any lightweight custom types, they can also implement the same interface, with no changes required to the base code.

What do you guys think?

The sad news is I am working in Framework 2!! 😀


This was so simple following on from So simple I almost want to cry, but it’s great because the code works like a charm!

So here is what I did (you’ll laugh!):

Code added to the generic class

bool TypeValid()  {      // Get the TypeCode from the Primitive Type      TypeCode code = Type.GetTypeCode(typeof(PrimitiveDataType));        // All of the TypeCode Enumeration refer Primitive Types      // with the exception of Object and Empty (Null).      // Since I am willing to allow Null Types (at this time)      // all we need to check for is Object!      switch (code)      {          case TypeCode.Object:              return false;          default:              return true;      }  }  

Then a little utility method to check the type and throw an exception,

private void EnforcePrimitiveType()  {      if (!TypeValid())          throw new InvalidOperationException(              "Unable to Instantiate SimpleMetadata based on the Generic Type of '" + typeof(PrimitiveDataType).Name +               "' - this Class is Designed to Work with Primitive Data Types Only.");  }  

All that then needs to be done is to call EnforcePrimitiveType() in the classes constructors. Job done! 🙂

The only downside, it only throws an exception at runtime (obviously) rather than design time. But that’s no big deal and could be picked up with utilities like (which we don’t use at work).

Special thanks to Jon Limjap on this one!


public class Class1 where GenericType : struct  {  }  

This one seemed to do the job..

That’s the answer Generic type checking, Hope this helps those looking for an answer. Then we suggest to do a search for the next question and find the answer only on our site.

Disclaimer :

The answers provided above are only to be used to guide the learning process. The questions above are open-ended questions, meaning that many answers are not fixed as above. I hope this article can be useful, Thank you

Read More  grep a file, but show several surrounding lines?