I'm making a Vector class in C#, with a normalized parameter, which is the vector normalized. However, when I'm trying to calculate the normalized Vector, I get a stack overflow exception. Is there a better way to do it? Here is my code (inside the constructor):
namespace Vector3
{
internal class Program
{
static void Main(string[] args)
{
Vector3 vector = new(3, 4, 0);
Console.WriteLine(vector.magnitude);
Console.WriteLine(vector.normalized);
}
}
class Vector3
{
public float x, y, z;
public readonly float magnitude;
public readonly Vector3 normalized;
public Vector3(float x, float y, float z)
{
this.x = x;
this.y = y;
this.z = z;
this.magnitude = Convert.ToSingle(Math.Sqrt(Math.Pow(this.x, 2) + Math.Pow(this.y, 2) + Math.Pow(this.z, 2)));
this.normalized = new Vector3(this.x/this.magnitude, this.y/this.magnitude, this.z/this.magnitude);
}
}
}
You have infinite recursion in the constructor. While you creating
Vector3, you create anotherVector3inside the constructor fornormalizedvalue. And this cycle never ends and finally gives youStackOverflowExceptionYou can use lazy creation of
normalizedvalue as shown below.