繁体   English   中英

以下歧义错误是什么意思?

[英]What does the following ambiguity errors mean?

研究了这个错误,有人说这是一个错误,但是当我使用他们的一些建议时,并不能解决问题。 我该怎么办?

**码

/// Indicates if the profiles has been added.

public Boolean _isNew
{
    get { return _isNew; }
    set { _isNew = value; }
}

/// Indicates if any of the fields in the class have been modified

public Boolean _isDirty
{
    get { return _isDirty; }
    set { _isDirty = value; }
}


 //Gets and Sets delimiterChar 

         public Char _delimiterChar
     {
    get { return _delimiterChar; }
    set  { _delimiterChar = value;}

    }

错误**

Ambiguity between 'ConsoleApplication3.ProfileClass.isNew'and 'ConsoleApplication3.ProfileClass.isNew

Ambiguity between 'ConsoleApplication3.ProfileClass.isDirty'and 'ConsoleApplication3.ProfileClass.isDirty

Ambiguity between 'ConsoleApplication3.ProfileClass._delimiterChar'and 'ConsoleApplication3.ProfileClass._delimiterChar

您发布的代码将导致递归并最终导致stackoverflow。 您正在尝试在属性设置器中设置属性。 您需要一个后备字段或自动属性来实现您的工作。 就像是:

private bool _isNew;
public Boolean IsNew
{
    get { return _isNew; }
    set { _isNew = value; }
}

要么

public Boolean IsNew {get; set;}

在C#中,如果指定要获取和设置的内容,则不能使用与属性相同的名称(自引用问题)。 到目前为止,您正在尝试获取并为其设置属性,该属性无效。 同样要注意命名约定,您的公共财产不应以下划线开头,而应遵循大写驼峰法。

有两个答案,根据您需要做什么,两个答案同样有效。

方法1:如果您取出它正在获取和设置的内容,则C#可以确定IsNew属性引用了一个隐含字段。 这实质上是方法2的简写。

public bool IsNew { get; set; } // shorthand way of creating a property

方法2:指定要获取和设置的字段。

private bool  _isNew; // the field
public bool IsNew { get => _isNew; set => _isNew = value; } // this is the property controlling access to _isNew

在此处阅读更多信息: 速写访问器和变量

本质上,如果不需要执行任何其他操作,则默认情况下使用Method 1。 但是,如果您需要在获取或设置时提供其他功能,请使用方法2(即,在MVVM模式中查找示例https://www.c-sharpcorner.com/UploadFile/raj1979/simple-mvvm-pattern- in-wpf /

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM