Skip to main content
 首页 » 编程设计

c#之为什么我得到 'Operator ' !此结构实现中的 =' can not be applied to operands of type Point and null>'

2024年02月13日26zhoujg

我真的不知道为什么在此处尝试验证属性代码中的输入值时出现此错误:

using ConsoleApplication4.Interfaces; 
 
public struct Vector:IComparable 
{ 
    private Point startingPoint; 
 
    private Point endPoint; 
 
    public Point StartingPoint   
    { 
        get 
        { 
            return new Point(startingPoint); 
        } 
        set 
        { 
            if(value != null) 
                this.startingPoint = value; 
        } 
    } 
 
    public Point EndingPoint 
    { 
        get 
        { 
            return new Point(this.endPoint); 
        } 
        set 
        { 
            if(value != null) 
                this.startingPoint = value; 
        } 
    } 

我得到的错误是在我有 if(value!=null)

的行上

请您参考如下方法:

struct是一个值类型 - 它不能像 class 那样为“null”可能。您可以使用 Nullable<Point> (或 Point? ),但是。