views:

714

answers:

8

This code compiles successfully, but I think it should fail to compile. Also, when you run it you get a NullReferenceException. The missing code is the "new Bar" in the initialization of the Bar property.

class Bar
{
    public string Name { get; set; }
}

class Foo
{
    public Bar Bar { get; set; }
}


class Program
{
    static void Main(string[] args)
    {
        var foo = new Foo
                      {
                          Bar = { Name = "Hello" }
                      };
    }
}

Is this a known bug?

+38  A: 

Why do you think it should fail to compile? It is nested object initializer syntax, and it is the responsibility of the client code to provide a valid value for initialization.

From the documentation:

C# spec 7.5.10.2 "Object initializers"

A member initializer that specifies an object initializer after the equals sign is a nested object initializer, i.e. an initialization of an embedded object. Instead of assigning a new value to the field or property, the assignments in the nested object initializer are treated as assignments to members of the field or property

desco
Wow, I didn't know it! Thanks a lot.
gaearon
Great description of the issue.
Chris Lively
Thank you. Good answer!
Edmondo Pentangelo
A: 

Why is this a "bug"?

Muad'Dib
how is this an "answer"? :)
fearofawhackplanet
+3  A: 

The new is unecessary in an object initializer:

object-creation-expression:
    new   type   (   argument-list(opt)   )   object-or-collection-initializer(opt) 
    new   type   object-or-collection-initializer

object-or-collection-initializer:
    object-initializer
    collection-initializer

object-initializer:
    {   member-initializer-list(opt)   }
    {   member-initializer-list   ,   }

initializer-value:
    expression
    object-or-collection-initializer

It's that last one that is most important. It represents the right-hand-side of your property = value syntax. This means that the right-hand-side can be a normal c# expression (with a new operator) or another initalizer. In which case, all you need are the opening and closing braces.

Kirk Woll
Yeah, understood about the new not being necessary but where is the type for bar?
Maxm007
It's inferred because the type is known based on the property being initialized.
Kirk Woll
so if it inferred it its a bar, and then assumes its New Bar , why does it throw nullrefexception.
Maxm007
Ahhh got it. If you preinitialize bar it will work. I did not know you could use the initializer for existing instances
Maxm007
+1  A: 

Bar is a property of Foo, so it is allowing you to access it and assigning a name property at compile time, but at run time it checks for the valid instance of Bar which is not present so throwing null reference exception, it will be the case with any C# version.

saurabh
+15  A: 

No this is not a bug.

If you want it to run you either put a new before Bar (just like you did for Foo before the initializer) or you create the Bar object in Foo's constructor.

The object initializer is essentially just syntactic sugar.

This:

var foo = new Foo
            {
                Bar = { Name = "Hello" }
            };

Is exactly the same as this:

var foo = new Foo();
foo.Bar.Name = "Hello"; 
Isak Savo
+2  A: 

If you change your code to the following equivalent, you will also get a runtime error of a NullReferenceException instead of a compile time error/warning.

static void Main(string[] args) {
    Foo foo2 = new Foo();
    foo2.Bar.Name = "test";
}

The effect is the same, Bar is never properly initialized. Now, from a compiler writers perspective it is extremely difficult to determine in all cases as to whether Bar was properly initialized prior to use.

Chris Lively
+2  A: 
...
 Bar = { Name = "Hello"}
...

means: Foo.Bar.Name="Hello" not: {Foo.Bar=new Bar(); Foo.Bar.Name="Hello";}

This will compile and will not throw any exception, so it's not a bug, you're just initializing an unexisting object:

class Bar
{
    public string Name;
}
class Foo
{
private Bar _bar = new Bar();
public Bar Bar
{
  get { return _bar; }
  set { _bar = value; }
}
}
class Program
{
 static void Main(string[] args)
 {
  Foo foo = new Foo
  {
   Bar = { Name = "Hello"}
  };
 }
}
Andy
A: 

I create a working sample.

Its easy, only add a "new Bar()" an it work fine


class Bar
{
    public string Name { get; set; }
}

class Foo
{
    public Bar Bar { get; set; }
}


class Program
{
    static void Main(string[] args)
    {
        var foo = new Foo
                      {
                          Bar = new Bar() { Name = "Hello" }
                      };

        Console.WriteLine(foo.Bar.Name);
        Console.ReadLine();
    }
}
Andreas Hoffmann