之一,当应在C#中使用可空类型? [英] When should one use nullable types in c#?

查看:133
本文介绍了之一,当应在C#中使用可空类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经多次要求在许多采访下列问题....但还是不能用一个简单的例子来说明他们...

I have been repeatedly asked the following questions in many interviews.... But still can't explain them with a simple example...


  1. 什么是可空类型在C#?

  2. 在当前应在c#中使用可空类型?

  3. 您能否举一个简单的例子吗?

有什么建议?

推荐答案

使用可空类型(C#编程指南)

有关时,你可能会使用一个例子
  可空类型,考虑如何在
  普通布尔变量可以有两种
  值:true和false。没有
  这标志着值未定义。在
  许多编程应用中,最
  特别是数据库交互,
  变量可以在未定义存在
  州。例如,在所涉及的领域
  数据库可能包含真正的价值
  或假的,但它也可以包含不
  价值可言。同样,参考
  类型可设置为null,以指示
  他们不被初始化。

For an example of when you might use a nullable type, consider how an ordinary Boolean variable can have two values: true and false. There is no value that signifies "undefined". In many programming applications, most notably database interactions, variables can exist in an undefined state. For example, a field in a database may contain the values true or false, but it may also contain no value at all. Similarly, reference types can be set to null to indicate that they are not initialized.

这篇关于之一,当应在C#中使用可空类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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