5

I'm trying to convert this code over to C# and was wondering what is the equivalent to Javascript's "Array.push"? Here's a few lines of the code i'm converting:

 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 
      }
   }
1
  • Any progress with the push.js conversions? Will you opensource/share it? I'm at the same problem right now. Commented Sep 23, 2019 at 8:38

3 Answers 3

9

You could use a 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++)
    {

    }
}

Of course this code looks extremely ugly in C#. Depending on what manipulations you are performing on those strings you could take advantage of the LINQ features built into C# to convert it into a one-liner and avoid writing all those imperative loops.

This is to say that when converting source code from one language to another it's not a matter of simply searching for the equivalent data type, etc... You could also take advantage of what the target language has to offer.

Sign up to request clarification or add additional context in comments.

Comments

4

You can replace that either with

or

or

1 Comment

and use .Add where you'd use Push()
2

It can be much more clean, declarative and nice in C#, for example:

//In .NET both lists and arraus implement IList interface, so we don't care what's behind
//A parameter is just a sequence, again, we just enumerate through
//and don't care if you put array or list or whatever, any enumerable
public static IList<string> GenerateMacroStuff(IEnumerable<string> macroInits) {
{
    return macroInits
                .Select(x => x + "some random string or function returns that") //your re-initialization
                .Select(x => YourDoSomeStuff(x)) //what you had in your foreach
                .ToArray();
}

And it can be used then:

var myInits = new[] {"Some init value", "Some init value 2", "Another Value 3"};
var myMacroStuff = GetMacroStuff(myInits); //here is an array of what we need

BTW, we can suggest you a solution how to "do stuff" properly and nicely if you just describe what you want, not just show us a code we don't have any clue how to use and ask how to translate it literally. Because a literal translation can be so unnatural and ugly in .NET world, and you will have to maintain this ugliness... We don't want you to be in this position :)

2 Comments

+1 for LINQ (although it may be beyond the OP's current knowledge): Remember that you can use .Select(YourDoSomeStuff) instead of .Select(x => YourDoSomeStuff(x))
Yes I remember. But if you are new in LINQ it might be hard to understand that .Select(DoSomeStuff) is actually a method group.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.