C#结构体比较

雨燕双飞 提交于 2020-01-25 02:06:44

C#结构体是值类型,方法中传参时是值的拷贝;

结构体定义:

enum PointPosition
{
    TopLeft,
    TopMiddle,
    TopRight
}

struct EdgePoint
{
    public int left{get;set;}

    public int top{get;set;}

    public PointPosition position{get;set;}

    public static EdgePoint Empty
    {
        get{
            return new EdgePoint();
        }
    }
}
//枚举转换
bool b = Enum.IsDefined(typeof(PointPosition),"TopLeft");
PointPosition p = (PointPosition)Enum.Parse(typeof(PointPosition),"TopLeft");
//true
Console.WriteLine(b);
b = Enum.IsDefined(typeof(PointPosition),"TopMiddle");
//true
Console.WriteLine(b);
b = Enum.IsDefined(typeof(PointPosition),"BottomLeft");
//false
Console.WriteLine(b);

EdgePoint p1 = EdgePoint.Empty();
EdgePoint p2 = EdgePoint.Empty();
//true
Console.WriteLine(p1.Equals(p2));
p1.left = 10;
//false
Console.WriteLine(p1.Equals(p2));
p2.left = 10;
//true
Console.WriteLine(p1.Equals(p2));

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!