迭代静态值集(或字段,属性等) [英] Iterate across static set of values (or fields, properties, etc)

查看:80
本文介绍了迭代静态值集(或字段,属性等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码块:

Consider the following chunk of code:

foreach (string c in new string[] { "abc", "defg"}) { } 



有没有办法使用 foreach - 在C#中构建,不涉及动态创建数组?我知道 foreach 只能用于 IEnumerable -implementing类型,但请考虑以下代码块:


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. */



这不是很干净吗?



它会在以下情况哪些多个对象需要部分平等对待,但不需要在其他地方的集合中统一。



这种方法显然不适合大型迭代(如果相似性那么大,为什么不是迭代成员收集?)但是在多个(同等类型)对象需要大量属性赋值的情况下它会很好。特别是当它生成多个ASP.NET Web窗体控件时很容易变得如此丑陋:


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?

推荐答案

不是我,不是 - 它不会更清楚。

你正在做的是拼写出来编译器究竟是您尝试创建的对象类型,而不是让它猜测。是的,您可以在开始时从变量类型推断出一种类型,但是如果那是var呢?还是一个抽象类?



为了节省你一点点打字,真的有必要增加潜在的混乱吗?



无论如何 - 这不是一个问题。这是一个讨论的开始,应该发布在论坛上,而不是在这里。
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.


不要看错了方向。语句的语法是一种语法,根据定义将其视为给定和使用。



你知道怎么做,没有别的:

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", }) { /* ... */ }





下一个真正的解决方案。



在搜索简洁时,你需要看看在问题的根源。



你的问题是:你有 tbD tbA 等,作为单独命名的控件。你不应该把它们放在第一位。解决方案很简单:不要使用设计师。为了简洁和可维护性,这是邪恶的。只在代码中执行。首先在代码中创建一些控件的数组,将它们添加到表单中(仅在显示之前)。



仅将设计器用于一般布局。通过设计师进行任何重复性工作仅适用于睡眠者。这是不是很明显,这导致了大量的纯手工工作,数百次点击?成为软件开发人员。



-SA



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


这篇关于迭代静态值集(或字段,属性等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆