为什么空在.NET中存在吗? [英] Why does null exist in .NET?

查看:80
本文介绍了为什么空在.NET中存在吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么值在.NET空?这种优越的是有保证的地方,一切都将有一个值,没有什么电话为空?

Why can values be null in .NET? Is this superior to having a guarantee where everything would have a value and nothing call be null?

任何人都知道这些方法都是叫什么名字?

Anyone knows what each of these methodologies are called?

无论哪种方式,我不是很熟悉这一点,但不会有一切的值,使事情变得更容易,在简单,即消除null检查,并能够编写没有更加简化的算法计算另辟蹊径的检查。

Either way, I am not very knowledgeable on this, but wouldn't having a value for everything makes things easier, in terms of simplicity, i.e. eliminating null checks, and being able to write more streamlined algorithms that doesn't have to branch out for checks.

有哪些优点和每个款式的缺点在性能,简约,parallellism,适应未来发展等。

What are the pros and cons of each style in terms of performance, simplicity, parallellism, future-proofing, etc.

推荐答案

作为吸引力,因为世界上没有是,它确实present许多困难的许多现有的模式和结构。例如,考虑以下构造这将需要大的变化,如果不存在

As appealing as a world without null is, it does present a lot of difficulty for many existing patterns and constructs. For example consider the following constructs which would need major changes if null did not exist

  1. 创建引用类型的数组翼:新的对象[42] 。在现有的CLR世界阵列将充满这是非法的。数组语义需要改变这里颇有几分
  2. 这使得默认(T)有用的,只有当 T 是值类型。在引用类型或无约束的仿制药使用也不会被允许
  3. 字段的结构,它们是引用类型需要被禁止。值类型可以在其中方便地填补了引用类型的字段与的CLR 0初始化的今天。这是不可能在一个非空的世界,因此场卫生组织类型是结构的引用类型需要被禁止
  1. Creating an array of reference types ala: new object[42]. In the existing CLR world the arrays would be filled with null which is illegal. Array semantics would need to change quite a bit here
  2. It makes default(T) useful only when T is a value type. Using it on reference types or unconstrained generics wouldn't be allowed
  3. Fields in a struct which are a reference type need to be disallowed. A value type can be 0-initialized today in the CLR which conveniently fills fields of reference types with null. That wouldn't be possible in a non-null world hence fields whos type are reference types in struct's would need to be disallowed

以上都不是问题是无法解决的,但他们确实产生变化,真正的挑战是如何开发人员往往会考虑编码。个人而言,我希望C#和.NET的设计采用了消除零,但遗憾的是它并没有我想像这样上面就有点用它做的问题。

None of the above problems are unsolvable but they do result in changes that really challenge how developers tend to think about coding. Personally I wish C# and .Net was designed with the elimination of null but unfortunately it wasn't and I imagine problems like the above had a bit to do with it.

这篇关于为什么空在.NET中存在吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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