Jump to content
  • Advertisement
Sign in to follow this  
HermanssoN

C# ref keyword

This topic is 3480 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello. I'm writing a small game engine in C# and OpenGL (Tao). I have a question abot thw ref keyword. what I would like to determine is if it's more optimized to take parameters by ref, and when it's apropriate. lets take a vector for example. public static void Add( ref Vector3 V1, ref Vector3 V2, out Result ) { Result.X = V1.X + V2.X; Result.Y = V1.Y + V2.Y; Result.Z = V1.Z + V2.Z; } VS public static void Add( Vector3 V1, Vector3 V2 ) { return new Vector3( V1.X + V2.X, V1.Y + V2.Y, V1.Z + V2.Z); } I'm fairly new to using C#, but using ref is the same as saying go to the place in memory where V1 and V2 is stored and read their values, rather then alternative two, where the in parameters are copies of the vectors you sent to the function. Making copies are slower than reading directly?? Is it more or less optimized to use the function that take ref?? taking ref as in parmeter has other disadvantages, for example you can't pass properties as ref, you can't pass this as ref either.

Share this post


Link to post
Share on other sites
Advertisement
In general passing large value types by reference will be more efficient than by value. Part of this is due to the fact that the JIT cannot inline functions that take or return value type parameters (fixed in .Net 3.5 SP1). Also the copying can induce significant overhead.

Avoid using properties in simplistic types, such as vector and matrix, because it can introduce hidden performance costs (SlimDX found this out the hard way :P).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!