-7

Not a duplicate of Why can't you call methods with c# object initializer syntax?

How can I do something like this in one statement? (I know I can't do it in initializer)

Vector vector1 = new Vector { X = 1, Y = 1, VectorMethod1() };
Vector vector2 = new Vector { X = 1, Y = 1, VectorMethod2 };
Vector vector3 = new Vector { X = 1, Y = 1, SomeExternalMethod(vector3) };

I need a one line expression to do the thing equal to such an initializer, not an initializer. I need this to create objects and configure them flexibly inside method calls.

  • 1
    This is still the object initializer. What do you think is the different between this and the duplicate? – Avin Kavish Jun 11 '19 at 05:26
  • I need a one line expression to do the thing equal to such an initializer, not an initializer. – Alice McGee Jun 11 '19 at 05:34
  • @AliceMcGee I see. So you just want a single expression that evaluates to a vector that has some properties set and a method called, not necessarily implemented with an object initialiser? – Sweeper Jun 11 '19 at 05:38
  • So you want to create an object and call a method on it as a oneliner? – Stefan Jun 11 '19 at 05:38
  • Closing and giving dislikes. Is it the way you help people here? – Alice McGee Jun 11 '19 at 05:40
  • 5
    @AliceMcGee Ignore the voting. It isn't personal. People aren't voting against you. They are expressing their opinion re: the quality of the question. Your job is to improve the question (note in particular - your job is **not** to try and defend the question). The best way to do that is to answer questions posed by commenters. I haven't voted against your question - but honestly it isn't clear. It can be improved. As is, this feels like a XY problem - https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem . – mjwills Jun 11 '19 at 05:40
  • I need this to create objects and configure them flexibly inside method calls. – Alice McGee Jun 11 '19 at 05:43
  • 1
    Why does it need to be in one statement **specifically**? The problem with your question, as is, is it **only makes sense to you**. It is clear in your head, but not ours. What are you trying to achieve? Why do you want to use only one statement? Why can't you use multiple lines of code? When we ask questions **we aren't attacking you**. Your doctor asks details about your pain because she wants to help you. We too want to help you - but we need to understand where the pain is first. – mjwills Jun 11 '19 at 05:43
  • There is no good way to do that in one statement. You can do it in one line however, if you really need to: `Vector vector1 = new Vector { X = 1, Y = 1 }; vector1.VectorMethod1();` – Botz3000 Jun 11 '19 at 05:45
  • You can kind of do it in one statement by wrapping everything in a lambda expression: `var vector = ((Func)(() => { var v = new Vector { X = 1, Y = 1}; v.SomeMethod(); return v; }))();`, but that looks extremely ugly. – Sweeper Jun 11 '19 at 05:47
  • 3
    Terrible service here. I quit. And surely I will tell my friends not to use SO. – Alice McGee Jun 11 '19 at 05:54
  • 2
    @AliceMcGee I can see how this must be frustrating for you. For those unaccustomed to SO and its aims and purposes, it can feel difficult. The key bit to understand with SO is it is trying to serve two purposes. Firstly, help you. Secondly, help the next person who has the same issue as you. The benefit of you making your problem clearer is that then the question serves both purposes really well - you get a great answer, and the next person can read and understand the answer to. I know that feels like work - and that can feel frustrating. I hope you have a great day! – mjwills Jun 11 '19 at 05:57
  • 1
    If you feel you have been excessively targeted or unfairly treated, feel free to click the flag next to each comment, or contact the moderators, or raise a topic at https://meta.stackoverflow.com/ . We can all do better - and we welcome your feedback. – mjwills Jun 11 '19 at 05:59

1 Answers1

1

If the method returns itself, it is available:

class Vector
{
    public Vector VectorMethod1()
    {
         return this;
    }
}

Vector vector1 = (new Vector { X = 1, Y = 1 }).VectorMethod1();
shingo
  • 18,436
  • 5
  • 23
  • 42
  • And you could always write one or more wrappers to do this. Funny no one mentioned constructors yet. – H H Jun 11 '19 at 06:17
  • I'd suggest an extension method like `T With(this T obj, Action action) { action(obj); return obj; }`. It's simple to use: `new MyType { Prop1 = prop1Value }. With(obj => obj.Prop2 = prop2Value).With(obj => obj.DoSomething()); // etc` – CSDev Jun 11 '19 at 11:40