如何在C#中拥有n个变量
想象一下这段代码
for (int iDay = 1; iDay <= total_days; iDay++)
{
question = CheckString(s.challenge_1_q);
answer = CheckStringA(s.challenge_1_a);
// more here
}
,但我真正拥有的是从 challenge_1_q
到 challenge_24_q
和 challenge_1_a
到 challenge_24_a
我最好的选择是什么拥有动态变量,因为今天是 24,“明天”可能只有 18。
使用 dynamic
是正确的方法吗?或者我真的需要一个开关
并忘记动态性?
imagine this code
for (int iDay = 1; iDay <= total_days; iDay++)
{
question = CheckString(s.challenge_1_q);
answer = CheckStringA(s.challenge_1_a);
// more here
}
but what I really have is from challenge_1_q
to challenge_24_q
and challenge_1_a
to challenge_24_a
what is my best option to have dynamic variables as today it's 24, "tomorrow" could be only 18.
is the use of dynamic
the proper way? or I really need to have a switch
and forget about dynamism ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建一个名为 QuestionAnswer 的类,然后在 s 上存储一个 List。
访问代码将如下所示:
QuestionAnswer 类:
以及现有类的定义:
您可以将数十个项目添加到列表中,而不是使用数十个变量:
Create a class called QuestionAnswer, then store a List on s.
The accessing code will look like this:
The QuestionAnswer class:
And the definition on your existing class:
Instead of having dozens of variables, you add dozens of items to the list:
您还可以创建
Structure
并使用类似的东西
You can also create
Structure
And use Something like this