Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7570087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:19:52+00:00 2026-05-30T15:19:52+00:00

Obviously the title is somewhat tongue in cheek , but I’ve checked and double

  • 0

Obviously the title is somewhat tongue in cheek, but I’ve checked and double checked and I can’t see the error in my logic.

The compiler complains that variable parsed might not be initialized in the return statement. I don’t agree. Which of us is wrong, and why?

public DateTime? Test(string nextDate)
{
    DateTime parsed;

    if (nextDate != "TBC" && !DateTime.TryParse(nextDate, out parsed))
    {
        throw new Exception();
    }

    if (nextDate == "TBC")
        return null;

    return parsed;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:19:53+00:00Added an answer on May 30, 2026 at 3:19 pm

    No, the compiler isn’t broken at all.

    The compiler isn’t meant to be able to tell that

    if (nextDate != "TBC")
    

    and

    if (nextDate == "TBC")
    

    are mutually exclusive. It doesn’t try to make any connection between the two conditions. So it can’t tell that you’ll definitely have called DateTime.TryParse(nextDate, out parsed) if you get as far as return parsed;.

    Basically, the compiler follows relatively simple rules to determine definite assignment (and reachability). Simple rules are easy to reason about, easy to implement, and easy to code to.

    Fortunately, you can make your code simpler and make it compile at the same time:

    public DateTime? Test(string nextDate)
    {
        if (nextDate == "TBC")
        {
            return null;
        }
    
        DateTime parsed;    
        if (!DateTime.TryParse(nextDate, out parsed))
        {
            throw new Exception();
        }
        return parsed;
    }
    

    Now we’re dealing with the “special case” of “TBD” in one place right at the start – and then we can ignore that special case for the rest of the code and call TryParse unconditionally, leaving parsed definitely-assigned.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Horrible title, I know. I want to have some kind of wordwrap, but obviously
The title obviously states it : I can't make the second-level cache work for
Obviously the query in the title does not work, but it might illustrate in
Obviously, the title has code that can't be interpreted. An uninformative SyntaxError: invalid syntax
Don't really know how to formulate the title, but it should be pretty obvious
Obviously best-case is O(n), but apparently the worst-case is O(n 2 ), which I
The title of this question might be a bit off but its the closest
I know this is a bad title for my question but what i am
Sorry for the bad title, but... I tried git flow on a project I
Obviously modifying it would be out of the question. But you would think just

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.