我正在尝试将此代码转换为C#,并且想知道Javascript的"Array.push"等同于什么?这是我正在转换的几行代码:
var macroInit1, macroInit2;
var macroSteps = new Array();
var i, step;
macroInit1 = "Random String";
macroInit2 = "Random String two";
macroSteps.push(macroInit1 + "another random string");
macroSteps.push(macroInit2 + "The last random string");
for (i=0; i<10; i++)
{
for (step = 0; step < macroSteps.length; step++)
{
// Do some stuff
}
}
Run Code Online (Sandbox Code Playgroud)
你可以使用List<string>:
var macroInit1 = "Random String";
var macroInit2 = "Random String two";
var macroSteps = new List<string>();
macroSteps.Add(macroInit1 + "another random string");
macroSteps.Add(macroInit2 + "The last random string");
for (int i = 0; i < 10; i++)
{
for (int step = 0; step < macroSteps.Count; step++)
{
}
}
Run Code Online (Sandbox Code Playgroud)
当然,这段代码在C#中看起来非常难看.根据您对这些字符串执行的操作,您可以利用C#中内置的LINQ功能将其转换为单行,并避免编写所有这些命令性循环.
这就是说,当将源代码从一种语言转换为另一种语言时,不仅仅是简单地搜索等效数据类型等等......您还可以利用目标语言提供的功能.
| 归档时间: |
|
| 查看次数: |
5977 次 |
| 最近记录: |