Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign to interface array initializator compiles but why?

Tags:

c#

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; }
    }
}
like image 338
Mateusz Avatar asked Dec 19 '13 14:12

Mateusz


2 Answers

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.

like image 141
Sriram Sakthivel Avatar answered Nov 15 '22 18:11

Sriram Sakthivel


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 } };
like image 23
dcastro Avatar answered Nov 15 '22 16:11

dcastro