I was going through  IEnumerable and IEnumerator , but could not get one point clearly..if we have foreach, then why do we need this two interfaces? Is there any scenario where we have to use interfaces.If yes, then can somebody explain with an example. Any suggestions and remarks are welcome. Thanks.
27821
Can anyone explain IEnumerable and IEnumerator to me? For example, when to use it over foreach? what's the difference between IEnumerable and IEnumerator? Why do we need to use it?
103027
In C#, if you have two base interfaces with the same method (say, F()) you can use explicit implementation to perform different impl. for F(). This alloes you to differently treat the object, corresponding to the current point of view: as IMyInterface1 or IMyInterface2. Is this possible in Java?
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?
75761
I'd like to do the equivalent of the following in LINQ, but I can't figure out how:  IEnumerable&lt;Item&gt; items = GetItems(); items.ForEach(i =&gt; i.DoStuff());   What is the real syntax?
55036
I saw this reply from Jon on :     If you want a single collection to   contain multiple unrelated types of   values, however, you will have to use   List&lt;object&gt;   I'm not comparing ArrayList vs List&lt;&gt;, but ArrayList vs List&lt;object&gt;, as both will be exposing elements of type object. What would be the benefit of using either one in this case?  EDIT: It's no concern for type safety here, since both class is exposing object as its item. One still needs to cast from object to the desired type. I'm more interested in anything other than type safety.  EDIT: Thanks Marc Gravell and Sean for the answer. Sorry, I can only pick 1 as answer, so I'll up vote both.
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.
25404
In the  question one of the answers had the following code snippet:  IEnumerable&lt;object&gt; FilteredList() {     foreach(object item in FullList)     {         if(IsItemInPartialList(item))             yield return item;     } }   What does the yield keyword do there?  I've seen it referenced in a couple places, and one other question, but I haven't quite figured out what it actually does.  I'm used to thinking of yield in the sense of one thread yielding to another, but that doesn't seem relevant here.
86447
I love recursion. I think it simplifies things a lot. Another may disagree; I think it also makes the code a lot easier to read. However, I've noticed that recursion is not used as much in languages such C# as they are in LISP (which by the way is my favorite language because of the recursion).   Does anybody know if there is any good reasons not use recursion in the languages such as C#? Is it more expensive than iteration?
1080
How can you enumerate an enum in C#?  E.g. the following code does not compile:  public enum Suit {     Spades,     Hearts,     Clubs,     Diamonds }  public void EnumerateAllSuitsDemoMethod() {     foreach (Suit suit in Suit)     {         DoSomething(suit);     } }   And it gives the following compile-time error:     'Suit' is a 'type' but is used like a 'variable'   It fails on the Suit keyword, the second one.
58176
I was looking at C# collection initializers and found the implementation to be very pragmatic but also very unlike anything else in C#  I am able to create code like this:  using System; using System.Collections;  class Program {     static void Main()     {         Test test = new Test { 1, 2, 3 };     } }  class Test : IEnumerable {     public IEnumerator GetEnumerator()     {         throw new NotImplementedException();     }      public void Add(int i) { } }   Since I have satisfied the minimum requirements for the compiler (implemented IEnumerable and a public void Add) this works but obviously has no value.  I was wondering what prevented the C# team from creating a more strict set of requirements?  In other words why, in order for this syntax to compile, does the compiler not require that the type implement ICollection?  That seems more in the spirit of other C# features.
12224
Inspired by another question asking about the missing Zip function: Why is there no ForEach extension method on the IEnumerable interface? Or anywhere? The only class that gets a ForEach method is List&lt;&gt;. Is there a reason why it's missing, maybe performance?
49724
Take the following two lines of code:  for (int i = 0; i &lt; some_vector.size(); i++) {     //do stuff }   And this:  for (some_iterator = some_vector.begin(); some_iterator != some_vector.end();     some_iterator++) {     //do stuff }   I'm told that the second way is preferred. Why exactly is this?
21215
I have just noticed that a multidimensional array in C# does not implement IEnumerable&lt;T&gt;, while it does implement IEnumerable. For single-dimensional arrays, both IEnumerable&lt;T&gt; and IEnumerable are implemented.  Why this difference? If a multi-dimensional array is IEnumerable, surely it should also implement the generic version? I noticed this because I tried to use an extension method on a multidimensional array, which fails unless you use Cast&lt;T&gt; or similar; so I can definitely see the an argument for making multidimensional arrays implement IEnumerable&lt;T&gt;.  To clarify my question in code, I would expect the following code to print true four times, while it actually prints true, false, true, true:  int[] singleDimensionArray = new int[10]; int[,] multiDimensional = new int[10, 10];  Debug.WriteLine(singleDimensionArray is IEnumerable&lt;int&gt;); Debug.WriteLine(multiDimensional is IEnumerable&lt;int&gt;); Debug.WriteLine(singleDimensionArray is IEnumerable); Debug.WriteLine(multiDimensional is IEnumerable);
53155
Anyone have a good rule of thumb for choosing between different implementations of Java Collection interfaces like List, Map, or Set?  For example, generally why or in what cases would I prefer to use a Vector or an ArrayList, a Hashtable or a HashMap?
9629
I use a lot of lists and arrays but I have yet to come across a scenario in which the array list couldn't be used just as easily as, if not easier than, the linked list.  I was hoping someone could give me some examples of when the linked list is notably better.
17471
I'm getting the error below when trying to loop through a listbox and then remove the item.     List that this enumerator is bound to has been modified. An enumerator can only be used if the list does not change.   foreach (string s in listBox1.Items) {     MessageBox.Show(s);     //do stuff with (s);     listBox1.Items.Remove(s); }   How can I remove the item and still loop through the contents?
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!