这可能是个愚蠢的问题,但是下面的属性,会不会出现刚拿到就抛出异常的情况呢?
如果我做了类似 bool value = this.BatchValuation;
的操作 - 但我还没有设置 BatchValuation
,它会不会只设置 value
为null
,还是会引发异常?
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/