Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Consider the following chunk of code:
foreach (string c in new string[] { "abc", "defg"}) { } 
Is there a way to use a foreach-construct in C# that doesn't involve on-the-fly creation of an array? I know that foreach is only supposed to work on IEnumerable-implementing types, but consider the following chunk of code:
foreach (string c in { "abc", "defg" }) { } /* This doesn't compile. */
Wouldn't that feel cleaner?
 
It would in situations in which multiple objects require partial equal treatment but aren't required to be unified in a collection anywhere else.
 
This approach obviously wouldn't be suitable for large iterations ("If the similarities are that big, why aren't the iteration members in a collection?") but it'd be nice in situations where multiple (equally typed) objects require mass property assignment. Especially when it comes down to generating multiple ASP.NET Web Forms controls which easily can get ugly like this:
public class MyControl : Control
{
   private readonly TextBox tbA = new TextBox();
   private readonly TextBox tbB = new TextBox();
   private readonly TextBox tbC = new TextBox();
   private readonly TextBox tbD = new TextBox();
 
   protected override void OnInit(EventArgs e)
   {
      base.OnInit(e);
      foreach (TextBox textBox in new TextBox[] { tbA, tbB, tbC, tbD })
         this.Controls.Add(textBox);
      assignProperties();
   }
 
   private void assignProperties()
   {
      // Assign property values that happen to be equal to tbA and tbB
      // even though they don't have anything else in common.
      //
      //   "tbA.EnableViewState = tbB.EnableViewState = true;"
      // ...is also an option, but that too becomes messy sometimes.
      foreach (TextBox textBox in new TextBox[] { tbA, tbB })
         textBox.EnableViewState = true;
 
      // Assign property values to tbA, tbB, and tbC. Still, they don't
      // have anything else in common.
      foreach (TextBox textBox in new TextBox[] { tbA, tbB, tbC })
         textBox.ApplyStyleSheetSkin(Page);
 
      foreach (TextBox textBox in new TextBox[] { tbD, tbA })
         textBox.SomeOtherCoincidentallyEqualProperty = "Value";
   }
}
It's obviously a first-world developer issue, but I'm very keen on code brevity. Any thoughts?
Posted 21-Feb-13 7:13am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Not to me, no - it wouldn't be clearer.
What you are doing is spelling out to the compiler exactly what type of object you are trying to create, rather than leaving it to guess. Yes, you could infer a type from the variable type at the start, but what if that was "var"? Or an abstract class?
 
To save you a little bit of typing is it really necessary to add potential confusion?
 
Anyway - this isn't a question. It's a discussion starter, and should be posted in the forums, not here.
  Permalink  
Comments
Hoogdraaed at 25-Feb-13 10:16am
   
OriginalGriff, thanks for your reply.
 
I think I kindof agree; it's half a discussion starter / half a question. I was curious about a shorter construction so that was my initial thought. I'll consider using the forum next time; I'm new to posting here.
 
Edit: considering your argument on the construct, I hadn't put thought into 'var' and abstract classes. That's the reason I was looking for. Now I actually feel silly for thinking such a construct would be acceptable and logical.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Don't look in a wrong direction. A syntax of a statement is a syntax, consider it as given and use by definition.
 
You know how to do it, there is nothing else:
foreach (string c in new string[] { "abc", "defg", }) { /* ... */ }
 
The real solution goes next.
 
In you search for brevity, you need to look in the root of the problem.
 
Your problem is: you have tbD, tbA, etc., as individually named controls. You should not have them in first place. The solution is simple: don't use the designer. For brevity and maintainability, this is evil. Do it in code only. Create arrays of some controls in code in first place, add them to the form (only before it is shown).
 
Use the designer only for general layout. Doing any repetitive work via designer is just for lamers. Isn't that obvious that this lead to a lot of pure manual work, hundreds of clicks? Be a software developer.
 
—SA
  Permalink  
v2
Comments
Hoogdraaed at 25-Feb-13 10:22am
   
Sergey, thanks for your reply.
 
I hate using the designer for the reasons you've just pointed out; the code is just an example. I'm actually on your side when it comes down to manual creation of generic web forms (user) controls. But that's a different subject.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 DamithSL 295
1 OriginalGriff 214
2 BillWoodruff 170
3 Zoltán Zörgő 165
4 Maciej Los 113
0 OriginalGriff 7,760
1 DamithSL 5,874
2 Sergey Alexandrovich Kryukov 5,444
3 Maciej Los 5,076
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web02 | 2.8.141223.1 | Last Updated 21 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100