为什么我在使用以下代码时遇到异常?

发布于 2024-10-01 00:45:05 字数 683 浏览 0 评论 0原文

我在接口中定义了一个属性,如下所示:

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

旧伤还要旧人安 2024-10-08 00:45:05

您可以通过在调用之前检查 OnPropertyChanged 是否为 null 来修复此问题,假设 OnPropertyChanged 是一个事件或委托变量(您还没有明确说明这一点)。这与 boolBoolean 无关,它们是等效的(假设 Boolean 解析为 System.Boolean >)。

我看不出它会抛出 NullReferenceException 的任何其他原因 - 尽管它确实可以帮助您澄清在抛出异常时您是在调用 getter 还是 setter。一个简短但完整的例子会更有帮助。

You fix it by checking whether OnPropertyChanged is null before calling it, assuming OnPropertyChanged is an event or a delegate variable (you haven't made this clear). This has nothing to do with bool or Boolean, which are equivalent (assuming that Boolean is resolved to System.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.

笑咖 2024-10-08 00:45:05

bool 只是 Boolean 的语法快捷方式

bool is just a syntax shortcut for Boolean

第七度阳光i 2024-10-08 00:45:05

boolBoolean 在编译时计算结果相同。

Both bool and Boolean evaluate to the same thing in compilation.

深者入戏 2024-10-08 00:45:05

没有任何。 Boolean 是 .net CLI 用于表示真/假值的值。 bool 是 c# 使用的。

none. Boolean is what the .net CLI uses to represent a true/false value. bool is what c# uses.

风流物 2024-10-08 00:45:05
  1. bool 是 struct System.Boolean 的 C# 别名。他们是一样的。
  2. 您的问题可能是 this.OnPropertyChanged 未分配。这与 boolBoolean 完全无关。
  1. bool is a C# alias for the struct System.Boolean. They are the same.
  2. Your problem probably is that this.OnPropertyChanged is unassigned. This is completely unrelated to bool vs Boolean.
记忆里有你的影子 2024-10-08 00:45:05

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.

伊面 2024-10-08 00:45:05

bool 关键字只是 Boolean 关键字的类型别名。

就像int 一样,它是Int32 的别名。

The bool keyword is just a type alias for the Boolean keyword.

Just the same as int is an alias for Int32.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文