If some method requires an ICollection&lt;T&gt; as an argument, but I have only an IEnumerable&lt;T&gt; available:  Is it better to convert this IEnumerable&lt;T&gt; to a IList&lt;T&gt; or is it better to convert it to an array T[] or shall I convert it to something else or is there no difference at all?  The IEnumerable&lt;T&gt; is only required for this method call, so no further read/write/extend is required.
22625
MyClass[] array; List&lt;MyClass&gt; list;   What are the scenarios when one is preferable over the other? And why?
7798
According to the , 3rd edition:        I wish to understand why this decision has been made. What's wrong with generic exceptions?  (As far as I know, generics are simply compile-time syntactic sugar, and they will be translated to Object anyway in the .class files, so effectively declaring a generic class is as if everything in it was an Object. Please correct me if I'm wrong.)
29105
Can someone explain to me why in .NET 2.0 if I have an interface, IPackable and a class that implements that interface OrderItem, when I have a method that takes in a List&lt;IPackable&gt;, passing in a list of List&lt;OrderItem&gt; does not work?    Does anyone know how I could accomplish this functionality?  Code:  public interface IPackable {         double Weight{ get; } }  public class OrderItem : IPackable   public List&lt;IShipMethod&gt; GetForShipWeight(List&lt;IPackable&gt; packages) {    double totalWeight = 0;    foreach (IPackable package in packages) {         totalWeight += package.Weight;    } }   The following code does not work.  List&lt;OrderItem&gt; orderItems = new List&lt;OrderItem&gt;(); List&lt;IShipMethod&gt; shipMethods = GetForShipWeight(orderItems);
57326
I noticed that the generic IEnumerator&lt;T&gt; inherits from IDisposable, but the non-generic interface IEnumerator does not. Why is it designed in this way?  Usually, we use foreach statement to go through a IEnumerator&lt;T&gt; instance. The generated code of foreach actually has try-finally block that invokes Dispose() in finally.
64302
I frequently find myself writing code like this:  List&lt;int&gt; list = new List&lt;int&gt; { 1, 3, 5 }; foreach (int i in list) {     Console.Write("{0}\t", i.ToString()); } Console.WriteLine();   Better would be something like this:  List&lt;int&gt; list = new List&lt;int&gt; { 1, 3, 5 }; Console.WriteLine("{0}\t", list);   I suspect there's some clever way of doing this, but I don't see it. Does anybody have a better solution than the first block?
49954
In C#, when I have an interface and several concrete implementations, can I cast the interface to a concrete type or is concrete type cast to interface?  What are the rules in this case?
40820
I have few methods that returns different Generic Lists.   Exists in .net any class static method or whatever to convert any list into a datatable? The only thing that i can imagine is use Reflection to do this.  IF i have this:  List&lt;Whatever&gt; whatever = new List&lt;Whatever&gt;();   (This next code doesn't work of course, but i would like to have the possibility of:  DataTable dt = (DataTable) whatever;
119910
I'm using .NET 3.5 and would like to be able to obtain every *n*th item from a List.  I'm not bothered as to whether it's achieved using a lambda expression or LINQ.  Edit  Looks like this question provoked quite a lot of debate (which is a good thing, right?).  The main thing I've learnt is that when you think you know every way to do something (even as simple as this), think again!
1843
I have a couple of questions about generic wildcards in Java:   What is the difference between List&lt;? extends T&gt; and List&lt;? super T&gt;? What is a bounded wildcard and what is an unbounded wildcard?
832
.NET offers a generic list container whose performance is almost identical (see Performance of Arrays vs. Lists question). However they are quite different in initialization.   Arrays are very easy to initialize with a default value, and by definition they already have certain size:  string[] Ar = new string[10];   Which allows one to safely assign random items, say:  Ar[5]="hello";   with list things are more tricky. I can see two ways of doing the same initialization, neither of which is what you would call elegant:  List&lt;string&gt; L = new List&lt;string&gt;(10); for (int i=0;i&lt;10;i++) L.Add(null);   or  string[] Ar = new string[10]; List&lt;string&gt; L = new List&lt;string&gt;(Ar);   What would be a cleaner way?  EDIT: The answers so far refer to capacity, which is something else than pre-populating a list. For example, on a list just created with a capacity of 10, one cannot do L[2]="somevalue"  EDIT 2: People wonder why I want to use lists this way, as it is not the way they are intended to be used. I can see two reasons:   One could quite convincingly argue that lists are the "next generation" arrays, adding flexibility with almost no penalty. Therefore one should use them by default. I'm pointing out they might not be as easy to initialize. What I'm currently writing is a base class offering default functionality as part of a bigger framework. In the default functionality I offer, the size of the List is known in advanced and therefore I could have used an array. However, I want to offer any base class the chance to dynamically extend it and therefore I opt for a list.
117936
The following throws an InvalidCastException.  IEnumerable&lt;int&gt; list = new List&lt;int&gt;() { 1 }; IEnumerable&lt;long&gt; castedList = list.Cast&lt;long&gt;(); Console.WriteLine(castedList.First());   Why?  I'm using Visual Studio 2008 SP1.
1725
I have a generic method with this (dummy) code (yes I'm aware IList has predicates, but my code is not using IList but some other collection, anyway this is irrelevant for the question...)  static T FindThing&lt;T&gt;(IList collection, int id) where T : IThing, new() {     foreach T thing in collecion     {         if (thing.Id == id)             return thing;     }     return null;  // ERROR: Cannot convert null to type parameter 'T' because it could be a value type. Consider using 'default(T)' instead. }   This gives me a build error      "Cannot convert null to type parameter   'T' because it could be a value type.   Consider using 'default(T)' instead."   Can I avoid this error?
560
It seems that a List object cannot be stored in a List variable in C#, and can't even be explicitly cast that way.  List&lt;string&gt; sl = new List&lt;string&gt;(); List&lt;object&gt; ol; ol = sl;   results in Cannot implicitly convert type System.Collections.Generic.List&lt;string&gt; to System.Collections.Generic.List&lt;object&gt;  And then...  List&lt;string&gt; sl = new List&lt;string&gt;(); List&lt;object&gt; ol; ol = (List&lt;object&gt;)sl;   results in Cannot convert type System.Collections.Generic.List&lt;string&gt; to System.Collections.Generic.List&lt;object&gt;  Of course, you can do it by pulling everything out of the string list and putting it back in one at a time, but it is a rather convoluted solution.
25516
I have heard that the Java implementation of Generics is not as good as the C# implementation. In that the syntax looks similar, what is it that is substandard about the Java implementation, or is it a religious point of view?
123032
Possible Duplicate:          What are the differences between IEnumerator and IEnumerable?
27319
I want to use a collection initializer for the next bit of code:  public Dictionary&lt;int, string&gt; GetNames() {     Dictionary&lt;int, string&gt; names = new Dictionary&lt;int, string&gt;();     names.Add(1, "Adam");     names.Add(2, "Bart");     names.Add(3, "Charlie");     return names; }   So typically it should be something like:  return new Dictionary&lt;int, string&gt; {     1, "Adam",    2, "Bart"    ...   But what is the correct syntax for this?
104904
I really should be able to get this, but I'm just to the point where I think it'd be easier to ask.  In the C# function:  public static T GetValue&lt;T&gt;(String value) where T:new() {    //Magic happens here }   What's a good implementation for the magic? The idea behind this is that I have xml to parse and the desired values are often primitives (bool, int, string, etc.) and this is the perfect place to use generics... but a simple solution is eluding me at the moment.  btw, here's a sample of the xml I'd need to parse  &lt;Items&gt;     &lt;item&gt;         &lt;ItemType&gt;PIANO&lt;/ItemType&gt;         &lt;Name&gt;A Yamaha piano&lt;/Name&gt;         &lt;properties&gt;             &lt;allowUpdates&gt;false&lt;/allowUpdates&gt;             &lt;allowCopy&gt;true&lt;/allowCopy&gt;         &lt;/properties&gt;        &lt;/item&gt;     &lt;item&gt;         &lt;ItemType&gt;PIANO_BENCH&lt;/ItemType&gt;         &lt;Name&gt;A black piano bench&lt;/Name&gt;         &lt;properties&gt;             &lt;allowUpdates&gt;true&lt;/allowUpdates&gt;             &lt;allowCopy&gt;false&lt;/allowCopy&gt;             &lt;url&gt;www.yamaha.com&lt;/url&gt;         &lt;/properties&gt;     &lt;/item&gt;     &lt;item&gt;         &lt;ItemType&gt;DESK_LAMP&lt;/ItemType&gt;         &lt;Name&gt;A Verilux desk lamp&lt;/Name&gt;         &lt;properties&gt;             &lt;allowUpdates&gt;true&lt;/allowUpdates&gt;             &lt;allowCopy&gt;true&lt;/allowCopy&gt;             &lt;quantity&gt;2&lt;/quantity&gt;         &lt;/properties&gt;     &lt;/item&gt; &lt;/Items&gt;