Sign in to follow this  
supercoder74

[.net] generics in c#

Recommended Posts

hello, lets say I want to make a templated class, taking a typename, then an int. something like this:
template<typename t,int c>
class cclass
{
   t arr[c];
   cclass(){/*...*/}
   //some other functions...
}

I have read up about c# generics, and to my understanding you can only take typenames, but no other types. Is this correct?

Share this post


Link to post
Share on other sites
RipTorn, your example won't work.

@supercoder74: that's correct, you can only have typenames as generic parameters. Given that .NET Arrays are not inlined into the memory layout of classes (unlike C++-stackarrays) there would not be any benefit if you could provide a generic int parameter. Just do this:


class CClass<T> {
T[] arr;

public CClass(int arraySize) {
arr = new T[arraySize];
}
}


(Sidenote: there are arrays that can be inlined, but only for unsafe structs:

unsafe struct X {
public fixed char foo[128];
}

Here it would be nice to allow a generic definition of the size of foo. This is, however, not possible :(
)


Regards,
Andre

Share this post


Link to post
Share on other sites
I don't believe there is templates in C#, but I could be wrong.

I think if you want to have Generics in C#, have your classes handle user data as type "object". If you check the .NET Library, you'll notice almost everything is handled as "object". All of the collection classes cast whatever you throw into them as type "object" and when you pull them out you have to re-cast them.

Share this post


Link to post
Share on other sites
Quote:
Original post by static_matt13
I don't believe there is templates in C#, but I could be wrong.

I think if you want to have Generics in C#, have your classes handle user data as type "object". If you check the .NET Library, you'll notice almost everything is handled as "object". All of the collection classes cast whatever you throw into them as type "object" and when you pull them out you have to re-cast them.


That's only with yucky 1.1 (which I have to use [sad]).

Share this post


Link to post
Share on other sites
Quote:

class CClass<T> {
T[] arr;

public CClass(int arraySize) {
arr = new T[arraySize];
}
}

oh yeah, I forgot about that arrays arent on the stack. Thanks for the info!

Share this post


Link to post
Share on other sites
Quote:
Original post by Rob Loach
Of couse you can use a System.Object datatype for almost anything.


So long as you don't mind the evil that is boxing and having to validate (or hope) that the type you're expecting is actually in the array.

Share this post


Link to post
Share on other sites
It is, but you have to remember to use it [smile] If you don't your application could come crashing down with a type conversion exception. There's also a small performance hit involved (although odds are that won't really matter)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this