HighTechTalks DotNet Forums  

Generics Question

Dotnet Framework (CLR) microsoft.public.dotnet.framework.clr


Discuss Generics Question in the Dotnet Framework (CLR) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Scott M.
 
Posts: n/a

Default Generics Question - 11-23-2007 , 12:28 PM






1. Although I get the relative simplicity of Generics over using interfaces
is it true that I *could* do what Generics offers with interfaces?



Reply With Quote
  #2  
Old   
Scott M.
 
Posts: n/a

Default Re: Generics Question - 11-23-2007 , 01:32 PM






Why not? If I use a paremeterized constructor that asks for a type that is
an interface, wouldn't that work?


"Mattias Sjögren" <mattias.dont.want.spam (AT) mvps (DOT) org> wrote

Quote:
1. Although I get the relative simplicity of Generics over using
interfaces
is it true that I *could* do what Generics offers with interfaces?

No, regular (non-generic) interfaces doesn't provide a way for you to
parameterize types.


Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.



Reply With Quote
  #3  
Old   
Jon Skeet [C# MVP]
 
Posts: n/a

Default Re: Generics Question - 11-23-2007 , 02:13 PM



Scott M. <smar (AT) nospam (DOT) nospam> wrote:
Quote:
Why not? If I use a paremeterized constructor that asks for a type that is
an interface, wouldn't that work?
How would you build an equivalent of List<T> using interfaces, such
that you get the same kind of compile-time type safety?

--
Jon Skeet - <skeet (AT) pobox (DOT) com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


Reply With Quote
  #4  
Old   
Scott M.
 
Posts: n/a

Default Re: Generics Question - 11-23-2007 , 02:28 PM



I don't know Jon, that's why I asked the question.


"Jon Skeet [C# MVP]" <skeet (AT) pobox (DOT) com> wrote

Quote:
Scott M. <smar (AT) nospam (DOT) nospam> wrote:
Why not? If I use a paremeterized constructor that asks for a type that
is
an interface, wouldn't that work?

How would you build an equivalent of List<T> using interfaces, such
that you get the same kind of compile-time type safety?

--
Jon Skeet - <skeet (AT) pobox (DOT) com
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk



Reply With Quote
  #5  
Old   
Jon Skeet [C# MVP]
 
Posts: n/a

Default Re: Generics Question - 11-23-2007 , 03:01 PM



Scott M. <smar (AT) nospam (DOT) nospam> wrote:
Quote:
I don't know Jon, that's why I asked the question.
Well, you asked it in a way that sounded like you were proposing a
solution. What would your parameterized constructor look like?

In short, to answer your original question: no, you couldn't do
everything that generics offers with interfaces, because interfaces
don't allow you to parameterize types or methods *by types*.

--
Jon Skeet - <skeet (AT) pobox (DOT) com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


Reply With Quote
  #6  
Old   
Scott M.
 
Posts: n/a

Default Re: Generics Question - 11-23-2007 , 04:31 PM



But that's what I don 't understand. Why can't I create a parameterized
constructor for a class and have its parameter be of an interface type?

Sub New(x As IEmployee)

Then this type would be essentially parameterized, would it not?



"Jon Skeet [C# MVP]" <skeet (AT) pobox (DOT) com> wrote

Quote:
Scott M. <smar (AT) nospam (DOT) nospam> wrote:
I don't know Jon, that's why I asked the question.

Well, you asked it in a way that sounded like you were proposing a
solution. What would your parameterized constructor look like?

In short, to answer your original question: no, you couldn't do
everything that generics offers with interfaces, because interfaces
don't allow you to parameterize types or methods *by types*.

--
Jon Skeet - <skeet (AT) pobox (DOT) com
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk



Reply With Quote
  #7  
Old   
Jon Skeet [C# MVP]
 
Posts: n/a

Default Re: Generics Question - 11-23-2007 , 04:38 PM



Scott M. <smar (AT) nospam (DOT) nospam> wrote:
Quote:
But that's what I don 't understand. Why can't I create a parameterized
constructor for a class and have its parameter be of an interface type?

Sub New(x As IEmployee)

Then this type would be essentially parameterized, would it not?
No, it wouldn't. It would be taking an IEmployee, but that doesn't make
the rest of the type parameterized *by type*.

Again, look at List<T>. Look at what it gives you in terms of strong
typing. Now try to construct the same sort of thing without generics.
Really, try it - I suspect that's the best way of seeing why generics
are important.

Now in many cases you don't *need* generics - it's very rare for it to
be worth creating your own generic type; generic methods are more
commonly useful (as new code), but most of the time you'll find
yourself *using* generic types rather than creating new ones.

--
Jon Skeet - <skeet (AT) pobox (DOT) com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


Reply With Quote
  #8  
Old   
Scott M.
 
Posts: n/a

Default Re: Generics Question - 11-23-2007 , 05:09 PM



I do understand the benefits of Generics: the type safety they birng and the
time that they save. My question is more for academic reasons.

If I expan on my last signature....

Class foo

Private emp As IEmployee

Sub New(x As IEmployee)
emp = x
End Sub

Private Sub New

End Sub

End Class

Now emp is acting like <T> would in a Gerneic type is it not? "emp" can be
used by any member of this type and with type-safety. I'm going to get type
safety when someone attempt to instance a "foo" (and passes something other
than an IEmployee) and I'm going to get type-safety (and intellisense) when
"emp" is used throughout the class.

Again, just to be clear, I fully understand how Generics makes this much
simpler and gives me the added benefit of its built-in type safety. But,
I'm wondering *if* I can accomplish the same results by passing Interfaces
with Is there some other benefit I'm missing?



"Jon Skeet [C# MVP]" <skeet (AT) pobox (DOT) com> wrote

Quote:
Scott M. <smar (AT) nospam (DOT) nospam> wrote:
But that's what I don 't understand. Why can't I create a parameterized
constructor for a class and have its parameter be of an interface type?

Sub New(x As IEmployee)

Then this type would be essentially parameterized, would it not?

No, it wouldn't. It would be taking an IEmployee, but that doesn't make
the rest of the type parameterized *by type*.

Again, look at List<T>. Look at what it gives you in terms of strong
typing. Now try to construct the same sort of thing without generics.
Really, try it - I suspect that's the best way of seeing why generics
are important.

Now in many cases you don't *need* generics - it's very rare for it to
be worth creating your own generic type; generic methods are more
commonly useful (as new code), but most of the time you'll find
yourself *using* generic types rather than creating new ones.

--
Jon Skeet - <skeet (AT) pobox (DOT) com
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk



Reply With Quote
  #9  
Old   
Jon Skeet [C# MVP]
 
Posts: n/a

Default Re: Generics Question - 11-23-2007 , 05:36 PM



Scott M. <smar (AT) nospam (DOT) nospam> wrote:
Quote:
I do understand the benefits of Generics: the type safety they birng and the
time that they save.
Right. Things that can't be done just by using interfaces.

Quote:
My question is more for academic reasons.

If I expan on my last signature....

Class foo

Private emp As IEmployee

Sub New(x As IEmployee)
emp = x
End Sub

Private Sub New

End Sub

End Class

Now emp is acting like <T> would in a Gerneic type is it not?
No. It's just acting as an interface implementation.

I'm sorry, I really don't see the overlap between interfaces and
generics here at all. Yes, you can use emp and it'll be strongly typed.
However:

1) It will be boxed if the implementation is a value type
2) You can *only* use it as an IEmployee unless you perform casting at
runtime (losing the safety).

For instance, you can't expose another member of the class with the
specific IEmployee implementation used by the constructor, because you
don't know that at compile-time. Basically, you have no information
other than the fact that it's *some* implementation of IEmployee.

There are plenty of times when that's enough, but that doesn't mean
that generics are either meant to be a replacement for interfaces *or*
that interfaces can do the same things as generics.

--
Jon Skeet - <skeet (AT) pobox (DOT) com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


Reply With Quote
  #10  
Old   
Scott M.
 
Posts: n/a

Default Re: Generics Question - 11-23-2007 , 06:27 PM



I think my question is mostly answered from this last reply. See inline...


"Jon Skeet [C# MVP]" <skeet (AT) pobox (DOT) com> wrote

Quote:
Scott M. <smar (AT) nospam (DOT) nospam> wrote:
I do understand the benefits of Generics: the type safety they birng and
the
time that they save.

Right. Things that can't be done just by using interfaces.
Again, I ask why? Interfaces may not bring the same level of type safety as
List(of T), but they do provide a type of saftey, in that a contraint is
being put on the input. I just don't buy that "it can't be done" using
Interfaces. It may be harder and it may require more safety's, but I
believe it can be done.

Quote:
My question is more for academic reasons.

If I expan on my last signature....

Class foo

Private emp As IEmployee

Sub New(x As IEmployee)
emp = x
End Sub

Private Sub New

End Sub

End Class

Now emp is acting like <T> would in a Gerneic type is it not?

No. It's just acting as an interface implementation.

I'm sorry, I really don't see the overlap between interfaces and
generics here at all.
I've been reading Professional .NET 2.0 Generics (wrox) and the author does
present interfaces as a pre-Generics way of solving some problems that
Generics solve (this example being one of them).

Quote:
Yes, you can use emp and it'll be strongly typed.
However:

1) It will be boxed if the implementation is a value type
2) You can *only* use it as an IEmployee unless you perform casting at
runtime (losing the safety).
What? Any class that implements IEmployee can be passed into the foo
constructor as it, no casting required. And no casting would be required to
use it as an IEmployee inside the class either.

Quote:
For instance, you can't expose another member of the class with the
specific IEmployee implementation used by the constructor, because you
don't know that at compile-time. Basically, you have no information
other than the fact that it's *some* implementation of IEmployee.
Sure. But if I'm only interested in the IEmployee members, this is not a
problem. My whole premis here is based on the assumption that IEmployee
contains the members I want to use. Rememer: I'm already sold on Generics,
I'm looking for a devil's advocate agument on the need for them.

Quote:
There are plenty of times when that's enough, but that doesn't mean
that generics are either meant to be a replacement for interfaces *or*
that interfaces can do the same things as generics.
I appreciate your replies.

-Scott

Quote:
--
Jon Skeet - <skeet (AT) pobox (DOT) com
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.