C#初始化问题

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 };

#0

Can be done by:

var x = new ImplementedInteface { Value = 30.0 };

#1

You should be able to do:

var x = new ImplementedInterface {Value = 30.0};

#2

var x = new ImplementedInterface() { Value = 30.0 };

#3

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 };

#4

var instance = new ImplementedInterface { Value = 30.0 }; will work. However, this isn't really the same set of operations as C++ initializer lists -- this is an object initializer. It initializes the new instance via the default constructor and then invokes the property setters for each property.

In other words, the object is constructed before the property setters run. If you want the values for the properties set before construction of ImplementedInterface completes, you'd have to write a constructor, as you noted. This distinction in behavior usually doesn't matter, but it's good to be aware of.

#5

I am not sure if you have a special reason to use the interfaces that way but the following code might work for you.

public class ImplementedInterface2 : List<double> { }

 public class test
    {
        public void x()
        {
            var x = new ImplementedInterface2() { 30.0 };
        }
    }

#6

var x = new ImplementedInterface { Value = 30.0 };

#7

You can definitely use an initialization list, but you have to specify what 30.0 is (this is true for any initialization list, not just the code you have):

var x = new ImplementedInteface { Value=30.0 };

推荐文章

如何将文本放在顶部的EditText小部件中?

如何将文本放在顶部的EditText小部件中?

推荐文章

进程记忆的开始是什么意思

进程记忆的开始是什么意思

推荐文章

将AST编译回源代码

将AST编译回源代码

推荐文章

实体管理器刷新

实体管理器刷新

推荐文章

向JMenu添加快捷方式

向JMenu添加快捷方式

推荐文章

SQL Server 2008 R2定期不接受连接

SQL Server 2008 R2定期不接受连接

推荐文章

使用Moq模拟对象,在进行单元测试时使用Ninject

使用Moq模拟对象,在进行单元测试时使用Ninject

推荐文章

如何更改列表项的背景色

如何更改列表项的背景色

推荐文章

如何更新哈希数组(perl中的哈希数组)中的值?

如何更新哈希数组(perl中的哈希数组)中的值?

推荐文章

Rails:使用大集合

Rails:使用大集合

推荐文章

css焦点在safari和chrome中不起作用

css焦点在safari和chrome中不起作用

推荐文章

cognos的预先要求

cognos的预先要求

推荐文章

动态地向XCode项目添加资源,总是落后一个周期

动态地向XCode项目添加资源,总是落后一个周期

推荐文章

在MonoTouch(C#)中创建JSON对象

在MonoTouch(C#)中创建JSON对象

推荐文章

Drupal 7:basic页面中的动态元素/令牌(以用户名登录)

Drupal 7:basic页面中的动态元素/令牌(以用户名登录)

推荐文章

Java注释动态类型转换

Java注释动态类型转换