研究了这个错误,有些人说这是一个bug,但当我使用他们的一些建议时,它并没有解决问题。我该怎么办?
**代码
/// 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发布于 2019-08-20 06:38:23
您发布的代码将导致递归和最终的堆栈溢出。您正在尝试在属性设置器中设置属性。你要么需要一个支持字段,要么需要自动属性来实现你正在做的事情。类似于:
private bool _isNew;
public Boolean IsNew
{
get { return _isNew; }
set { _isNew = value; }
}或
public Boolean IsNew {get; set;}发布于 2019-08-20 06:43:22
在C#中,如果指定要获取和设置的内容,则不能使用与属性相同的名称(自引用问题)。到目前为止,您正在尝试获取属性并将其设置为自身,这是无效的。另外,关于命名约定,您的公共属性不应以下划线开头,而应遵循大写驼峰大小写。
对此有两个答案,根据您需要做什么,这两个答案都是同样有效的。
方法1:如果您取出正在获取和设置的内容,C#可以确定IsNew属性引用了一个隐含字段。这本质上是方法2的简写。
public bool IsNew { get; set; } // shorthand way of creating a property方法二:指定要获取和设置的字段。
private bool _isNew; // the field
public bool IsNew { get => _isNew; set => _isNew = value; } // this is the property controlling access to _isNew点击此处了解更多信息:Shorthand Accessors and Mutators
实际上,如果不需要执行任何额外的操作,则默认使用方法1。但是,如果在获取或设置时需要提供额外的功能,则使用方法2(即在https://www.c-sharpcorner.com/UploadFile/raj1979/simple-mvvm-pattern-in-wpf/示例中查找MVVM模式)。
https://stackoverflow.com/questions/57564660
复制相似问题