6

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 };
1
  • Definitely not lame, nice question. +1 Commented Dec 7, 2010 at 16:27

8 Answers 8

7

Can be done by:

var x = new ImplementedInteface { Value = 30.0 };
Sign up to request clarification or add additional context in comments.

6 Comments

You missed the round brackets after class name.
@Marcel Gheorghita, you can skip this bracket. no need.
@Marcel - those are optional.
If using a parameterless constructor, it's been optional since object initialization was introduced.
@Marcel Gheorghita, no marcel, as Brian said, It comes with object initialization, if you have resharper, offers to remove this brackets.
|
2
var x = new ImplementedInterface() { Value = 30.0 };

Comments

2

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

Comments

2

You should be able to do:

var x = new ImplementedInterface {Value = 30.0};

Comments

1

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.

Comments

1

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

Comments

0
var x = new ImplementedInterface { Value = 30.0 };

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.