#641 – Using the this Keyword in a struct

In a class, the this keyword refers to the current instance of a class, or the instance that a particular method was invoked on.

You can also use the this keyword in a struct and it will refer to the current instance of the struct.

In the example below, the this keyword is used to refer to the fields of the current instance of the struct, to distinguish them from the input parameters that have the same name.

    public struct BoxSize
    {
        public double x;
        public double y;
        public double z;

        public bool HasBiggerVolume(double x, double y, double z)
        {
            if ((this.x * this.y * this.z) > (x * y * z))
                return true;
            else
                return false;
        }
    }
Advertisements