> OK so you are saying the elements in the list are subtypes of > some type... say IEntity?
yes, but you also have to process lists inside these entities, thus also lists of IEntity. > And thus the > "// yadda yadda" > > Is something like; > > Foreach(IEntity orderOrProduct in list) > { > // yadda yadda on orderOrProduct > } > > Is thus the problem that .net doesn't realise that (in this > example, i.e. > assuming the type parameters are not in parameters on any > method of IEntity, i.e contravaraint parameters I think).... > > ISomething<IProduct> is a subtype of ISomething<IEntity> ? > > Thus you should be able to simply declare the parameter as > > "private void ProcessISomething(ISomething<IEntity> toProcess)" > > And dot net accept ISomething<IProduct> as a subtype of > ISomething<IEntity> (given it obeys rules of contra/covariance). no, that's not going to work. C#/VB.NET don't support co/contra-variance. So you've to pass in an IList toProcess. > Rather than > > "private void ProcessISomething<X>(ISomething<X > toProcess) > Where X : IEntity" > > And thus end up constantly parametising methods that really > shouldn't need parametising? correct. > Or am I off on a tangent? No you're right. Methods in interfaces with a where restriction are more strict than the where clause of a method in a class, they're less flexible, so that's also something to take into account. > Your way out of this is to remove the parametisation of the interface? Yes. I've for example a collection class, CollectionCore<T> which is the base class for my EntityCollection<T> class. (I use my own collection class because I need fine-grained control when a method is called etc.). I also implement IEntityCollection on EntityCollection<T>, so I can use it in code which doesn't know the T type, for example in code which obtains the lists of related entities inside an entity instance (e.g. a list returned by Customer with Customer.Orders, Customer.Employees etc.) The fun part is that you still can write generic code, even without casting, but you don't need generic types. FB > > -----Original Message----- > From: Discussion of advanced .NET topics. > [mailto:[EMAIL PROTECTED] On Behalf Of Frans Bouma > Sent: 18 October 2006 11:47 > To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM > Subject: Re: [ADVANCED-DOTNET] Type declarations in ide.... > > > Not completely with this. > > > > This bit..... > > > > foreach(IList list in containingLists) > > { > > // yadda yadda > > } > > > > What sort of things are you doing in the "yadda yadda" bit..... > > If you don't know the type of the elements of list you can't access > > them. > > > > If you do know the type of the elements then, the type is known. > > processing the items in the list. > > Let me give you a real world example: your code > receives a collection of entity classes (which implement an > interface) and you've to sort the complete graph (so > containing entities inside these entities as well) using > topology sort. > > It's unknown if the collection inside the first > entity for example contains Order entities or products, you > also shouldn't care. > > What's great is that if the entities implement an > interface, say IEntity, and the collections implement an > interface, say IEntityCollection, (or IList of you want to), > you can solve this with easy to read code. > > If you don't have the interfaces, you will have to > deal with the generics somewhere and it gets so incredibly > complex, it's not fun anymore. The where clauses are > impractical to work with, especially with generic interfaces > mixed with generic classes. > > > P.S. > > > > I tend to do this with most techniques that are new to me, I abuse > > them to the point where they become impractical.....and then I know > > their limits....I'm just taking generics to an extreme in > order to get > > a feel for their practical limits/application. > > Ok, well I had that plan once as well :) when I > ported my .NET 1.x o/r core to .NET 2.0 and thought that with > one new core using generics all over the place, it would be > simpler, easier to read and more compact. > > That was a mistake. :). The code became so utterly > complex, that I started over, using non-generic interfaces (I > Used generic interfaces in the first attempt) implemented by > generic classes. The end result is readable code and uses > generics where appropriate and doesn't when it's not needed. > > IMHO new features like generics are far overrated > when you compare it to readability and simplicity. Code > should be so simple to read it's simply too boring to even be > considered 'cool'. If someone asks you: "Hey man, show me > some really awesome code you wrote lately", and you look at > your code and with everything you think: > "this is too boring to show", you're at the right track. > > > FB > > > > > -----Original Message----- > > From: Discussion of advanced .NET topics. > > [mailto:[EMAIL PROTECTED] On Behalf Of > Frans Bouma > > Sent: 18 October 2006 10:30 > > To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM > > Subject: Re: [ADVANCED-DOTNET] Type declarations in ide.... > > > > > You'll have to explain with an example.....it's early in > > the morning > > > and my brain isn't up to it. > > > > public class Foo : ISomething > > { > > private List<Bar> _bars; > > > > //... ctor, creation etc. > > > > List<Object> ISomething.GetContainingLists() > > { > > List<Object> toReturn = new List<Object>(); > > toReturn.Add(_bars); > > return toReturn; > > } > > } > > > > > > Now, in some other code, an ISomething instance is passed in. > > This can be a Foo instance, but it can also be a FooToo instance: > > > > public class FooToo : ISomething > > { > > private List<BarToo> _bars; > > > > //... ctor, creation etc. > > > > List<Object> ISomething.GetContainingLists() > > { > > List<Object> toReturn = new List<Object>(); > > toReturn.Add(_bars); > > return toReturn; > > } > > } > > > > > > So, consuming code, which consumes ISomething looks like: > > > > private void ProcessISomething(ISomething toProcess) { > > List<Object> containingLists = > toProcess.GetContainingLists(); > > > > // and now what... how to access the lists inside > > containingLists ? > > // that's right, via a non-generic interface. because any > > generic interface > > // would require _at compile time_ the generic type. > > Which is UNKNOWN. > > foreach(IList list in containingLists) > > { > > // yadda yadda > > } > > } > > > > You might think: I can solve this with a helper method: > > > > private void DoProcessing<T>(List<T> toProcess) { > > // yadday yadda > > } > > > > and write ProcessISomething as: > > > > However, how would you use T in this method? You've > to specify > > a where clause, and ProcessISomething, how would you write that? > > > > See, with generic interfaces, you don't solve this, as you > > then still need a generic type parameter to specify _at > compile time_ > > . With NON generic interfaces you solve this. That's why > non-generic > > interfaces should be used to consume generic code in routines which > > don't know the type of the generic parameter. > > > > Using interfaces is also a way to do generic > programming, i.e. > > write a program that can work with multiple types. Mixing > interfaces > > and generics, makes things only more complicated instead of more > > usable and readable. > > > > Frans > > > > > > > > > > > > -----Original Message----- > > > From: Discussion of advanced .NET topics. > > > [mailto:[EMAIL PROTECTED] On Behalf Of > > Frans Bouma > > > Sent: 17 October 2006 16:39 > > > To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM > > > Subject: Re: [ADVANCED-DOTNET] Type declarations in ide.... > > > > > > > I agree....its more of an experiment at the > > moment.....and they are > > > > interfaces....just parametised ones. > > > > > > Generic interfaces? Isn't that mitigating the aspect of > > > interfaces? > > > I mean: if you have an instance of Class<T>, and pass > that to some > > > code and inside that code you don't know the type of T (this can > > > happen), you can refer to the instance of Class<T> with the > > interface, > > > so your code will become very simple. If you have generic > > interfaces > > > as well, the problem remains and you have to at some > point need to > > > know the type of T to get things compiled. > > > > > > This especially creates problems when T has to be of some > > > type, so where clauses have to be added to the method. > > > With an interface you can solve this problem. so I always > > use the rule > > > of thumb: use interfaces to use generic classes in code where you > > > don't know the generic parameter type, and thus keep your > > interfaces > > > non-generic just for this purpose. > > > > > > FB > > > > > > > > > > > -----Original Message----- > > > > From: Discussion of advanced .NET topics. > > > > [mailto:[EMAIL PROTECTED] On Behalf Of > > > Frans Bouma > > > > Sent: 17 October 2006 13:17 > > > > To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM > > > > Subject: Re: [ADVANCED-DOTNET] Type declarations in ide.... > > > > > > > > > A slightly bizarre question.... > > > > > > > > > > I've got some generic methods that return generic types, > > > > the problem > > > > > is that sometimes the types get very large...e.h. > > > > > > > > > > > > > > > Foo<Foo<Bar<Foo<Bar>,Foo<Foo>>>> x = > > > > > a.GetFoo<Foo<Bar<Foo<Bar>,Foo<Foo>>>> > > > > > (b); > > > > > > > > > > is there a way of getting the IDE to auto generate the type > > > > > declaration, so I don't spend hours foo'ing and bar'ing. > > > > > > > > I'm not sure if you should continue with this. It > > > makes code > > > > VERY unreadable. Too much of a good is still 'too much' ;) > > > > > > > > You might want to use an interface here and there. > > > > > > > > Frans > > > > > > > > =================================== > > > > This list is hosted by DevelopMentor(r) http://www.develop.com > > > > > > > > View archives and manage your subscription(s) at > > > > http://discuss.develop.com > > > > > > > > > > > > ************************************************************** > > > > ************* > > > > CONFIDENTIALITY NOTICE > > > > > > > > The contents of this e-mail are confidential to the > > > ordinary user of > > > > the e-mail address to which it was addressed, and may also be > > > > privileged. If you are not the addressee of this e-mail > > > you may not > > > > copy, forward, disclose or otherwise use it or any part of > > > it in any > > > > form whatsoever.If you have received this e-mail in > error, please > > > > e-mail the sender by replying to this message. > > > > > > > > It is your responsibility to carry out appropriate virus > > and other > > > > checks to ensure that this message and any attachments do > > > not affect > > > > your systems / data. Any views or opinions expressed in > > this e-mail > > > > are solely those of the author and do not necessarily > > > represent those > > > > of MTV Networks Europe unless specifically stated, nor > does this > > > > message form any part of any contract unless so stated. > > > > > > > > MTV reserves the right to monitor e-mail communications from > > > > external/internal sources for the purposes of ensuring > > correct and > > > > appropriate use of MTV communication equipment. > > > > > > > > MTV Networks Europe > > > > ************************************************************** > > > > ************* > > > > > > > > > > > > =================================== > > > > This list is hosted by DevelopMentorR http://www.develop.com > > > > > > > > View archives and manage your subscription(s) at > > > > http://discuss.develop.com > > > > > > > > > > =================================== > > > This list is hosted by DevelopMentor(r) http://www.develop.com > > > > > > View archives and manage your subscription(s) at > > > http://discuss.develop.com > > > > > > > > > ************************************************************** > > > ************* > > > CONFIDENTIALITY NOTICE > > > > > > The contents of this e-mail are confidential to the > > ordinary user of > > > the e-mail address to which it was addressed, and may also be > > > privileged. If you are not the addressee of this e-mail > > you may not > > > copy, forward, disclose or otherwise use it or any part of > > it in any > > > form whatsoever.If you have received this e-mail in error, please > > > e-mail the sender by replying to this message. > > > > > > It is your responsibility to carry out appropriate virus > and other > > > checks to ensure that this message and any attachments do > > not affect > > > your systems / data. Any views or opinions expressed in > this e-mail > > > are solely those of the author and do not necessarily > > represent those > > > of MTV Networks Europe unless specifically stated, nor does this > > > message form any part of any contract unless so stated. > > > > > > MTV reserves the right to monitor e-mail communications from > > > external/internal sources for the purposes of ensuring > correct and > > > appropriate use of MTV communication equipment. > > > > > > MTV Networks Europe > > > ************************************************************** > > > ************* > > > > > > > > > =================================== > > > This list is hosted by DevelopMentorR http://www.develop.com > > > > > > View archives and manage your subscription(s) at > > > http://discuss.develop.com > > > > > > > =================================== > > This list is hosted by DevelopMentor(r) http://www.develop.com > > > > View archives and manage your subscription(s) at > > http://discuss.develop.com > > > > > > ************************************************************** > > ************* > > CONFIDENTIALITY NOTICE > > > > The contents of this e-mail are confidential to the > ordinary user of > > the e-mail address to which it was addressed, and may also be > > privileged. If you are not the addressee of this e-mail > you may not > > copy, forward, disclose or otherwise use it or any part of > it in any > > form whatsoever.If you have received this e-mail in error, please > > e-mail the sender by replying to this message. > > > > It is your responsibility to carry out appropriate virus and other > > checks to ensure that this message and any attachments do > not affect > > your systems / data. Any views or opinions expressed in this e-mail > > are solely those of the author and do not necessarily > represent those > > of MTV Networks Europe unless specifically stated, nor does this > > message form any part of any contract unless so stated. > > > > MTV reserves the right to monitor e-mail communications from > > external/internal sources for the purposes of ensuring correct and > > appropriate use of MTV communication equipment. > > > > MTV Networks Europe > > ************************************************************** > > ************* > > > > > > =================================== > > This list is hosted by DevelopMentorR http://www.develop.com > > > > View archives and manage your subscription(s) at > > http://discuss.develop.com > > > > =================================== > This list is hosted by DevelopMentor(r) http://www.develop.com > > View archives and manage your subscription(s) at > http://discuss.develop.com > > > ************************************************************** > ************* > CONFIDENTIALITY NOTICE > > The contents of this e-mail are confidential to the ordinary > user of the e-mail address to which it was addressed, and may > also be privileged. If you are not the addressee of this > e-mail you may not copy, forward, disclose or otherwise use > it or any part of it in any form whatsoever.If you have > received this e-mail in error, please e-mail the sender by > replying to this message. > > It is your responsibility to carry out appropriate virus and > other checks to ensure that this message and any attachments > do not affect your systems / data. Any views or opinions > expressed in this e-mail are solely those of the author and > do not necessarily represent those of MTV Networks Europe > unless specifically stated, nor does this message form any > part of any contract unless so stated. > > MTV reserves the right to monitor e-mail communications from > external/internal sources for the purposes of ensuring > correct and appropriate use of MTV communication equipment. > > MTV Networks Europe > ************************************************************** > ************* > > > =================================== > This list is hosted by DevelopMentorR http://www.develop.com > > View archives and manage your subscription(s) at > http://discuss.develop.com > =================================== This list is hosted by DevelopMentorĀ® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com