# [.net] generics in c#

## Recommended Posts

supercoder74    154
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 on other sites
RipTorn    722
public class cclass<t,c> where c:Int32
{
...
}

##### Share on other sites
VizOne    598

@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 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 on other sites
Quote:
 Original post by static_matt13I 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 on other sites
supercoder74    154
Quote:
 class CClass { 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 on other sites
Rob Loach    1504
Of couse you can use a System.Object datatype for almost anything.

##### Share on other sites
joanusdmentia    1060
Quote:
 Original post by Rob LoachOf 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 on other sites
isn't that why you use the "is" command?

##### Share on other sites
joanusdmentia    1060
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)