Sign in to follow this  

[SlimDX] Immutable Vectors?

Recommended Posts

Tobble    104
I am new to C# and I was happy to finally figure out that strings are immutable and I could pass around references without worrying if the string is going to be changed.

The vector functions seem to return a new vector and there is no Set(x, y, z) function so I was thinking that maybe they were supposed to immutable too. But the X, Y and Z components are read/write.

I guess I could just treat them as immutable but it worries me that vector calculations in loops might generate a lot of garbage. So maybe there should be a VectorBuilder class.

Anyway, I was just wondering what the usual usage is.

Share this post

Link to post
Share on other sites
MJP    19788
Keep in mind that Vector2/Vector3/Vector4 are value types (structs), and so will be passed by value except in methods that take take a ref or out parameter. So in most cases you won't have to worry about other code changing its fields on you, since they'll be getting copies. Being a value type also means that temporary copies will live in the stack rather than being allocated from the heap, and they won't be managed by the garbage collector. So you don't have to worry about it generating garbage just from doing calculations.

Since you're new to C# I'd recommend looking up how value types and reference types work. It's an important part of the language, and it's rather different from how things work in Java or C++. Knowing when to use a class or a struct can sometimes the difference between code that's totally bogged down by the GC and code that runs smoothly.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this