Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class VS ref Struct

I am programming a game using C#, thus, I am very concerned about performance.

I would like to know what are the main differences, and if possible, performance considerations of using either a Class to pass data around, or a struct passed by reference.

I wish not to copy the data around, for performance reasons (I assume passing by ref is much faster than by value here).

I know that a class is always passed by reference and that a struct is passed by value, but I talking about passing the struct by reference here.

An example of the data I wish to pass :

    public delegate void PathCompleteDelegate(List<PathFinderNode> path);
public struct PathFinderJob{
    public PathCompleteDelegate callback;
    public Vector3 start, end;
    public PathSize unitSize;
    public BoxCollider boxCollider;
}

In the previous example, would using a class make a difference? If so, what would the difference be? Would a class be faster than a struct in this example? Why?

Thank you. Joao Carlos

like image 506
Joao Carlos Avatar asked Mar 29 '12 10:03

Joao Carlos


People also ask

What is a ref struct?

What is a ref struct? Well, a ref struct is basically a struct that can only live on the stack. Now a common misconception is that since classes are reference types, those live on the heap and structs are value types and those live on the stack.

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.

Why use a struct over a class C#?

1) Structures provide better performance when we have small collections of value-types that you want to group together. 2) Use Structure if all member fields are of value type. Use Class if any one member is of reference type.

What is difference between struct and class in C#?

Basically, a class combines the fields and methods(member function which defines actions) into a single unit. A structure is a collection of variables of different data types under a single unit. It is almost similar to a class because both are user-defined data types and both hold a bunch of different data types.


1 Answers

I know that a class is always passed by reference and that a struct is passed by value, but I talking about passing the struct by reference here.

You probably have the right idea, but this is incorrect. Everything in C# is passed by value unless you use the ref keyword.

Class instances are reference types, struct instances are value types.

When you pass a reference type by value, you pass a copy of the reference (small). When you pass a value type by value, you pass a copy of the whole data (potentially large).

Jon Skeet has a good explanation of all this here.

like image 169
Graham Clark Avatar answered Sep 30 '22 23:09

Graham Clark