- Home /
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.
Answer by vexe · Sep 09, 2013 at 02:38 PM
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:
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 */);
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.
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:
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.
the for loop.
the while loop.
the do-while loop.
Hope I helped :)
Pretty sure the generic List also uses boxing and unboxing under the hood, but only for primitive types.
Also, it should be noted that List
is not always the best option to use. There is an alternative list structure, LinkedList
which has some slightly different properties. If you're doing a lot of adding and removing to the list, and only need to iterate through the entire list (for example, if you have a list of objects and you need to update all of them every frame), then LinkedList is more efficient. However, if you are doing a lot of reading of individual values, then List is more efficient. In technical terms:
LinkedList:
Add is O(1)
Get is O(n) where n is the index of the item
Iteration is O(n) where n is the number of objects in the list
List:
Add is O(n) where n is the number of objects in the list (if adding a new item pushes it over capacity, otherwise it is O(1))
Get is O(1)
Iteration is O(n) where n is the number of objects in the list
As a general guide for this, you want the majority of your operations to be as low as you can (so O(1) is better than O(n)). Generally, because List's Add is only O(n) some of the time, in most cases, List is preferable, but LinkedList does have some quite common uses.
Great comment, totally agree! - In fact I like to use LinkedList more :D - I recently used a Queue implemented by a linked list to implement a Clipboard-kinda structure.
$$anonymous$$aking the decision of LinkedList VS List really depends on what you need. Is fast accessing crucial to you? do you have a huge number of elements you iterate through regularly? go for lists. Are you in a situation where you don't care about fast access, but care about memory, you don't want redundant re-allocations each time the list gets full? go with a LinkedList, etc.
Well, both lists are virtually identical for iteration, since they both just follow pointers. List gets the pointers from the list wrapper, while LinkedList gets them from the previous element.
Here's some interesting benchmarking List VS LinkedList And some more info about List VS ArrayList for the OP.
Answer by Sisso · Sep 09, 2013 at 01:38 PM
These links will help you:
http://wiki.unity3d.com/index.php/Programming
http://wiki.unity3d.com/index.php/Comparison_of_Programming_Languages
Your answer
