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
1
Question by LeftyTwoGuns · Jun 18, 2014 at 08:15 PM · c#javascriptconvert#pragma strict

Does C# use #pragma strict?

I've noticed I see #pragma in JavaScript but don't think I've seen it in C#. Is this a Java only thing? When I'm adapting a script from Java to C#, I should omit #pragma strict?

Comment
Add comment · Show 3
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 perchik · Jun 18, 2014 at 08:34 PM 1
Share

Pragma strict is a way to tell javascript to "be a stricter compiler". That is, it should throw errors when you try to do dumb things that javascript allows. C# is by nature "stricter" and doesn't need to be told to do that

avatar image AndyMartin458 · Jun 18, 2014 at 08:42 PM 1
Share

stricter isn't the right term. Precisely it requires you to define the type of each variable such as int, double, etc in javascript.

avatar image Eric5h5 · Jun 18, 2014 at 08:44 PM 1
Share

Java isn't an abbreviation for Javascript, it's an entirely different language, which is not used in Unity.

2 Replies

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

Answer by Bunny83 · Jun 18, 2014 at 10:09 PM

Actually, no. The C# compiler doesn't understand the pragma "strict". The MS C# compiler actually only understands two pragmas. Not sure if the Mono compiler has any additional ones but "strict" certainly isn't one of them. Unknown pragmas are usually ignored, however to keep the code clean you shouldn't add it to a C# script.

Like Eric already said JavaScript is not Java. You really shouldn't confuse those two languages. Java has no relation to Unity at all (with the small exception of Android since android apps run in a Java VM). Unity's JavaScript is actually called UnityScript (internally and more and more externally as well). UnityScript just has a JavaScript-like-syntax but is actually a complete seperate language. Unity called it "JavaScript" only for marketing purposes ^^

Comment
Add comment · Show 2 · 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 LeftyTwoGuns · Jun 19, 2014 at 12:02 AM 0
Share

Thanks for the clarification

When discussing "JavaScript" for Unity, is it better to refer to it as UnityScript, then?

avatar image Bunny83 · Jun 19, 2014 at 12:36 AM 0
Share

@LeftyTwoGuns: Yes definitely ;) However calling it "JavaScript" is also fine... It's actually wrong but it's been like this for years now, so that can't be fixed anymore :)

Internally it's called UnityScript. Even the compiled assembly names of your "javascript"-scripts are called "assembly-unityscript-firstpass.dll" and "assembly-unityscript.dll"

avatar image
2

Answer by Kiwasi · Jun 18, 2014 at 10:05 PM

As far as I can tell C# does not support #pragma strict. See reference here.

C# by nature uses strong typing. Though you can still use the var type to tell the compiler to infer the type.

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 Bunny83 · Jun 18, 2014 at 10:11 PM 0
Share

var is actually not a type. It's just syntactical sugar ;) The type is still staticly declared. If the type can't be deter$$anonymous$$ed at compiletime you'll get an error.

avatar image Kiwasi · Jun 18, 2014 at 10:29 PM 0
Share

That is true.

avatar image AndyMartin458 · Jun 18, 2014 at 10:30 PM 0
Share

@Bunny83 the term "syntactic sugar" implies that it does not do anything useful. I would argue var does in fact do something that nothing else can do, and it is therefore useful if the API and return types of a library are unknown to the user (rare I know). I've seen it used for obscure classes whose API does not need to be known. It's use is similar to that of auto in C++ as you described, but I believe Bored$$anonymous$$ormon understood that since he said compiler, not runtime. Sorry for the rant ;)

avatar image Bunny83 · Jun 18, 2014 at 11:44 PM 0
Share

@Andy$$anonymous$$artin458: Why does "syntactic sugar" implies it's not useful? o.O What i said is that var is not a seperate type and the variable is still strong typed. In the answer it sounds like "var" is an exception.

ps: If syntactic sugar is not useful we would all code in assembler and IL, right? ;)

pps: "var" does in fact nothing special which can't be done in another way. It just saves you from writing out the type name. In the end (in the compiled form / IL) you'll see no difference between:

     var tmp = 5;

or

     int tmp = 5;

var should never be used to "hide" obscure classes. Code should be readable. You can use var when it's clear what type the variable will get:

 //positive examples:
 var speed = 5.0f;                 // float
 var vec = Vector3.Project(A,B);   // Vector3
 var myList = new List<string>();  // List<string>
 
 // negative example:
 var someVar = someFunction();     // ???
 
 // in such cases it's way better to do:
 RaycastHit hit = someFunction();

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

27 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make this line work in C#? 1 Answer

Help converting this to C# - a few issues 3 Answers

java to C# conversion 1 Answer

Converting a javascript to C# 2 Answers

What is this C# code in javascript 0 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