When should I use a struct instead of a class?

后端 未结 15 2277
臣服心动
臣服心动 2020-11-22 13:00

MSDN says that you should use structs when you need lightweight objects. Are there any other scenarios when a struct is preferable over a class?

Some people might ha

15条回答
  •  野性不改
    2020-11-22 13:13

    I am surprised I have not read at any of the previous answer this, which I consider the most crucial aspect :

    I use structs when I want a type with no identity. For example a 3D point:

    public struct ThreeDimensionalPoint
    {
        public readonly int X, Y, Z;
        public ThreeDimensionalPoint(int x, int y, int z)
        {
            this.X = x;
            this.Y = y;
            this.Z = z;
        }
    
        public override string ToString()
        {
            return "(X=" + this.X + ", Y=" + this.Y + ", Z=" + this.Z + ")";
        }
    
        public override int GetHashCode()
        {
            return (this.X + 2) ^ (this.Y + 2) ^ (this.Z + 2);
        }
    
        public override bool Equals(object obj)
        {
            if (!(obj is ThreeDimensionalPoint))
                return false;
            ThreeDimensionalPoint other = (ThreeDimensionalPoint)obj;
            return this == other;
        }
    
        public static bool operator ==(ThreeDimensionalPoint p1, ThreeDimensionalPoint p2)
        {
            return p1.X == p2.X && p1.Y == p2.Y && p1.Z == p2.Z;
        }
    
        public static bool operator !=(ThreeDimensionalPoint p1, ThreeDimensionalPoint p2)
        {
            return !(p1 == p2);
        }
    }
    

    If you have two instances of this struct you don't care if they are a single piece of data in memory or two. You just care about the value(s) they hold.

提交回复
热议问题