Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I Make These Vectors Classes or Structs in C#

I am creating a geometry library in C# and I will need the following immutable types:

  • Vector2f (2 floats - 8 bytes)
  • Vector2d (2 doubles - 16 bytes)
  • Vector3f (3 floats - 12 bytes)
  • Vector3d (3 doubles - 24 bytes)
  • Vector4f (4 floats - 16 bytes)
  • Vector4d (4 doubles - 32 bytes)

I am trying to determine whether to make them structs or classes. MSDN suggests only using a struct if the size if going to be no greater than 16 bytes. That reference seems to be from 2005. Is 16 bytes still the max suggested size?

I am sure that using structs for the float vectors would be more efficient than using a class, but what should I do about the double vectors? Should I make them structs also to be consistent, or should I make them classes?

Updated: Looks like the everyone agrees they should be structs. Thanks for the great answers.

like image 615
dewald Avatar asked Apr 18 '10 20:04

dewald


People also ask

Is it better to use struct or class?

Class instances each have an identity and are passed by reference, while structs are handled and mutated as values. Basically, if we want all of the changes that are made to a given object to be applied the same instance, then we should use a class — otherwise a struct will most likely be a more appropriate choice.

Are structs more efficient than classes?

Unlike class, struct is created on stack. So, it is faster to instantiate (and destroy) a struct than a class. Unless (as Adam Robinson pointed out) struct is a class member in which case it is allocated in heap, along with everything else.

Why do we use class instead of structure?

The major difference like class provides the flexibility of combining data and methods (functions ) and it provides the re-usability called inheritance. Struct should typically be used for grouping data. The technical difference comes down to subtle issues about default visibility of members.

When would you use a struct type in C?

You can use it to store variables in different types. The struct type is comparable to classes in object-oriented programming. Sometimes you may need to assign values to objects with the same properties. Instead of creating multiple variables for these objects in your C program, you can define them in a struct.


3 Answers

Microsoft's XNA Framework uses structures for its Vector2/3/4 data types. These contain fields of type float. I don't see anything wrong with doing the same; using fields of type double shouldn't make a big difference.

like image 59
dtb Avatar answered Oct 21 '22 16:10

dtb


I would generally make types like these structs. Structs are more likely to be placed on the stack, and if you use arrays with them, you can get much nicer peformance than if, say, you were to use a List<> of objects. As long as you stay away from operations which will cause your vector classes to be boxed, the struct is the generally going to be the higher performance way to go.

like image 33
Dave Markle Avatar answered Oct 21 '22 15:10

Dave Markle


Immutable structs is a good choice. The size recommendation on MSDN is the weakest, 32 bytes is not really big.

The main argument would be that Vectors are used like simple (numerical) types and an implementation as value type is most appropriate.

A good parallel are the new Complex and BigInteger structs in dotNet 4

like image 4
Henk Holterman Avatar answered Oct 21 '22 16:10

Henk Holterman