Skip to content
Advertisement

Objects difference between javascript and C#

I want to convert a code from JavaScript to C#. in javascript we can use an object easily as follows, this function returns the indices of the elements that their addition is equal to the target

JavaScript

the return of the function will be [1,3] which is an array of the indices of 5,7 (as 5+7 = 12)

So what is a similar way to accomplish this solution in c#? Is this applicable by using Objects in C#? or even Arrays/Lists/Dictionaries? What is the best way?

Thanks

Advertisement

Answer

You can do that in JavaScript because arrays are secretly objects here.

In C# it will be a little more complicated because you can’t enlarge arrays like that.

You can use List, but you will have to increase the length and insert nulls in between.

I suggest using a dictionary because it’s similar to JavaScript’s object.

JavaScript

Then you can iterate like that:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement