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

  • Home
  • SEARCH
  • 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 694605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:52:25+00:00 2026-05-14T02:52:25+00:00

I have the following piece of code: class Foo { public Foo() { Bar

  • 0

I have the following piece of code:

class Foo
{

    public Foo()
    {
        Bar bar;
        if (null == bar)
        {

        }
    }
}

class Bar { }

Code gurus will already see that this gives an error. Bar might not be initialized before the if statement.

What is the value of bar? Shouldn’t it be null? Aren’t they set to null? (null pointer?)

  • 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-14T02:52:26+00:00Added an answer on May 14, 2026 at 2:52 am

    No, local variables don’t have a default value1. They have to be definitely assigned before you read them. This reduces the chance of you using a variable you think you’ve given a sensible value to, when actually it’s got some default value. This can’t be done for instance or static variables because you don’t know in what order methods will be called.

    See section 5.3 of the C# 3.0 spec for more details of definite assignment.

    Note that this has nothing to do with this being a reference type variable. This will fail to compile in the same way:

    int i;
    if (i == 0) // Nope, i isn't definitely assigned
    {
    }
    

    1 As far as the language is concerned, anyway… clearly the storage location in memory has something in it, but it’s irrelevant and implementation-specific. There is one way you can find out what that value is, by creating a method with an out parameter but then using IL to look at the value of that parameter within the method, without having given it another value. The CLR doesn’t mind that at all. You can then call that method passing in a not-definitely-assigned variable, and lo and behold you can detect the value – which is likely to be the “all zeroes” value basically.

    I suspect that the CLI specification does enforce local variables having a default value – but I’d have to check. Unless you’re doing evil things like the above, it shouldn’t matter to you in C#.

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

Sidebar

Related Questions

Let's say we have the following piece of code: public class Event { }
I have the following piece of code: class ICookable { public: virtual void CookMe
I have a piece of code like the following: public class ActivityHelper { public
I have following piece of code: class Test{ private: int id; public: Test(int v):id(v)
I have the following sample piece of code, public class test implements Runnable{ public
I have the following piece of code in Visual C++ 2005 : : class
If I have the following piece of Ruby code: class Blah def self.bleh @blih
I have the following piece of code pattern: void M1(string s, string v) {
I have the following piece of code which replaces template markers such as %POST_TITLE%
I have trouble with the following piece of code. When I go through with

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.