.NET Forum / Languages / C# / March 2008
What is equivalent of 'null'
|
|
Thread rating:  |
COHENMARVIN@lycos.com - 27 Feb 2008 18:11 GMT 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
zeero - 27 Feb 2008 18:21 GMT 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
> 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 Nicholas Paldino [.NET/C# MVP] - 27 Feb 2008 18:21 GMT 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'.
 Signature - Nicholas Paldino [.NET/C# MVP] - mvp@spam.guard.caspershouse.com
> 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 Cowboy (Gregory A. Beamer) - 27 Feb 2008 18:39 GMT 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;
 Signature Gregory A. Beamer MVP, MCP: +I, SE, SD, DBA
*************************************************
| Think outside the box! *************************************************
> 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 Jon Skeet [C# MVP] - 27 Feb 2008 19:12 GMT > There is a null: > [quoted text clipped - 3 lines] > 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?
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet World class .NET training in the UK: http://iterativetraining.co.uk
Cowboy (Gregory A. Beamer) - 28 Feb 2008 15:21 GMT 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.
 Signature Gregory A. Beamer MVP, MCP: +I, SE, SD, DBA
*************************************************
| Think outside the box! *************************************************
>> There is a null: >> [quoted text clipped - 13 lines] > > What are you suggesting can't be done? Jon Skeet [C# MVP] - 28 Feb 2008 15:27 GMT On Feb 28, 3:21 pm, "Cowboy \(Gregory A. Beamer\)" <NoSpamMgbwo...@comcast.netNoSpamM> wrote:
> Yep. I thought about that later and realized I was being overstressed, and, > therefore, stupid. [quoted text clipped - 5 lines] > > 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
Cowboy (Gregory A. Beamer) - 28 Feb 2008 15:37 GMT 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.
 Signature Gregory A. Beamer MVP, MCP: +I, SE, SD, DBA
*************************************************
| Think outside the box! *************************************************
> On Feb 28, 3:21 pm, "Cowboy \(Gregory A. Beamer\)" > <NoSpamMgbwo...@comcast.netNoSpamM> wrote: [quoted text clipped - 23 lines] > > Jon Jon Skeet [C# MVP] - 28 Feb 2008 15:57 GMT On Feb 28, 3:37 pm, "Cowboy \(Gregory A. Beamer\)" <NoSpamMgbwo...@comcast.netNoSpamM> wrote:
> 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
Cowboy (Gregory A. Beamer) - 28 Feb 2008 16:23 GMT > On Feb 28, 3:37 pm, "Cowboy \(Gregory A. Beamer\)" > <NoSpamMgbwo...@comcast.netNoSpamM> wrote: [quoted text clipped - 5 lines] > 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.
>> Thanks for watchdogging. I am using the newsgroups as a stress release >> and [quoted text clipped - 6 lines] > (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. :-)
 Signature Gregory A. Beamer MVP, MCP: +I, SE, SD, DBA
*************************************************
| Think outside the box! *************************************************
Jon Skeet [C# MVP] - 28 Feb 2008 16:40 GMT > > 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 [quoted text clipped - 8 lines] > 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.
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet World class .NET training in the UK: http://iterativetraining.co.uk
RobinS - 06 Mar 2008 06:00 GMT > On Feb 28, 3:37 pm, "Cowboy \(Gregory A. Beamer\)" > <NoSpamMgbwo...@comcast.netNoSpamM> wrote: [quoted text clipped - 22 lines] > > 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.
KWienhold - 06 Mar 2008 07:06 GMT > 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
Jon Skeet [C# MVP] - 06 Mar 2008 07:43 GMT > > 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 [quoted text clipped - 5 lines] > 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...
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet World class .NET training in the UK: http://iterativetraining.co.uk
Cor Ligthert[MVP] - 28 Feb 2008 18:32 GMT 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
Jon Skeet [C# MVP] - 28 Feb 2008 19:11 GMT > 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 :)
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet World class .NET training in the UK: http://iterativetraining.co.uk
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|