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 it's members.
Can this be done somehow like this (using initialization lists) or the same behavior can only be achieved using the constructor with Double
argument?
var x = new ImplementedInteface { 30.0 };
Can be done by:
var x = new ImplementedInteface { Value = 30.0 };
var x = new ImplementedInterface() { Value = 30.0 };
The only way to achieve what you're after is if your class implements IEnumerable<T>
and has an Add method:
public class MyClass : IEnumerable<double>
{
public void Add(double x){}
}
Then you can do:
MyClass mc = new MyClass { 20.0 };
Obviously that's not what you want, because that doesn't set your Value
and it allows you to add multiple values:
MyClass mc = new MyClass { 20.0, 30.0 , 40.0 };
Just go with the standard object initializes like others have pointed out:
var x = new ImplementedInterface() { Value = 30.0 };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With