Question when Converting JS to C#

I’m starting to get the hang of how closely related js and c# are, but there are some instances where it’s not really a direct conversion. Can anybody point out how to correctly convert the following lines of code?

var logFile = "ChatLog.txt";

import System.IO; 
 
private var groupNames = ArrayList();
 
private var scrollingNotices = new Array();
 
for(var entry in playerList) {}

Thanks in advance.

This:

var logFile = "ChatLog.txt";

Will become:

string logFile = "ChatLot.txt"; 

In C#, you can also use the ‘var’ keyword but only in local space, like:

void something(int x)
{
  var t = myObjWhichHasAveryLongNameThatImTooLazyToType(x);
  ...
}

It’s meant to be used when the type name is long as you see. In that situation it makes the code look nicer and easier to read. It lets the compiler determine the type of the variable/object by what comes after the assignment,It’s NEVER meant to be used in situations where you don’t know what’s the return type of a function, so you say “oh well, I don’t know/understand what this returns, so I’ll just use var and let the compiler figure it out for me”
When you use var, it has to be done during initialization, which means you can’t do this:

var t;
t = 10;

you also can’t use it in global space, like:

private int x, y;
public Enemy sleeping;
var myObj = new MyObj(); // <-- ERROR

Moving on, this:

import System.IO;  

Will become:

using System.IO;

This:

private var groupNames = ArrayList();

Will become:

private ArrayList groupNames = new ArrayList();

Couple of notes here:

  1. Almost try to never use ArrayList,
    it stores objects, which then have
    to be cast out which involves
    getting into boxing/unboxing. See
    this video for more info.
    Instead use a List - Located
    inside System.Collection.Generic
    wanna have a list of ints? List<int> myList = new List<int>(/* pass in the len if you want */);

  2. In C#, when you deal with reference
    types, you have to allocate memory
    for them. You do that using the
    ‘new’ keyword. Which basically
    allocates memory enough for your
    object to fit in - using the ‘new’
    keyword your object lives in the
    heap memory, which is where all your
    reference types will be, as apposed to the stack for value types. Dif
    between stack and heap
    .

  3. In C#, if you don’t specify an
    access modifier (public, private,
    protected, internal, etc) - private
    will be used by default. Both the
    declarations below are the same:

    int x;
    private int y;

This:

private var scrollingNotices = new Array();

Will become:

you should know now by yourself :)

Btw does Js have the ‘new’ keyword? - If so I didn’t know about that actually.

Finally:

for(var entry in playerList) {}

Becomes:

foreach(var entry in playerList) {}

In C# there are a couple of loops:

  1. foreach: which iterates over an
    IEnumerable (like a list, or
    any type of collection) - I think
    you know how this works, you’ve been
    using it in JS.
  2. the for loop.
  3. the while loop.
  4. the do-while loop.

Hope I helped :slight_smile:

These links will help you:

http://wiki.unity3d.com/index.php/Programming

http://wiki.unity3d.com/index.php/Comparison_of_Programming_Languages