[.net] generics in c#

Started by
8 comments, last by joanusdmentia 18 years, 8 months ago
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?
I program in my sleep,but when I sleep I use the partition in my head that doesnt have g++ or the .net library, so im kinda screwed.
Advertisement
public class cclass<t,c> where c:Int32
{
...
}
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
Andre Loker | Personal blog on .NET
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.
"God''s in his Heaven. All''s right with the World"
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!
I program in my sleep,but when I sleep I use the partition in my head that doesnt have g++ or the .net library, so im kinda screwed.
Of couse you can use a System.Object datatype for almost anything.
Rob Loach [Website] [Projects] [Contact]
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.
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
isn't that why you use the "is" command?
"God''s in his Heaven. All''s right with the World"
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)
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V

This topic is closed to new replies.

Advertisement