473,387 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Using Generic Classes as Type Parameters

class A<Ta>
{
public A() { }
}

class B<TA, Ta>
where TA : A<Ta>
{
public B() { }
}

class Program
{
static void Main(string[] args)
{
B<A<int>, int> var = new B<A<int>, int>();

//how I'd like to do it
//B<A<int> > = new B<A<int> >();
}
}

Is there some combination of syntax that'll let me declare a variable
of type B as I'd like to do it? It's a bit clunky having to say that I
want int twice.

Nov 23 '05 #1
3 2212
Levi,

No, there is not a way to do it. You will have to declare it like that
on the type level every time. Type parameters on the method level can be
implied, but at the type level (except when indicating the type in static
methods), they can not be.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Levi" <pi*******@bellsouth.net> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
class A<Ta>
{
public A() { }
}

class B<TA, Ta>
where TA : A<Ta>
{
public B() { }
}

class Program
{
static void Main(string[] args)
{
B<A<int>, int> var = new B<A<int>, int>();

//how I'd like to do it
//B<A<int> > = new B<A<int> >();
}
}

Is there some combination of syntax that'll let me declare a variable
of type B as I'd like to do it? It's a bit clunky having to say that I
want int twice.

Nov 23 '05 #2
Levi,

One other thing, you can always declare a type alias with the using
statement, and then use that. At the top of your file, you can do:

using MyGenericB = B<A<int>, int>;

Then, in your code, you can do:

MyGenericB var = new MyGenericB();

And it will compile fine.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Levi" <pi*******@bellsouth.net> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
class A<Ta>
{
public A() { }
}

class B<TA, Ta>
where TA : A<Ta>
{
public B() { }
}

class Program
{
static void Main(string[] args)
{
B<A<int>, int> var = new B<A<int>, int>();

//how I'd like to do it
//B<A<int> > = new B<A<int> >();
}
}

Is there some combination of syntax that'll let me declare a variable
of type B as I'd like to do it? It's a bit clunky having to say that I
want int twice.

Nov 23 '05 #3
I don't have the ability to test this here, but this should work:

B <A<Ta>, Ta> CreateB<Ta>()
{
return new B<A<Ta>, Ta>()
}

static void Main(string[] args)
{
B<A<int>, int> var = CreateB<int>();
}
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
"Levi" <pi*******@bellsouth.net> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
class A<Ta>
{
public A() { }
}

class B<TA, Ta>
where TA : A<Ta>
{
public B() { }
}

class Program
{
static void Main(string[] args)
{
B<A<int>, int> var = new B<A<int>, int>();

//how I'd like to do it
//B<A<int> > = new B<A<int> >();
}
}

Is there some combination of syntax that'll let me declare a variable
of type B as I'd like to do it? It's a bit clunky having to say that I
want int twice.

Nov 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Johnas Owan | last post by:
Hi! I'd like to build xml out of some c# classes with the XMLSerializer. My problem is: I have an envelope and want to fill it with different types of sub-nodes. But I don't like to build the...
3
by: Scottie_do | last post by:
I'm considering switching to C# and using VS2005, but I'd like to know if I can have a list of values at runtime and then specify (at runtime) what it's value type is. For example, I would have...
3
by: Tigger | last post by:
I have an object which could be compared to a DataTable/List which I am trying to genericify. I've spent about a day so far in refactoring and in the process gone through some hoops and hit some...
2
by: john | last post by:
Hi to All, Suppose I would like to implement a generic class with three type parameters: class UniversalClass<TA, TB, TC>() where TA: A, new() where TB: B, new() where TC: C, new()
7
by: Brad Wood | last post by:
The following works fine; passing a type to CreateInstance: private Type _next; BaseWizardForm nextForm = (BaseWizardForm)Activator.CreateInstance( _next ); So why doesn't this work (error =...
5
by: Torben Laursen | last post by:
I am writing a COM in C# using visual studio 2005 and VSTO. Inside the code I use some support classes that are generic but they are not used in the inferface of the COM. However I still get a...
1
by: Hoss | last post by:
Discovered this today after alot of head-scratching, just thought I would toss it out into cyberspace in hopes that it would help someone. This of course, will not compile public class...
2
by: yogb21 | last post by:
Hello ! I want to know...can I write a method inside C# class which has generic input parameters and generic return type. If yes..can someone help me with such method declaration and method usage....
1
by: raylopez99 | last post by:
Here is an example of a home grown generic class, representing a pair of values. Adapted from Jon Skeet's book "C# In Depth". The generic class is "sealed" for some reason (I think for...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.