无论如何使用2个变量来命名变量? [英] Is there anyway to name a variable using 2 variables?

查看:160
本文介绍了无论如何使用2个变量来命名变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个以2个其他变量命名的新变量,我不知道如何在C#中做到这一点。如果有效,下面的代码将创建名为number1,number2,number3,number4和number5的5个变量。带下划线的int是我的视觉工作室说我收到错误无效的表达式术语'int'。



我尝试了什么:



I want to have a new variable named after 2 other variables and I'm not sure how to do it in C#. The code below would create 5 variables named number1, number2, number3, number4, and number5 if it worked. The underlined int is where my visual studio says I'm receiving the error "Invalid expression term 'int'".

What I have tried:

int a = 1;
string number = "number";




for(int counter = 1; counter <=5; counter++)
{
    Console.Write("Please input a number: ");
    int (string.Concat(number,a)) = int.Parse(Console.ReadLine());
    a++;
}

推荐答案

不,你不能用动态名称动态创建变量。



看起来你想从输入中获取5个整数并存储它们。您是否考虑过使用阵列 [ ^ ] (或 List< int> [ ^ ])?
No, you cannot dynamically create variables with a dynamic name.

It looks like you want to take 5 integers from input and store them. Have you considered using an array[^] (or a List<int>[^])?


如果您知道自己想要的数量,请使用数组或类似内容,如果您不知道,请使用列表吨。关于如何使用数组的示例数据的c#数组。
Use an array or things like this if you know how many you want, or a List if you don't. Google "c# arrays" for examples\tutorials on how to use arrays.


引用:

是无论如何使用2个变量命名变量?

Is there anyway to name a variable using 2 variables?



不使用C#。

你想要做的是数组存在的原因。


Not with C#.
What you want to do is the reason why arrays exist.


这篇关于无论如何使用2个变量来命名变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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