I was wondering, when exactly can I use the foreach loop? Do I have to implement IEnumerable?
123569
This is in C#, I have a class that I am using from some else's DLL. It does not implement IEnumerable but has 2 methods that pass back a IEnumerator. Is there a way I can use a foreach loop on these. The class I am using is sealed.
24897
foreach (var name in parent.names) {     if name.lastname == null)     {         Violated = true;         this.message = "lastname reqd";     }      if (!Violated)     {         Violated = !(name.firstname == null) ? false : true;         if (ruleViolated)             this.message = "firstname reqd";     } }   Whenever violated is true, I want to get out of the foreach loop immediately. How do I do it?
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.
2292
I have been working with a string[] array in C# that gets returned from a function call. I could possibly cast to a Generic collection, but I was wondering if there was a better way to do it, possibly by using a temp array.  What is the best way to remove duplicates from a C# array?
90809
Does anyone know if there is a good equivalent to Java's Set collection in C#? I know that you can somewhat mimic a set using a Dictionary or a HashTable by populating but ignoring the values, but that's not a very elegant way.
75124
I often want to grab the first element of an IEnumerable&lt;T&gt; in .net, and I haven't found a nice way to do it.  The best I've come up with is:  foreach(Elem e in enumerable) {   // do something with e   break; }   Yuck!  So, is there a nice way to do this?
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?
58615
Have you ever tried this before?  static void Main(string[] args) {     int x = 10;     x = x++;     Console.WriteLine(x); }   Output: 10.  but for   static void Main(string[] args) {     int x = 10;     x++;     Console.WriteLine(x); }   Output: 11.  Could anyone explain why this?
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?
71175
I need to move backwards through an array, so I have code like this:  for (int i = myArray.Length - 1; i &gt;= 0; i--) {     // Do something     myArray[i] = 42; }   Is there a better way of doing this?  Update: I was hoping that maybe C# had some built-in mechanism for this like:  foreachbackwards (int i in myArray) {     // so easy }   Update 2: There are better ways. Rune takes the prize with:  for (int i = myArray.Length; i-- &gt; 0; ) {         //do something } //or for (int i = myArray.Length; i --&gt; 0; ) {     // do something }   which looks even better in regular C (thanks to Twotymz):  for (int i = lengthOfArray; i--; ) {         //do something }
17235
Currently, I'm using:  DataTable dt = CreateDataTableInSomeWay();  List&lt;DataRow&gt; list = new List&lt;DataRow&gt;();  foreach (DataRow dr in dt.Rows) {     list.Add(dr); }   Is there a better/magic way?
13274
Does anybody have useful example of this assignment inside a C# method? I have been asked for it once during job interview, and I am still interested in answer myself.
11195
I found an example in the  for Dynamic LINQ that allows you to use a sql-like string (e.g. OrderBy("Name, Age DESC")) for ordering. Unfortunately, the method included only works on IQueryable&lt;T&gt;. Is there any way to get this functionality on IEnumerable&lt;T&gt;?
19431
What, if any, is the performance difference between the following two loops?  for (Object o: objectArrayList) {     o.DoSomething(); }   and   for (int i=0; i&lt;objectArrayList.size(); i++) {     objectArrayList.get(i).DoSomething(); }
36756
Is there a pattern using Linq to dynamically create a filter?  I have the need to create custom filtering on a list, in the past I would just dynamically create the SQL...it doesn't seem like this is possible with Linq.
108218
This is an extension of question from . I just want to verify if the following is actually safe enough for production use.  List&lt;string&gt; lists = new List&lt;string&gt;(); //Code to retrieve lists from DB     foreach (string list in lists) {     Button btn = new Button();     btn.Click += new EventHandler(delegate { MessageBox.Show(list); }); }   I only run through the above once per startup. For now it seems to work alright. As Jon has mentioned about counterintuitive result in some case. So what do I need to watch out here? Will it be ok if the list is run through more than once?
50950
Is there a way to convert a List(of Object) to a List(of String) in c# or vb.net without iterating through all the items? (Behind the scenes iteration is fine – I just want concise code)  Update: The best way is probably just to do a new select  myList.Select(function(i) i.ToString()).ToList();   or  myList.Select(i =&gt; i.ToString()).ToList();