473,231 Members | 1,659 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,231 software developers and data experts.

Different generic type parameter in generic class constructorparameter

Is it possible to pass a generic parameter of the same class to to its
constructor, where the "T" type passed in the constructor is different
than the "T" type of the instanced class?

ie,

public class SomeList<T>
{

public SomeList(SomeList<TthisSomeList)
{
//another SomeList<Twhere the T is different type
}
}

This may reveal my relatively new understanding of class generics, but
I didn't find a similar post.

TIA
Terry
Jun 27 '08 #1
9 3116
<ta*****@yahoo.comwrote:
Is it possible to pass a generic parameter of the same class to to its
constructor, where the "T" type passed in the constructor is different
than the "T" type of the instanced class?
No, and you can't write generic constructors either. However, you *can*
write a generic static method to return the new list:

public class SomeList<T>
{
public static SomeList<TCreateList<V(SomeList<VotherList)
{
...
}
}

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #2
On Sun, 25 May 2008 00:18:23 -0700, Jon Skeet [C# MVP] <sk***@pobox.com>
wrote:
<ta*****@yahoo.comwrote:
>Is it possible to pass a generic parameter of the same class to to its
constructor, where the "T" type passed in the constructor is different
than the "T" type of the instanced class?

No, and you can't write generic constructors either.
Which of course begs the question: other than the fact that the C# spec
doesn't include a "type-parameter-list" as part of a valid constructor
declaration, why aren't generic constructors allowed? Is there something
about the way a generic class is compiled that precludes allowing that?

Pete
Jun 27 '08 #3
Which of course begs the question...
why aren't generic constructors allowed?
Well, the calling syntax alone would be interesting... you'd need:

new SomeClass<TClassArg1 , ... , TClassArgN><TCtorArg1 , ... ,
TCtorArgN>
And it would preclude both " : new()" (constraint-based) and
Activator.CreateInstance() etc.
I suspect that it might help to see a sensible use-case where it can't
already be solved (as Jon said) with a static generic Create<...>
method; (genuine question) can you think of any such examples?

Marc
Jun 27 '08 #4
Marc Gravell <ma**********@gmail.comwrote:
Which of course begs the question...
why aren't generic constructors allowed?

Well, the calling syntax alone would be interesting... you'd need:

new SomeClass<TClassArg1 , ... , TClassArgN><TCtorArg1 , ... ,
TCtorArgN>
And it would preclude both " : new()" (constraint-based) and
Activator.CreateInstance() etc.
I suspect that it might help to see a sensible use-case where it can't
already be solved (as Jon said) with a static generic Create<...>
method; (genuine question) can you think of any such examples?
Chaining a constructor call from a derived class to a base class would
present a problem. Just occasionally I've found this a nuisance, but
not enough to make me feel that its omission was a mistake.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #5
On May 25, 3:18*am, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
No, and you can't write generic constructors either. However, you *can*
write a generic static method to return the new list:

public class SomeList<T>
{
* * public static SomeList<TCreateList<V(SomeList<VotherList)
* * {
* * * * *...
* * }

}

--
Jon Skeet - <sk...@pobox.com>
Web site:http://www.pobox.com/~skeet*
Blog:http://www.msmvps.com/jon.skeet
C# in Depth:http://csharpindepth.com
Thanks for the suggestion; that static function I think will work
nicely in what I'm trying to do, which is mapping class properties to
a table definition, and then create a collection of that class with
the table data.

Where a generic constructor could be useful is recursion; in my
situation, it would have been nice to recursively create generic list
objects, where one of the classes has a SomeList<Tproperty of it's
own.

And this generic constructor would have been a 2nd overloaded method.
What if the compiler were to allow generic constructors, given that a
default ctor was explicitly defined, and a restriction on how many
generic type parameters could be used? That could allow recursion.

Thanks again for the help,
Terry
Jun 27 '08 #6
On May 25, 3:18*am, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
No, and you can't write generic constructors either. However, you *can*
write a generic static method to return the new list:

public class SomeList<T>
{
* * public static SomeList<TCreateList<V(SomeList<VotherList)
* * {
* * * * *...
* * }

}

--
Jon Skeet - <sk...@pobox.com>
Web site:http://www.pobox.com/~skeet*
Blog:http://www.msmvps.com/jon.skeet
C# in Depth:http://csharpindepth.com
Thanks for the suggestion; that static function I think will work
nicely in what I'm trying to do, which is mapping class properties to
a table definition, and then create a collection of that class with
the table data.

Where a generic constructor would have been nice, would have been to
recursively create generic list
objects, where one of the classes has a SomeList<Tproperty of it's
own, although I hadn't gotten that far.

But this generic constructor would have been a 2nd overloaded method.
What if the compiler were to allow generic constructors, given that a
default ctor was explicitly defined, and a restriction on how many
generic type parameters could be used? That could allow recursion
with different types.

Thanks again for the help,
Terry
Jun 27 '08 #7
On May 25, 12:12*pm, Marc Gravell <marc.grav...@gmail.comwrote:
Which of course begs the question...
why aren't generic constructors allowed?

Well, the calling syntax alone would be interesting... you'd need:

new SomeClass<TClassArg1 , ... , TClassArgN><TCtorArg1 , ... ,
TCtorArgN>
Not really; they could just introduce the same restriction that C++
constructor templates have - that types of all arguments in the call
must be deducible and cannot be specified explicitly.
And it would preclude both " : new()" (constraint-based) and
Activator.CreateInstance() etc.
Why?
I suspect that it might help to see a sensible use-case where it can't
already be solved (as Jon said) with a static generic Create<...>
method; (genuine question) can you think of any such examples?
It is a standard .NET pattern for a collection to have a constructor
taking an enumerable. The desire to further extend it to allow passing
collections of compatible item types only makes sense. Yes, it can be
done with a factory method, but a better question is, why workaround
when the language could provide a more straightforward way to handle
this without any downsides?
Jun 27 '08 #8
On Sun, 25 May 2008 01:12:50 -0700, Marc Gravell <ma**********@gmail.com>
wrote:
>Which of course begs the question...
why aren't generic constructors allowed?

Well, the calling syntax alone would be interesting... you'd need:

new SomeClass<TClassArg1 , ... , TClassArgN><TCtorArg1 , ... , TCtorArgN>
I'm not sure why that'd be a problem, but if it bothers someone too much,
I'd be happy to (as Pavel notes) limit use of generic constructors to
those cases where the type can be inferred, as is possible with other
generic methods now.
And it would preclude both " : new()" (constraint-based) and
Activator.CreateInstance() etc.
The latter I don't know enough about and am happy to take your word for
it, but I don't see why allowing constraints on the type parameter for a
generic constructor would be an issue.
I suspect that it might help to see a sensible use-case where it can't
already be solved (as Jon said) with a static generic Create<...>
method; (genuine question) can you think of any such examples?
Well, I think Jon's workaround is "fine" so far as it goes. It's just a
little awkward and I still don't see a clear reason why generic
constructors wouldn't be allowed.

Pete
Jun 27 '08 #9
Why?
OK, I guess that in *some* cases the overloaded form of
Activator.CreateInstance could try to do some inference, but there are
big problems when there is no preferred form - but this isn't anything
new... so fair enough ;-p
It is a standard .NET pattern for a collection to have a constructor
taking an enumerable.
When that enumerable is a different type, another option would be to
translate the tye *before* passing it to the ctor; with LINQ this is a
single call to the Cast or OfType extension methods. This doesn't
necessitate another factory method, so seems quite appealling - and it
would be trivial to reproduce in .NET 2.0 (via an iterator block) if
needed - or indeed, the ConvertAll method if using List<T[both shown
below].

Marc

using System.Collections.Generic;
using System.Linq;
class A { }
class B : A { }
static class Program
{
static void Main()
{
List<BbList = new List<B{ new B(), new B(), new B() };

List<AaList1 = bList.ConvertAll(b =(A)b);
List<AaList2 = new List<A>(bList.Cast<A>());
}
}
Jun 27 '08 #10

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

Similar topics

4
by: Jethro Guo | last post by:
C++ template use constraint by signature,It's very flexible to programmer but complex for complier, and at most time programmer can not get clear error message from complier if error occur. C#...
8
by: Vivek | last post by:
Hi, I wish to update the LIST<T> created in PARENT FORM from the CHILD FORM. Currently I have declared the LIST<Role> as public in my parent form. What can I do to update the <LIST>? Thanks
1
by: Arthur Dent | last post by:
Hi all... Heres what im looking to do.... I have a Generic class i wrote. Now, on another class, i want to add a method which can take in an object of my generic class... but the catch is, i want...
4
by: Andrew Ducker | last post by:
I have a collection of classes descending from a single root class (let's call it RootClass). They all currently have a property of Logical, of type Logical. However they actually return a...
4
by: Hyun-jik Bae | last post by:
Is that not allowed to assume generic type numeric type? As far as I've tried, I got an error with the following code: public class AAA<T> { public int Foo(T a) { return a; // error: Cannot...
9
by: mps | last post by:
I want to define a class that has a generic parameter that is itself a generic class. For example, if I have a generic IQueue<Tinterface, and class A wants to make use of a generic class that...
1
by: interX | last post by:
Hi I'm new in VC++ and have a question to generics. I have a generic class, which contains an array of the generic type. This array I can pin and then I would like to get an unmanaged pointer to...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
1
by: DotNetNewbie | last post by:
Hi, In the source code for System.Web.MVC the generic class is defined as: public class ViewPage<TViewData: ViewPage I want to create my own custom ViewPage class, and inherit from the...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.