Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - null vs "Could not evaluate expression"

I have code like this:

    private Box mCurBox;

    public Box CurBox
    {
        get { return mCurBox; }
        set
        {
            if (mCurBox != value)
            {
                mCurBox = value;
            }
        }
    }

When mCurBox is null then CurBox the debugger says "Could not be evaluated". If it knows that the value underneath is null then how come it can't figure it out?

like image 921
Vaccano Avatar asked Apr 21 '10 21:04

Vaccano


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What does %c mean in C?

%d is used to print decimal(integer) number ,while %c is used to print character . If you try to print a character with %d format the computer will print the ASCII code of the character.

Is C and C++ same?

While C and C++ may sound similar, their features and usage differ. C is a procedural programming language that support objects and classes. On the other hand C++ is an enhanced version of C programming with object-oriented programming support.

What is C full form?

Originally Answered: What is the full form of C ? C - Compiler . C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972.


2 Answers

The debugger can get gimpy now and then. But the expected case of getting "Could not be evaluated" is a Release build. Simple properties like this get optimized away by the JIT compiler. The property getter code would not even be present.

like image 41
Hans Passant Avatar answered Sep 19 '22 01:09

Hans Passant


That's because you have not defined mCurBox to be anything by default, so the compiler flags this as undefined behaviour.
You need to initialize mCurBox as null, either in the very same line you define it, or in a constructor.
In general, it's good practice to initalize reference types to null if you don't assign something to them when defining them.
Also, seeing as you're just assigning and retrieving, you can easily use auto properties.

like image 182
Rubys Avatar answered Sep 22 '22 01:09

Rubys