对于C#空类型的编码实践 [英] Coding practices for C# Nullable type

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

问题描述

我从来没有在我的C#代码中使用可空类型。现在,我已经决定改变在我的代码引入可空类型我的编码实践。

I have never used nullable types in my C# code. Now I have decided to change my coding practice by introducing nullable types in my code.

什么是在编码实践,同时使正常的过渡应作出的重大变化数据类型可空数据类型的应用程序的情况下?

What are the major changes in the coding practices should be made while making a transition from normal datatypes to nullable data-types in case of application programming?

什么是应该采取?

什么是点我应该始终注意

What are the points I should always watch out for?

推荐答案

可空< T> 是当你需要一个可能的无效状态值类型有用,或者如果数据正在被从数据库中检索可能包含该列的值。它是在一些旧的FORTRAN代码我移植到C#为无效值是负的或0很常见的,但是这是麻烦,特别是当值在数学函数被使用。这是更为明确的使用可空< T> 来显示可能的无效状态

Nullable<T> is useful for when you need a possible invalid state for a value type, or if the data is being retrieved from a database that may contain a null value for the column. It is very common in some old FORTRAN code I am porting to C# for invalid values to be negative or 0, but this is troublesome especially when the values are used in mathematical functions. It is far more explicit to use Nullable<T> to show that possible invalid state.

值得一提的下列是相同的:

It is worth mentioning that the following is the same:

Nullable<int> myNullableInt;
int? myNullableInt;

这篇关于对于C#空类型的编码实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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