slandau
slandau

Reputation: 24102

Boolean properties in c#

This might be a dumb question, but the property below, will there ever be a situation where just getting it will cause an exception?

Like if I did something like bool value = this.BatchValuation; - but I hadn't set BatchValuation yet, will it just set value to null, or will it cause an exception?

public bool BatchValuation { get; set; }

Upvotes: 34

Views: 37373

Answers (6)

Konstantin
Konstantin

Reputation: 3957

Once I had a very weird situation, when auto-implemented property like this threw a NullReferenceException exception. The reason was Emit Mappper library (http://emitmapper.codeplex.com/).

This library (which is very good otherwise) uses Refleciton.Emit and, when it sees no public constructor for the type, it emits mapping call sites, which pass null reference as implied first argument of property/method (which ends up as "this" in all instance methods).

So the property was trying to change field value of a null reference (that is, I had this==null situation)!

Upvotes: 0

JKhuang
JKhuang

Reputation: 1553

As BrokenGlass said, the boolean values default to false,

you can test it by yourself, I provide a sample as below.

    static void Main()
    {
        // Create an object, but don't set attribute.
        Foo foo = new Foo();

        if (!foo.BatchValuation)
            Console.WriteLine("BatchValuation is False");

        else
            Console.WriteLine("BatchValuation is True");

        Console.ReadKey();

    }

}


// Test class.
public class Foo
{
    private bool _batchValuation;
    public Foo() { }
    public bool BatchValuation 
    {
        get { return _batchValuation; }
        set { _batchValuation = value; }
    }
}

Upvotes: 0

Eric Lippert
Eric Lippert

Reputation: 660533

This might be a dumb question

It is not.

in the property below, will there ever be a situation where just getting it will cause an exception?

Possibly, yes. For example, another thread could abort your thread while it was in the middle of fetching that property; that would appear to be an exception thrown by the property getter. Or, you could run out of stack space right at the moment where the property is called, and an out-of-stack exception could be thrown. Or, when you call the property for the first time, the jitter might start up and try to allocate virtual memory for the generated code, but you are all out of virtual address space, so an out of memory exception could be thrown.

Those are all extraordinarily unlikely, but they are all possible. You asked if there would ever be such a situation, not if it was likely.

If I hadn't set BatchValuation yet, will it just set value to null, or will it cause an exception?

Neither; it will default to false. Booleans are not nullable.

Upvotes: 55

Anthony Pegram
Anthony Pegram

Reputation: 126992

In the context of class properties or member fields, each field is initialized to a default value for the type when the class is created prior to the constructor being run. In the class

class Foo
{
    bool bar;
    string s;
    int item;
    public double Baz { get; set; } 
}

The first three fields are set to their initial values (false, null, and 0, respectively), and the auto-generated backing field for Baz is also set to 0.0d. It is not an error to access these fields/properties without explicit user-initialization, unlike accessing uninitialized locals. It is for locals that the compiler requires explicit initialization.

class Foo
{
   int bar;

   void M()
   {
       Console.WriteLine(bar); // not an error, bar is 0;
       bool someBool;
       Console.WriteLine(someBool); // use of uninitialized local variable
   }
}

Upvotes: 3

BrokenGlass
BrokenGlass

Reputation: 161012

boolean values default to false if you have not set them specifically. So no there will be no exception, the property will just return false.

Remember that this is a value type we are talking about, only variables for reference types can be set to null. Value types do have a default value, which is zero for numeric types and false for boolean.

Also see Default Values Table

Upvotes: 29

Joe
Joe

Reputation: 82654

no, Boolean defaults to false. So

bool something; // something is false

Upvotes: 3

Related Questions