【问题标题】:simplifying multiple variable assignment code [closed]简化多变量赋值代码
【发布时间】:2014-12-08 20:24:07
【问题描述】:

我应该如何减少我必须编写的代码量?

我真的不知道我应该使用什么技术。

test1.Content = "test1...";
test2.Content = "test2...";
test3.Content = "test3...";
test4.Content = "test4...";
test5.Content = "test5...";
test6.Content = "test6...";
test7.Content = "test7...";

【问题讨论】:

  • “缩小”是什么意思?我看到的唯一可能的缩小是删除换行符/空格并更改变量名称(如果可以的话)。为什么要缩小anways?缩小是为了减少网络流量 - C# 代码很少在应用程序中通过网络传输。
  • 你的意思是重构吗?
  • 您想减少输入的代码量吗?
  • 嗯,我考虑过减少代码量。
  • @NadiaKrawczyk “我想过减少代码量”为了什么目标?表现?不会再快了。可读性?维护?了解test1 是什么也将有所帮助。有点控制不住?

标签: c# .net minify


【解决方案1】:

您可以使用字典来存储变量,而不是使用命名变量来定义所有变量。

var testDic = new Dictionary<int, Test>();
for (int i = 1; i < 8; i++)
{
    testDic[i] = new Test() { Content = "test" + i + "..." };
}

然后像testDic[2] 或testDic[6] 一样访问它们。

【讨论】:

  • 我很困惑;为什么不是数组? Test[] 甚至是列表 List&lt;Test&gt;。
  • 这两个也是可能的选项。 @托比
猜你喜欢
  • 2019-01-12
  • 1970-01-01
  • 2020-07-23
  • 2014-03-22
  • 1970-01-01
  • 2011-12-23
  • 1970-01-01
  • 1970-01-01
  • 2010-12-29
相关资源
最近更新 更多