-2

I’m looking for equivalent C# code of following line

private List<? extends HotSpot> hotSpots;

Any help is much appreciated.

A N M Bazlur Rahman
  • 2,280
  • 6
  • 38
  • 51
  • 7
    `private List hotSpots;` unlike java, C# generics are not a joke. If you come from a java background to C#, you will have to get used to real generics. – Federico Berasategui Aug 16 '13 at 20:47
  • 1
    Check this thread: http://stackoverflow.com/questions/4732494/cs-equivalent-of-javas-extends-base-in-generics – Eldar Agalarov Aug 16 '13 at 20:47
  • @HighCore are you going to say, java generics are joke? are you okay, I don't get it ... – A N M Bazlur Rahman Aug 16 '13 at 21:06
  • 2
    @rokonoid don't get him seriously. He always makes a similar comment whenever he sees a question about c# and java (*knowing comments can't be downvoted :)* ) – EZI Aug 16 '13 at 21:09
  • 4
    @rokonoid, QtX, just read up on type-erasure. There's really nothing more to say. You can't do `o instanceof T`, you can't do `new T[]`, you can't do `(T)o`. The list is long and filled with tears. – Kirk Woll Aug 16 '13 at 21:12
  • @KirkWoll my comment was not about comparing c# & Java. Read it again. – EZI Aug 16 '13 at 21:14
  • 1
    Primary reason for having wildcards in Java was to migrate pre generics code.I remember having arguments with the #java and trying to spend hours to explain utility of template classes (what new kids call 'generics'), operator overloading, better syntax for properties etc. I am glad, some of those arguments turned out constructive. – KalEl Aug 16 '13 at 21:18
  • 2
    I thought it was a joke when they used to say "we won't implement template classes, because it is not needed and the additional complexity can be fatal for developers to manage". It was the same argument everytime in #java, that you'd hurt yourself if you have this 'feature' in the language. I mean, seriously! – KalEl Aug 16 '13 at 21:20
  • 1
    @HighCore: Stop being so inflammatory, please! rokonoid: being rude to HighCore doesn’t really solve the problem! Thanks, everyone. – Ry- Aug 17 '13 at 05:25
  • @KirkWoll [`Class.isInstance()`](http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#isInstance(java.lang.Object)), [`Array.newInstance()`](http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Array.html#newInstance(java.lang.Class,%20int)), [`Class.cast()`](http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#cast(java.lang.Object). The main practical distinction between type erasure and reification isn't in the fact that the latter automagically passes the type token around. Having switched to C# lately, the lack of backwards compatibility also comes at a cost. – millimoose Aug 17 '13 at 13:03
  • @KalEl The fact that you're conflating template classes and generics makes it hard to take your claims seriously. They're completely different language features that happen to overlap in one or two use cases. Templates are a way of generating code at compile-time, where generics are a way to express higher-order type constraints. (Which templates actually can't do now that the "concepts" proposal is dead.) They both happen to accomplish typesafe collections, in completely different ways. They also happen to both use angle brackets in their syntax. But that's mostly it for the commonalities. – millimoose Aug 17 '13 at 13:09
  • @millimoose type-safety and generics are not conflicting ? Im not sure though, just heard a conversion like this. and generics are compiled time feature in java right ? – A N M Bazlur Rahman Aug 17 '13 at 15:19
  • @rokonoid They are, but they do not generate code like C++ templates do, they just enable better type checking. By contrast, from what I know of C++ templates, they don't really change the type system much. From what I gather from searching for this on SO, C++ doesn't really have any sort of variance (what Java's wildcards and C#'s `in` and `out` modifiers do) between instantiations of templates built into the language. (Although it seems to be possible to write specific classes that allow this.) – millimoose Aug 17 '13 at 16:05
  • @rokonoid So, tl;dr: Java/C# generics are a compile-time feature. C++ templates are a compile-time feature. That doesn't mean they work the same way. They work in dramatically different ways, and while they overlap in some problems they solve, each also does a bunch of things the other one doesn't. – millimoose Aug 17 '13 at 16:07
  • @rokonoid Last, but not least, C# is somewhat confusing here in that it also generates **types**. (I'm not sure if this is at compile-time or at runtime, and if it involves generating code or just some sort of thinner type token.) That is, in C# `List` and `List` are genuinely different types, and you can't even cast from one to the other at runtime. In Java there is *technically* possible - the compiler won't issue an error, and at runtime the cast will succeed. (And the code will probably break somewhere else later.) There is just one `ArrayList` type in the runtime though. – millimoose Aug 17 '13 at 16:13
  • @rokonoid Oh, and further muddying the waters is that Java's generics had wildcards since day one, while C# gained its variance features somewhat slowly later. (Originally I believe `IEnumerable` was special-cased to be covariant w/r/t `T`, and the next C# version had the `in` and `out` modifiers.) Wildcards also let you express type constraints that are much more flexible ([and sometimes inscrutable](http://docs.oracle.com/javase/tutorial/extra/generics/morefun.html)) than anything C# has. Which is why saying which system is "real" is kind of meaningless, the differences run too deep. – millimoose Aug 17 '13 at 16:19
  • @millimoose thanks a lot for your so informative comments. – A N M Bazlur Rahman Aug 17 '13 at 16:22
  • 1
    Also rich is railing against "complexity confuses developers", while also apparently not working in either C++, Scala, or Haskell. Or understanding *why* especially the latter two aren't very common in practice, nor have a notable ecosystem of libraries to solve practical tasks. Developers have more to contribute to a project than the ability to keep the *language* in their heads, and its complexity does come at a cost, and it increases the barrier of entry to a platform for people who are better at working in a given problem domain than in a complex language. It's not an easy tradeoff to make – millimoose Aug 17 '13 at 16:27
  • a answer from one of my colleague, "its kinds of picking one from the two girls. One does not really care about her beauty, rather cares much for you and the other one does the opposite. Now the choice is yours" – A N M Bazlur Rahman Aug 18 '13 at 10:46

2 Answers2

7

Depending on exactly what you need, you're probably either looking for:

public class MyClass
{
    private IList<HotSpot> hotSpots;
}

or using where

public class MyClass<T> where T : HotSpot
{
    private IList<T> hotSpots;
}
Tim S.
  • 55,448
  • 7
  • 96
  • 122
  • 6
    You could elaborate the differences. Just saying. – user2674389 Aug 16 '13 at 20:56
  • @user2674389 The second example defines MyClass as instantiable with a type parameter, e.g. `new MyClass()`, which would create an instance of MyClass which can only work with T objects that extend/implement MyHotSpot (which itself must extend/implement HotSpot, as specified by the `where` clause). The first class cannot be instantiated with a type parameter and so would always work with objects that extend/implement HotSpot. (I'm equivocating the class's functionality with the constraints of the 'hotSpot' list for the sake of simplicity.) – Mike Clark Aug 16 '13 at 21:13
  • 1
    You very rarely use `where` with classes on the right hand side. One of the cases where `where` can be used is where you need to constrain a type to include two or more interfaces `public class MyClass where T : IEquateable, ICollection` – Scott Chamberlain Aug 16 '13 at 21:15
  • 2
    @ScottChamberlain: On the contrary, I use `where` in generic class/function definitions all the time. Both my [High-speed Priority Queue](http://goo.gl/FRGZOr) and [Weighted Randomization](http://goo.gl/UShApL) data structures use it. And before the .Net 4.0 co/contravariance features, the .Net library [should have used it more, too](http://stackoverflow.com/a/2004371/238419). Perhaps you just don't work with many generic classes? – BlueRaja - Danny Pflughoeft Aug 16 '13 at 21:57
1

Functionally, I'd say the closest is:

IEnumerable<HotSpot> hotSpots;

If the real type of the the enumerable happens to be an IList, then ElementAt() and such will be O(1).

As of .NET 4.5, you could also use:

IReadOnlyList<HotSpot> hotSpots;

and use List.AsReadOnly() to wrap regular lists.

The .NET approach to variance in generics is letting specific interfaces be either covariant or contravariant, and consequently only allow them to define methods with the generic type parameter as the return value only, or only in the argument list. (As opposed to Java where the compiler does these checks in each expression.) My guess is the rationale is that C# implements generics using reification, and a concrete type like List<out T> can't exist in the type system.

millimoose
  • 39,073
  • 9
  • 82
  • 134