Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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#.
Posted
Updated 17-May-18 5:30am
v3

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;
 
Share this answer
 
v2
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.
 
Share this answer
 
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.
 
Share this answer
 
NULL is the old macro used in C-language. For C++ you should use nullptr. For C# use null.
 
Share this answer
 
Comments
CPallini 17-May-18 9:01am    
Go King Richard, go!
5.
Richard MacCutchan 17-May-18 9:49am    
Only a prince now :(
If you just want to initialize a variable to its default value:
Default value expressions (C# Programming Guide) | Microsoft Docs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900