template<typename t,int c>
class cclass
{
t arr[c];
cclass(){/*...*/}
//some other functions...
}
[.net] generics in c#
hello, lets say I want to make a templated class, taking a typename, then an int. something like this:
I have read up about c# generics, and to my understanding you can only take typenames, but no other types. Is this correct?
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:
(Sidenote: there are arrays that can be inlined, but only for unsafe structs:
Here it would be nice to allow a generic definition of the size of foo. This is, however, not possible :(
)
Regards,
Andre
@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
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.
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.
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]).
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!
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.
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)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement