What is equivalent of 'null'

C

COHENMARVIN

I'm a VB programmer learning C#. One thing I don't know is how to
test whether an object is 'null' or not. There is no 'null' keyword
in C#.
Why is that?
CM
 
Z

zeero

What do you mean there is no "null" in C#? VB has "nothing", C# has "null".
Try the code below:
class Thing
{

}
// than somewhere ....

Thing thing = null;
if (thing == null)
{
MessageBox.Show("I'm null");

}
else
{
MessageBox.Show("I dont believe you");

}

When the messagebox code runs, you'll should see the message "I'm null".

...ab
 
N

Nicholas Paldino [.NET/C# MVP]

CM,

I think you have it the other way around. There most definitely is a
null keyword in C#, but not one in VB. In VB, the equivalent is 'Nothing'.
 
C

Cowboy \(Gregory A. Beamer\)

There is a null:

if(var == null)
//Do something

If you are talking nullable types, you have to test against the type, not
against null, as nullable types are a bit of a kludge. There are also
certain types that cannot be null, unless made with a nullable type.

//default = null, but not the keyword
int? nullableInt;

//default = 0
int nonNullableint;

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

*************************************************
| Think outside the box!
|
*************************************************
 
J

Jon Skeet [C# MVP]

Cowboy (Gregory A. Beamer) said:
There is a null:

if(var == null)
//Do something

If you are talking nullable types, you have to test against the type, not
against null, as nullable types are a bit of a kludge.

No, you can test against null with no problem:

int? nullableInt = null; // That's fine
if (nullableInt==null) // That's fine too
{
...
}

What are you suggesting can't be done?
 
C

Cowboy \(Gregory A. Beamer\)

Yep. I thought about that later and realized I was being overstressed, and,
therefore, stupid.

if(!nullableInt.HasValue)
{
}

is the correct value for nullable types. The rest of the answer was, in
fact, correct, as many types have no concept of null. An int is the perfect
example here.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

*************************************************
| Think outside the box!
|
*************************************************
 
J

Jon Skeet [C# MVP]

Yep. I thought about that later and realized I was being overstressed, and,
therefore, stupid.

if(!nullableInt.HasValue)
{

}

is the correct value for nullable types.

It certainly gives the right result. I think I'd use

if (nullableInt==null)

for the sake of idiom, but each to their own :)
The rest of the answer was, in
fact, correct, as many types have no concept of null. An int is the perfect
example here.

Sure - but for every non-nullable type there's a nullable equivalent.

Jon
 
C

Cowboy \(Gregory A. Beamer\)

I agree completely on the "for every non-nullable", but it is a bit of a
kludge in many ways. :)

Thanks for watchdogging. I am using the newsgroups as a stress release and
sometimes the stress is way too high that my brain does not work. Between
the job and Miranda's cancer, it has been a rough few months. Sincerely,
thanks for keeping me honest on this one.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

*************************************************
| Think outside the box!
|
*************************************************
 
J

Jon Skeet [C# MVP]

I agree completely on the "for every non-nullable", but it is a bit of a
kludge in many ways. :)

Sort of. I can't think of a more elegant solution though. Admittedly
it's slightly worrying that different languages have different
operator behaviour - equality of nullable types in VB is different to
that in C#, for example.

What I'd like to see is non-nullable reference types. That could be
useful in a number of ways - although given the large body of code
already out there, it's probably too late.
Thanks for watchdogging. I am using the newsgroups as a stress release and
sometimes the stress is way too high that my brain does not work. Between
the job and Miranda's cancer, it has been a rough few months. Sincerely,
thanks for keeping me honest on this one.

So long as you'll do me the favour in return :)

(I worry sometimes that people don't want to correct MVPs. Everyone is
highly fallible, IME.)

Jon
 
C

Cowboy \(Gregory A. Beamer\)

Jon Skeet said:
Sort of. I can't think of a more elegant solution though. Admittedly
it's slightly worrying that different languages have different
operator behaviour - equality of nullable types in VB is different to
that in C#, for example.

This is a bigger issue than many think, and not just on nullable types. When
you bounce from language to language, you often find that the different way
features are handled causes you to get into situations where you end up
answering a question incorrectly for one answer that would be correct in
another.

I spend so little time in VB any more, but there are occasions where someone
has me examine some VB code and I have to.
What I'd like to see is non-nullable reference types. That could be
useful in a number of ways - although given the large body of code
already out there, it's probably too late.

I am not sure it would do much for me, but perhaps I will get more time to
ponder it later. I have, however, set up reference types that had default
values, so perhaps that would be an reason for a non-nullable reference
types.

NOTE: I am not fond of hard coded initialization of values, but it was a
better option than the client had done intially, which was chain behavior to
a constructor.
So long as you'll do me the favour in return :)

(I worry sometimes that people don't want to correct MVPs. Everyone is
highly fallible, IME.)


When I look back a year ago at some code, I think "man I sucked then". The
truth is, I will probably say the same in another six months to a year. And,
this is a good thing, as it means I am growing. The day I look at old code
and say "man, that is the best I have ever done" is the day I should go out
to pasture. :)

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

*************************************************
| Think outside the box!
|
*************************************************
 
J

Jon Skeet [C# MVP]

Cowboy (Gregory A. Beamer) said:
I am not sure it would do much for me, but perhaps I will get more time to
ponder it later. I have, however, set up reference types that had default
values, so perhaps that would be an reason for a non-nullable reference
types.

NOTE: I am not fond of hard coded initialization of values, but it was a
better option than the client had done intially, which was chain behavior to
a constructor.

Just think of everywhere that you currently have to specify that a
parameter can't be null, or that a method will never return null. Now
consider if instead you could write:

void Foo(string! x)

or

string! Foo()

Now, that could be done in a simplistic way by adding rules to the
compiler to insert checks which throw exceptions if these conditions
aren't met - but it could also be done in a deeper fashion such that it
became part of the type system. Making it part of the type system at
this point of .NET's life is probably infeasible - but it would be
interesting to know what the situation would have been like if it had
been like that to start with.
 
C

Cor Ligthert[MVP]

CM,

One of the two meanings of Noting in VB in C# is null. That is the
equivalent for reference types where if (x == null) means that x is not
instanced.

For the other meaning of nothing in VB (default value by value types) is
AFAIK no equivalent, you have always to set a new value type to its starting
value in C#. (I hope that in that there is nothing changed lately).

However I have seen that C# guys/girls have often trouble with this meaning
of Nothing by value types from VB.Net.
(Not that they don't know this, from Jon I am almost sure that he know
this).

Cor
 
J

Jon Skeet [C# MVP]

Cor Ligthert said:
One of the two meanings of Noting in VB in C# is null. That is the
equivalent for reference types where if (x == null) means that x is not
instanced.

Well, it means that the value of x is the null reference. It's worth
remembering that x itself is a variable, never an object. Its value is
never an object, either - only a reference, either to an object or
null. (Assuming that the type of the variable is a reference type, of
course.)
For the other meaning of nothing in VB (default value by value types) is
AFAIK no equivalent, you have always to set a new value type to its starting
value in C#. (I hope that in that there is nothing changed lately).

Not "lately", no - about 25 months ago :)

default(SomeType) is the default value of any type (value type or
reference type, or indeed nullable type).
However I have seen that C# guys/girls have often trouble with this meaning
of Nothing by value types from VB.Net.
(Not that they don't know this, from Jon I am almost sure that he know
this).

Yup :)
 
R

RobinS

Jon Skeet said:
Sort of. I can't think of a more elegant solution though. Admittedly
it's slightly worrying that different languages have different
operator behaviour - equality of nullable types in VB is different to
that in C#, for example.

What I'd like to see is non-nullable reference types. That could be
useful in a number of ways - although given the large body of code
already out there, it's probably too late.


So long as you'll do me the favour in return :)

(I worry sometimes that people don't want to correct MVPs. Everyone is
highly fallible, IME.)

Jon

Oh, don't worry, Jon. We're all just sitting here waiting with bated breath
for you to make a mistake so we can correct you. <g>

Still waiting...


Still waiting... Sigh.

RobinS.
GoldMail, Inc.
 
K

KWienhold

What I'd like to see is non-nullable reference types. That could be
useful in a number of ways - although given the large body of code
already out there, it's probably too late.

I think that would be a really useful feature, in addition to
parameters and return values, it would also be nice to be able to
specify it for properties.
Maybe you should add this to your C# 4 wishlist, who knows, they might
listen ;)

Kevin Wienhold
 
J

Jon Skeet [C# MVP]

KWienhold said:
I think that would be a really useful feature, in addition to
parameters and return values, it would also be nice to be able to
specify it for properties.
Maybe you should add this to your C# 4 wishlist, who knows, they might
listen ;)

I'm slightly surprised that isn't in my list already, actually... maybe
I've got it in one of the comments somewhere...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top