Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 gsgeek · May 09, 2017 at 05:20 AM · c#variablesmethod

Are c# methods arguments stored as local variables?

I'd like to know something about how are method arguments handled in Unity's c#. Suppose you have a method named myMethod ( int arg1 int arg2 int arg3 int arg4 int arg5). Suppose further that you're calling myMethod within your script with arguments whose value are not known at compiletime, because the arguments are themselves calls to other methods that perform complicated calculations based on the game state.

Now, I imagine that the compiler might treat this situation in one of a number of ways, and I'd like to know how exactly.

One possibility is this: when the method is invoked with a set of provided arguments, the arguments are evaluated immediately and stored as local variables. Thus, arg1, arg2, arg3, arg4 and arg5 are all cmputed once. Later, in the body of the method, whenever I refer to one of the arguments, I'll be referring to the already computed value. That would also mean that if the body of the method have several conditional branches, each one mentioning only one of the arguments, then the arguments that would not have been used in the actual execution of the method body would have been computed for nothing.

On the other hand, this is another possibility: the arguments are NOT computed and stored immediately when the method is invoked. Instead they are computed each time that are actually "used" insde the method body. This would mean that there would be no innecessary computations in case some of the arguments are only used in conditional branches that don't get to be executed in a certain run of the game, but would also mean that if I plan to use the arguments repeatedly through the method body, I should save the arguments value to variables in the method body.

I figured I'd ask this here in case this depends on how C# is implemented for Unity (i.e. in differences between Mono and .NET, or between Unity's version of Mono and the "standard" more updated version of it. Also I didn't find any word about this neither in the .NET documentation nor through StackOverflow's questions.

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

0 Replies

· Add your reply
  • Sort: 

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

6 People are following this question.

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

Related Questions

Calling a variable based on a variables name? 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Use a String Variable to Dynamically Invoke a Method 1 Answer

Problem with var in C# (Vector3, transform and similiar) 1 Answer


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