对Nullable类型有更好的了解 [英] Better understanding for Nullable types

查看:54
本文介绍了对Nullable类型有更好的了解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一些与可为空的类型有关的问题:

(0)使用可空类型的优缺点?
(1)为什么和何时在程序中使用可为null的类型?

感谢您的答复.

Hello all,

I have some questions related to nullable types:

(0) The advantages and disadvantages of using nullable types?
(1) Why and when I should use a nullable type in my program?

Thank you for your responses.

推荐答案

更好地查看
Better to look this [^] linkedin discussion
--NDK


这有很强烈的作业气味,所以我不会列出任何优点或缺点-您可以自己选择这些优点或缺点!

可为空的类型可以表示其基础值类型的正常值范围,以及一个附加的空值.例如,可空布尔可以包含truefalsenull.在这种情况下,可以使用null来表示未指定".在处理包含可能未分配值的元素的数据库和其他数据类型时,为数字和布尔类型分配null的功能特别有用.例如,数据库中的布尔值字段可以存储true或false值,也可以未定义.如果尝试将此类值检索为正常的bool值,则由于bool不能包含空值,将出现运行时错误.
This has a very strong smell of homework, so I''m not going to list any advantages or disadvantages - you can pick those out for your self!

A nullable type can represent the normal range of values for its underlying value type, plus an additional null value. For example, a Nullable Bool can contain true, false, and null. In this case, the null could be used to say "not specified". The ability to assign null to numeric and Boolean types is particularly useful when dealing with databases and other data types containing elements that may not be assigned a value. For example, a Boolean field in a database can store the values true or false, or it may be undefined. If you try to retrieve such a value into a normal bool value, you will get a run time error becasue bool cannot contain null.


请检查以下内容:

C#中的可空类型:基本介绍 [
check this out:

Nullable Types in C#: Basic Introduction[^]

hope it helps :)


这篇关于对Nullable类型有更好的了解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆