Mateusz
Mateusz

Reputation: 2317

Assign to interface array initializator compiles but why?

Today I was thinking it would be neat to make anonymous object which is type of some interface, and I have seen on SO that I am not only one.

Before I started checking out what happens I wrote some code like the one below. To my amusement it compiled, I am using .net framework 4 and I know there is no way to do anonymous objects implement interface, but I have not seen complaint from VS about this code.

Even better when I put braces intelisense is finding "Property" of my interface, just like it would be valid code.

Why is this piece compiling and when ran it is giving null reference exception?

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            Holder holder = new Holder { someInterface = { Property = 1 } };
            Console.WriteLine(holder.someInterface.Property);
        }
    }

    class Holder
    {
        public ISomeInterface someInterface{get; set;}
    }

    interface ISomeInterface
    {
        int Property { get; set; }
    }
}

Upvotes: 10

Views: 201

Answers (4)

nawfal
nawfal

Reputation: 73301

It's worth noting why the compiler lets this behaviour. The reason being someInterface need not be null always. This is what object initializer syntax is translated to:

Holder temp = new Holder(); //creates temp object calling default constructor
temp.someInterface = yourValue;
holder = temp; //finally assigned back to your variable.

In your case someInterface is left uninitialized. But it need not be the case if you have your empty constructor initializing someinterface correctly.

class Holder
{
    public Holder()
    {
       someInterface = new Class();
    }

    public ISomeInterface someInterface{get; set;}
}

Now this works:

Holder holder = new Holder { someInterface = { Property = 1 } };

Upvotes: 1

Sriram Sakthivel
Sriram Sakthivel

Reputation: 73502

Holder holder = new Holder { someInterface = { Property = 1 } };//<--Note you missed new keyword

Above line is equal to

Holder temp = new Holder();
temp.someInterface.Property = 1;
Holder holder = temp;// <--Here someInterface is null so you get null reference exception. 

This should be something like

Holder holder = new Holder { someInterface = new SomeClass(){ Property = 1 } };//<--Note the new keyword here

Note: Your code never introduced "Anonymous Type" It is an "Object Initializer".

When you use ObjectInitializer syntax with new keyword it means you're setting something, when you use ObjectInitializer syntax without new keyword it means you're reading something.

Upvotes: 10

dcastro
dcastro

Reputation: 68750

it would be neat to make anonymous object which is type of some interface,

I know there is no way to do anonymous objects implement interface, but I have not seen complaint from VS about this code.

The problem is, you're assuming that the following code creates a new instance of an anonymous type

new Holder { someInterface = { Property = 1 } };

{ Property = 1 } does not create a new instance of an anonymous type - this is an object initializer.

If you do replace your code with a proper instantiation of an anonymous type, then the compiler will complain that the instance cannot be implicitly converted to ISomeInterface, like you expected.

new Holder { someInterface = new { Property = 1 } };

Upvotes: 2

liran63
liran63

Reputation: 1336

You have to assign an instance to someInterface in Holder constractor; otherwise, it will be null.

Upvotes: 0

Related Questions