.NET Questions (CLOSED)

Questions and Answers on any aspect of .NET. Now closed.

This discussion group is now closed.

Have a question about .NET development? Try stackoverflow.com, a worldwide community of great developers asking and answering questions 24 hours a day.

The archives of .NET Questions contain years of Q&A. Even older .NET Questions are still online, too.

New Bug in VS 2005 SP1

Has anyone else experienced this? The following code now generates a warning saying "Unreachable code detected". This is new since applying VS 2005 SP1. The warning is given on the throw statement below.

public DateTime CurrentTime
{
    set
    {
        if (value == null)
        {
            throw new ArgumentNullException();  // Error here.
        }
        currentTime = value;
    }
}

It implies that the value passed in to the setter could never be null which is obviously not true. I just started getting this warning after upgrading with SP1.
Turtle Rustler
Saturday, January 20, 2007
 
 
DateTime is a struct (not a class) and therefore can't be null.
Christopher Wells Send private email
Saturday, January 20, 2007
 
 
You could always try:

using DateTime = System.Nullable<System.DateTime>;

If that's the behaviour you actually want. :)
Thom Lawrence Send private email
Saturday, January 20, 2007
 
 
A DateTime can never be null, since it's a value type!
Ionuţ Bizău
Saturday, January 20, 2007
 
 
Yeah, I always forget that. I split my time between Java and C# and I always forget that DateTime is a struct instead of an object.

Thanks!
Turtle Rustler
Saturday, January 20, 2007
 
 
Add a question mark after the DateTime declaration in the property:

public DateTime? MyProperty
{
    ...
}

Then the code will compile - the reason it doesn't is because DateTime without the nullable modifier will never be null, so your throw line is unreachable - it's not a bug.
Sam Piper Send private email
Monday, January 22, 2007
 
 
I can't make it nullable and as I said above I understand why it gives me the warning now.

But couldn't I also complain that it SHOULD be generating an actual compiler error (even in older versions)? Is it even legal to compare a DateTime to null? For example, the following code generates the error "Operator '==' cannot be applied to operands of type 'System.DateTime' and 'object'". So why does comparing directly to null not give me the same error? I'm just curious.

DateTime d;
object o = null;
if (d == o)  // Error here
{
}
Turtle Rustler
Monday, January 22, 2007
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz