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

  • 本文关键字:是什么 错误 歧义 c#
  • 更新时间 :
  • 英文 :


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

**法典

/// 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

您发布的代码将导致递归和最终的堆栈溢出。您正在尝试在属性资源库中设置属性。您要么需要支持字段,要么需要自动属性来实现您正在执行的操作。像这样:

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/(

最新更新