.net generic class or function parameters.

I don't understand why is this not possible to pass an integer parameter to a generic class or function as for c++ templates. It seems to me that it's far easier than allowing type parameter resolved at runtime.
Could someone please explain me if there is some practical reason for this and if there's a workarround for doing the same as templates but at runtime with integer parameters.

thanks,
Aurelien.


Answer this question

.net generic class or function parameters.

  • subsonic

    The reason C++ doesn't allow non-type generic parameters is simply because it's not a feature supported by the Common Language Runtime.  My understanding is that generics were originally designed to facilitate writing type-safe collections, and you generally don't need non-type parameters in order to do that.  Someone on the CLR forum might be able to give more details:

    http://forums.microsoft.com/MSDN/ShowForum.aspx ForumID=44&SiteID=1

    Your best workarounds are to either pass the value as a function parameter, or use templates instead of generics.

    Thanks,
    Mitch



  • .net generic class or function parameters.