I defined a int vector like this:
public struct MyIntVector2
{
public int x;
public int y;
public static int Dot(ref MyIntVector2 a, ref MyIntVector2 b)
{
return (a.x * b.x) + (a.y * b.y);
}
//..
//Some other methods
}
public class MyIntVector2Test : MonoBehaviour {
void Start () {
var a = new MyIntVector2(1,1);
var b = new MyIntVector2(2,2);
var c = MyIntVector2.Dot(ref a, ref b);
}
}
My question is about MyIntVector2.Dot()'s performance:
When I call MyIntVector2.Dot, the struct a and b will be boxing by C# runtime?
I read ref keyword - C# reference | Microsoft Learn
It said: There is no boxing of a value type when it is passed by reference.
I think
“public static int Dot(ref MyIntVector2 a, ref MyIntVector2 b)” 's performance is better than
“public static int Dot(MyIntVector2 a, MyIntVector2 b)”
Is right?
But I also looked UnityEngine.Vector2.Dot:
static float Dot(Vector2 lhs, Vector2 rhs)
Why unity don’t use ref keyword? like:
static float Dot(ref Vector2 lhs, ref Vector2 rhs)
This means struct will be boxing when it passed by “ref” in unity c#?