且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#:如何在for循环中为整数(i)分配许多变量?

更新时间:2022-04-13 03:29:23

正如其他人所说,数组(或其他集合)是一种更好的方法。

As others have said, an array (or another collection) is a much better way of doing this.

将数组视为仅通过索引始终访问的变量集合。

Think of an array as just being a collection of variables which you always access by index.

我不能想一想为什么你想要有一组变量,如你的问题所示。如果你绝对这样做,并且它们是实例/静态变量,你可以使用反射来访问它们,如果你真的想要 - 但是使用数组或其他集合会更好,更好。

I can't think of any reason why you'd want to have a set of variables as shown in your question. If you absolutely have to do that, and they're instance/static variables, you could use reflection to access them if you really wanted to - but using an array or other collection would be much, much better.