Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Benifir · Sep 09, 2013 at 01:12 PM · convertjs-c#

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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:

  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 :)

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hoeloe · Sep 09, 2013 at 03:30 PM 3
Share

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.

avatar image vexe · Sep 09, 2013 at 03:50 PM 1
Share
  • 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.

avatar image Hoeloe · Sep 09, 2013 at 03:53 PM 0
Share

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.

avatar image vexe · Sep 09, 2013 at 03:57 PM 0
Share

Here's some interesting benchmarking List VS LinkedList And some more info about List VS ArrayList for the OP.

avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

JsonConvert.DeserializeObject Not responding when building IOS project. 0 Answers

how to reference a js script attached to a prefabs child from a c# script attached to another prefab? 0 Answers

Convert to .tiff, with fbx export, removes the alpha channel? 1 Answer

Convert string to material 0 Answers

Convert Array 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges