C#初始化问题

Yip*_*Yay 6 c# initialization

这可能是蹩脚的,但在这里:

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

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

public class ImplementedInterface: InterfaceProxy<Double> {}
Run Code Online (Sandbox Code Playgroud)

现在我想创建一个实例ImplementedInterface并初始化它的成员.

可以这样做(使用初始化列表)或只能使用带Double参数的构造函数实现相同的行为吗?

var x = new ImplementedInteface { 30.0 };
Run Code Online (Sandbox Code Playgroud)

Sae*_*iri 7

可以通过以下方式完成

var x = new ImplementedInteface { Value = 30.0 };
Run Code Online (Sandbox Code Playgroud)

  • @Marcel Gheorghita,你可以跳过这个括号.没必要. (2认同)