![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
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. |
#3
| |||
| |||
|
|
Why not? If I use a paremeterized constructor that asks for a type that is an interface, wouldn't that work? |
#4
| |||
| |||
|
|
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 |
#5
| |||
| |||
|
|
I don't know Jon, that's why I asked the question. |
#6
| |||
| |||
|
|
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 |
#7
| |||
| |||
|
|
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? |
#8
| |||
| |||
|
|
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 |
#9
| |||
| |||
|
|
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? |
#10
| ||||||
| ||||||
|
|
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. |
|
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 |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |