Search code examples
c#initialization

Single-value object initializer


This might be lame, but here:

public interface Interface<T>
{
    T Value { get; }
}

public class InterfaceProxy<T> : Interface<T>
{
    public T Value { get; set; }
}

public class ImplementedInterface: InterfaceProxy<Double> {}

Now I want to create an instance of the ImplementedInterface and initialize its members.

Can this be done like this (using an object initializer) or can this only be achieved by using a constructor with Double argument?

var x = new ImplementedInterface { 30.0 };

Solution

  • Can be done by:

    var x = new ImplementedInteface { Value = 30.0 };