471,338 Members | 1,156 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 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 3006
<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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Jethro Guo | last post: by
8 posts views Thread by Vivek | last post: by
1 post views Thread by Arthur Dent | last post: by
4 posts views Thread by Andrew Ducker | last post: by
4 posts views Thread by Hyun-jik Bae | last post: by
1 post views Thread by interX | last post: by

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.