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 6375361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:36:19+00:00 2026-05-25T01:36:19+00:00

This is something that I have wondered about for a while now. I have

  • 0

This is something that I have wondered about for a while now. I have browsed a bunch of questions containing the error below in their title but couldn’t find one that explains this case.

First look at this example:

private void test() {
    string errorMessage;
    bool isOK = SomeClassWithBusinessRules.VerifySomeStuff(idOfStuffToVerify, ref errorMessage);
    if (!isOK)
        throw new BusinessException(errorMessage ?? "Some error occured.");
}

If you compile this the compiler will complain with this message:

Error 2 Use of unassigned local variable ‘errorMessage’

Changing the variable initializer to null will make it go away.

This will compile:

private void test() {
    string errorMessage = null;
    bool isOK = SomeClassWithBusinessRules.VerifySomeStuff(idOfStuffToVerify, ref errorMessage);
    if (!isOK)
        throw new BusinessException(errorMessage ?? "Some error occured.");
}

So why do we get the compilation error?

  • 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-25T01:36:19+00:00Added an answer on May 25, 2026 at 1:36 am

    When you pass it to VerifySomeStuff, you are specifying ref but it does not yet have a value. That is not legal, as VerifySomeStuff could choose to read the value, which does not have a defined value at this point. Assigning null satisfies the definite assignment requirement. An alternative would be out:

    string errorMessage;
    bool isOK = SomeClassWithBusinessRules.VerifySomeStuff(id, out errorMessage);
    if (!isOK)
        throw new BusinessException(errorMessage ?? "Some error occured.");
    

    which would be legal (but requires changes to VerifySomeStuff, since the signature must be changed and it is now required to assign a value to the parameter before exit (unless an exception occurs)). From the code shown, it you might choose for VerifySomeStuff to assign null to the parameter if there is no error.

    Of course, if the bool and string then duplicate the “was there a problem” purpose, you could also use:

    string errorMessage = SomeClassWithBusinessRules.VerifySomeStuff(id);
    bool isOK = errorMessage == null;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is something that I have always wondered about, but never bothered to profile.
This is something I've always wondered about. I understand that horizontal scaling is about
This is something I've wondered about in a couple of frameworks that I've messed
I have wondered for a while now how to this. If I want to
This is something that I have never fully grasped in .NET as to the
I want to parse a html content that have something like this: <div id=sometext>Lorem<br>
I have something that looks like this: As you can see, Blambo is a
In my code, I have something that looks like this: @implementation MyClass - (id)
I have XML that looks something like this: <Root xmlns=http://widgetspecA.com/ns> ...any... <WidgetBox> <A/> <B/>
I have just found this great tutorial as it is something that I need.

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.