创建一个控件列表只是为了在 foreach 中而不是在多行中设置一些共享属性是否可以?
Is it OK to create a List of controls just to set some shared properties in a foreach instead of on mulitiple lines?
我的代码看起来像这样:
if (someCondition)
{
control1.Enabled = true;
control1.BackColor = Colors.CornSilk;
control2.Enabled = true;
control2.BackColor = Colors.CornSilk;
control3.Enabled = true;
control3.BackColor = Colors.CornSilk;
...
}
else
{
control1.Enabled = false;
control1.BackColor = default(Color);
control2.Enabled = false;
control2.BackColor = default(Color);
control3.Enabled = false;
control3.BackColor = default(Color);
...
}
为这些控件创建列表并改为使用循环在内存或性能方面是否有任何缺点?
var requiredControls = new List<Control>() { control1, control2, control3, .. };
if (someCondition)
{
foreach(var c in requiredControls)
{
c.Enabled = true;
c.BackColor = Colors.CornSilk;
}
}
else
{
foreach(var c in requiredControls)
{
c.Enabled = false;
c.BackColor = default(Color);
}
}
甚至
var requiredControls = new List<Control>() { control1, control2, control3, .. };
foreach(var c in requiredControls)
{
c.Enabled = someCondition;
c.BackColor = (someCondition ? Colors.CornSilk : default(Color);
}
您所做的一切都很好,任何性能影响都微乎其微。
您真正需要担心的是可读性。您所做的内容具有很高的可读性,但您可以使用其他语言功能使其更具可读性。
params
关键字是您可以使用的一种工具。这是一个演示。
public class Program
{
public static void Main(string[] args)
{
var foo1 = new Foo() { Bar = 1 };
var foo2 = new Foo() { Bar = 2 };
AddOne(foo1, foo2);
}
public static void AddOne(params Foo[] foos)
{
foreach(var foo in foos)
{
foo.Bar++;
}
}
public class Foo
{
public int Bar { get; set; }
}
}
如果您谈论的是原始性能差异,那么第一个在性能方面是最好的,但是,正如所有评论和答案中提到的那样,差异将是在这种特定情况下可以忽略不计。
创建列表和遍历它的开销较小,考虑到在编译器优化中,有一个步骤对许多循环完成,即 loop unrolling
,这将减少循环步骤并逐行编写而不是遍历所有行。
我的代码看起来像这样:
if (someCondition)
{
control1.Enabled = true;
control1.BackColor = Colors.CornSilk;
control2.Enabled = true;
control2.BackColor = Colors.CornSilk;
control3.Enabled = true;
control3.BackColor = Colors.CornSilk;
...
}
else
{
control1.Enabled = false;
control1.BackColor = default(Color);
control2.Enabled = false;
control2.BackColor = default(Color);
control3.Enabled = false;
control3.BackColor = default(Color);
...
}
为这些控件创建列表并改为使用循环在内存或性能方面是否有任何缺点?
var requiredControls = new List<Control>() { control1, control2, control3, .. };
if (someCondition)
{
foreach(var c in requiredControls)
{
c.Enabled = true;
c.BackColor = Colors.CornSilk;
}
}
else
{
foreach(var c in requiredControls)
{
c.Enabled = false;
c.BackColor = default(Color);
}
}
甚至
var requiredControls = new List<Control>() { control1, control2, control3, .. };
foreach(var c in requiredControls)
{
c.Enabled = someCondition;
c.BackColor = (someCondition ? Colors.CornSilk : default(Color);
}
您所做的一切都很好,任何性能影响都微乎其微。
您真正需要担心的是可读性。您所做的内容具有很高的可读性,但您可以使用其他语言功能使其更具可读性。
params
关键字是您可以使用的一种工具。这是一个演示。
public class Program
{
public static void Main(string[] args)
{
var foo1 = new Foo() { Bar = 1 };
var foo2 = new Foo() { Bar = 2 };
AddOne(foo1, foo2);
}
public static void AddOne(params Foo[] foos)
{
foreach(var foo in foos)
{
foo.Bar++;
}
}
public class Foo
{
public int Bar { get; set; }
}
}
如果您谈论的是原始性能差异,那么第一个在性能方面是最好的,但是,正如所有评论和答案中提到的那样,差异将是在这种特定情况下可以忽略不计。
创建列表和遍历它的开销较小,考虑到在编译器优化中,有一个步骤对许多循环完成,即 loop unrolling
,这将减少循环步骤并逐行编写而不是遍历所有行。