NULL和null之间的区别 [英] Difference between NULL and null

查看:136
本文介绍了NULL和null之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#中C ++的NULL等价是什么?



我尝试过:



Null有一个默认值0.它可以用作bool,integer,pointer。但是当我们没有指向某个对象时,会使用C#null。我不确定在C#中是否可以使用null代替NULL。

解决方案

没有可以完全以相同方式使用的等价物。你分配的内容取决于你想如何使用它:



o是一个值为0的int:

对象o = 0; 



o是一个值为false的bool:

 object o = false; 



o是一个未分配的对象:

 object o = null; 


< C ++中的blockquote> NULL 只是一个产生零指针的宏(内存地址为零,实际上没有该变量的地址)。

您的问题的答案并不简单:在C#中使用 null 表示没有对任何对象的引用(对于托管代码);如果你试图操纵来自C#的指针(除非你真的知道你在做什么,我实际上不推荐),你可以使用 IntPtr.Zero 代替。

Quote:

Null有一个默认值0

粗略地看一下NULL - cppreference.com [ ^ ]。





C ++ C#是相当不同的编程语言。因此,您可能会将苹果与橙子进行比较。


what is the equivalent of NULL of C++ in C#?

What I have tried:

Null has a default value 0. it can be used as bool,integer, pointer. But C# null is used when we are not pointing to some object. I am not sure whether null can be used in place of NULL in C#.

解决方案

There's no equivalent that can be used in exactly the same way. What you assign depends on how you want to use it:

o is an int with value 0:

object o = 0;


o is a bool with value false:

object o = false;


o is an unassigned object:

object o = null;


NULL in C++ is just a macro which yields to a zero-pointer (a memory address of zero, meaning no address for that variable really).
The answer to your question is not straightforward: use null in C# to denote the absence of a reference to any object (for managed code); if you are trying to manipulate pointers from C# (which I actually do not recommend unless you really know what you are doing), you could use IntPtr.Zero instead.


Quote:

Null has a default value 0

Roughly, see NULL - cppreference.com[^].


C++ and C# are rather different programming languages. So you risk to compare apple with oranges.


这篇关于NULL和null之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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