C#新手:有什么之间的区别和QUOT;布尔"和"布尔"?

编程入门 行业动态 更新时间:2024-10-23 13:29:20
本文介绍了C#新手:有什么之间的区别和QUOT;布尔"和"布尔"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我开始用C#和遇到的事情是困扰我。我用的是布尔类型的变量,因为我是用在C ++中,我试图把函数或我希望是布尔值到我的变量属性的值。不过我经常遇到情况下,结果类型为布尔?而不是布尔和隐式转换失败。

I'm starting with C#, and encountered something that puzzles me. I use the "bool" type for variables as I was used to in C++, and I try to put the values of functions or properties I expect to be boolean into my variable. However often I encounter cases where the result type is "bool?" and not "bool" and the implicit casting fails.

而不是去书,我决定发布此在计算器上。任何人都可以请解释什么是两个,当每次使用之间的区别?另外,我应该使用布尔?作为我的变量的类型?这是最好的做法是什么?

Instead of going to the books, I decided to post this on StackOverflow. Could anyone please explain what is the difference between the two and when each is used? Also, should I use "bool?" as the type for my variable? Is this the best practice?

感谢您

推荐答案

一个类型后的 符号只是一个快捷方式的Nullable型,布尔?等同于可空<布尔方式>

The ? symbol after a type is only a shortcut to the Nullable type, bool? is equivalent to Nullable<bool>.

布尔是一个值类型,这意味着它不能空,所以可空类型基本上可以让你包装值类型,并能分配空给他们。

bool is a value type, this means that it cannot be null, so the Nullable type basically allows you to wrap value types, and being able to assign null to them.

布尔可包含三个不同的值:真正,假和空。

bool? can contain three different values: true, false and null.

另外,有没有短路运营商(安培;&安培; ||)吗?定义布尔

Also, there are no short-circuiting operators (&& ||) defined for bool?

只有逻辑AND,OR包容性,运营商的定义和他们的行为是这样的:

Only the logical AND, inclusive OR, operators are defined and they behave like this:

x y x & y x | y true true true true true false false true true null null true false true false true false false false false false null false null null true null true null false false null null null null null

可空类型基本上是一个通用的结构,具有以下公共属性:

The Nullable type is basically a generic struct, that has the following public properties:

public struct Nullable<T> where T: struct { public bool HasValue { get; } public T Value { get; } }

的的HasValue 属性指示当前对象是否具有价值,而值属性将得到当前值对象,或者如果HasValue为假的,它会引发InvalidOperationException。

The HasValue property indicates whether the current object has a value, and the Value property will get the current value of the object, or if HasValue is false, it will throw an InvalidOperationException.

现在,你一定很奇怪的东西,可为空是一个结构,不能为空值类型,那么为什么下面的语句是有效的?

Now you must be wondering something, Nullable is a struct, a value type that cannot be null, so why the following statement is valid?

int? a = null;

这例子编译成这样:

.locals init (valuetype [mscorlib]System.Nullable`1<int32> V_0) IL_0000: ldloca.s V_0 IL_0002: initobj valuetype [mscorlib]System.Nullable`1<int32>

一个调用initobj,它指定地址为空引用或相应的原始类型的0初始化值类型的每个字段。

A call to initobj, which initializes each field of the value type at a specified address to a null reference or a 0 of the appropriate primitive type.

这就是它,这里发生了什么是默认结构初始化

That's it, what's happening here is the default struct initialization.

int? a = null;

等同于:

Nullable<int> a = new Nullable<int>();

更多推荐

C#新手:有什么之间的区别和QUOT;布尔&QUOT;和&QUOT;布尔&QUOT;?

本文发布于:2023-05-25 10:55:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/226620.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:布尔   有什么   区别   新手   QUOT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!