c# - C# 中的 boolean 属性

标签 c# .net boolean

这可能是个愚蠢的问题,但是下面的属性,会不会出现刚拿到就抛出异常的情况呢?

如果我做了类似 bool value = this.BatchValuation; 的操作 - 但我还没有设置 BatchValuation,它会不会只设置 valuenull,还是会引发异常?

public bool BatchValuation { get; set; }

最佳答案

This might be a dumb question

不是。

in the property below, will there ever be a situation where just getting it will cause an exception?

可能是的。例如,另一个线程在获取该属性的过程中可能会中止您的线程;这似乎是属性 getter 抛出的异常。或者,您可能会在调用属性的那一刻用完堆栈空间,并且可能会抛出堆栈外异常。或者,当您第一次调用该属性时,抖动可能会启动并尝试为生成的代码分配虚拟内存,但您的虚拟地址空间都用完了,因此可能会抛出内存不足异常。

这些都是极不可能的,但它们都是可能的。您问的是是否会出现这种情况,而不是可能

If I hadn't set BatchValuation yet, will it just set value to null, or will it cause an exception?

都没有;它将默认为 false。 boolean 值不可为空。

关于c# - C# 中的 boolean 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8086795/

相关文章:

c# - Mvc4 : N tier architecture

c# - 使用Html.TextBoxFor时如何设置文本框的宽度

android - 无法从 Shared Preferences Android 检索 boolean 值

C++程序不会进入for循环

visual-c++ - c++线性公式简化库

c# - Json序列化错误 "There is already an open DataReader associated with this Command"

.net - 为什么 .Net 没有 Set 数据结构?

.net - 哪个 WPF 控制套件最适合 MVVM?

.net - 使用 Moq 等的好处

c# - 如何以 UTF-8 格式制作 Stream.Write() 输出