为什么我在使用以下代码时遇到异常?
我在接口中定义了一个属性,如下所示:
Boolean IsBusy { get; }
它在类中实现为:
private Boolean _isBusy = false;
public Boolean IsBusy
{
get
{
return this._isBusy;
}
private set
{
if (this._isBusy != value)
{
this._isBusy = value;
this.OnPropertyChanged("IsBusy");
}
}
}
然后,当我运行应用程序时,在检查构造函数中的 IsBusy 值时,我总是会收到以下错误:
“IsBusy”引发了“System. NullReferenceException' bool {System.NullReferenceException}
我无法弄清楚。如果我将所有 Boolean
更改为 bool
,则会出现相同的错误。
我该如何修复它?
I have a property defined in an interface as:
Boolean IsBusy { get; }
It is implemented in the class as:
private Boolean _isBusy = false;
public Boolean IsBusy
{
get
{
return this._isBusy;
}
private set
{
if (this._isBusy != value)
{
this._isBusy = value;
this.OnPropertyChanged("IsBusy");
}
}
}
Then when I run the app, I always get following kind of error when check IsBusy value in constructor:
'IsBusy' threw an exception of type 'System.NullReferenceException' bool {System.NullReferenceException}
I can't figure it out. If I change all Boolean
to bool
, get same error.
How can I fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以通过在调用之前检查
OnPropertyChanged
是否为 null 来修复此问题,假设OnPropertyChanged
是一个事件或委托变量(您还没有明确说明这一点)。这与bool
或Boolean
无关,它们是等效的(假设Boolean
解析为System.Boolean
>)。我看不出它会抛出 NullReferenceException 的任何其他原因 - 尽管它确实可以帮助您澄清在抛出异常时您是在调用 getter 还是 setter。一个简短但完整的例子会更有帮助。
You fix it by checking whether
OnPropertyChanged
is null before calling it, assumingOnPropertyChanged
is an event or a delegate variable (you haven't made this clear). This has nothing to do withbool
orBoolean
, which are equivalent (assuming thatBoolean
is resolved toSystem.Boolean
).I can't see any other reason it would throw
NullReferenceException
- although it would really help you could clarify whether you were calling the getter or the setter at the time it threw the exception. A short but complete example would be even more helpful.bool
只是Boolean
的语法快捷方式bool
is just a syntax shortcut forBoolean
bool
和Boolean
在编译时计算结果相同。Both
bool
andBoolean
evaluate to the same thing in compilation.没有任何。
Boolean
是 .net CLI 用于表示真/假值的值。bool
是 c# 使用的。none.
Boolean
is what the .net CLI uses to represent a true/false value.bool
is what c# uses.bool
是 structSystem.Boolean
的 C# 别名。他们是一样的。this.OnPropertyChanged
未分配。这与bool
与Boolean
完全无关。bool
is a C# alias for the structSystem.Boolean
. They are the same.this.OnPropertyChanged
is unassigned. This is completely unrelated tobool
vsBoolean
.C# 包含所有本机类型的别名。 String 代表 string,Int32 代表 int,等等。您使用的没有区别:
String 与 string在 C# 中,
布尔值不能为 NULL,因此您可能会因为 OnPropertyChanged 中的某些内容而收到错误。
C# contains aliase for all the native types. String for string, Int32 for int, etc. there is no difference with which you use:
String vs string in C#
Boolean cannot be NULL, so you are likely getting an error because of something in OnPropertyChanged.
bool
关键字只是Boolean
关键字的类型别名。就像
int
一样,它是Int32
的别名。The
bool
keyword is just a type alias for theBoolean
keyword.Just the same as
int
is an alias forInt32
.