Assignment evaluation in C#

09/13/08

Permalink 05:52:17 pm, by truewill Email , 158 words, 56 views   English (US)
Categories: C#

Assignment evaluation in C#

I debugged a very interesting problem today. Here’s an example C# 3 console application that reproduces the bug. This is contrived; the actual case was more complex.

using System;

namespace AssignmentEvaluationTest
{
    public class Test
    {
        public static void Main()
        {
            Test currentTest = new Test();
            currentTest.HoldItem();
        }

        public string Item
        {
            get
            {
                InsureItemHolderInitialized();
                return "An item";
            }
        }

        public void HoldItem()
        {
            _itemHolder.HeldItem = Item;  // NullReference
        }

        private void InsureItemHolderInitialized()
        {
            if (_itemHolder == null)
            {
                _itemHolder = new ItemHolder();
                Console.WriteLine("_itemHolder initialized");
            }
        }

        private ItemHolder _itemHolder;

        internal class ItemHolder
        {
            public string HeldItem { get; set; }
        }
    }
}

This will print the initialized message, then throw a NullReferenceException at the commented line.

Apparently the runtime takes the line

  _itemHolder.HeldItem = Item;

and evaluates _itemHolder first (null). It then evaluates Item, which has a side effect of setting _itemHolder. After that, it tries to dereference the cached value of _itemHolder (which remains null) to set HeldItem.

Introducing a variable to reference Item first is one easy fix; refactoring the code to simplify it is another.

Comments, Pingbacks:

Comment from: Jon Skeet [Visitor] Email · http://msmvps.com/jon.skeet
This surprised me when I read it - but it's definitely acting as the spec dictates. See section 7.16.1 of the C# 3 spec for the grisly details.

Nice catch though - it's not often I learn a bit more about evaluation order in C# :)
PermalinkPermalink 10/05/08 @ 14:06

Leave a comment:

Your email address will not be displayed on this site.
Your URL will be displayed.

Allowed XHTML tags: <p, ul, ol, li, dl, dt, dd, address, blockquote, ins, del, span, bdo, br, em, strong, dfn, code, samp, kdb, var, cite, abbr, acronym, q, sub, sup, tt, i, b, big, small>
(Line breaks become <br />)
(Set cookies for name, email and url)
(Allow users to contact you through a message form (your email will NOT be displayed.))
The name truewill is composed of two other words. What is the SECOND word?

Development Central

Development Central is the blog of Bill Sorensen, a professional software developer. Much of this will relate to C#, .NET, and OOP in general.

Disclaimer
These postings are provided "AS IS" with no warranties and confer no rights.

Search

Categories

Linkblog

b2evolution

contributors

XML Feeds

What is RSS?

Who's Online?

  • Guest Users: 2

powered by b2evolution free blog software