-1

Is there a way to call members within an object's scope? For example, instead of this:

myObject.Foo();
myObject.value = 0;

To call within the scope of myObject:

myObject.
{
    Foo();
    value = 0;
}

This would only serve to save on typing like the implicit type var does. It seems possible since at compile time it's known of what type myObject is.

josh
  • 3
  • 2
  • I really think auto-complete of any modern IDE will defeat the goal and the former snippet is much more readable. And I am not even touching the fact that the latter snippet is not valid C#. – Fildor May 13 '20 at 14:37
  • In VB however, you can use [With](https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/with-end-with-statement). But the question is about C#, where I am not aware of an existing equivalent. – Fildor May 13 '20 at 14:39

1 Answers1

0

No, there is no such syntax available in C#, other than in a call to a constructor, when creating a new instance of the object, you can assign to its public properties.

For example:

var obj = new MyObject {
    Value1 = 0;
    Value2 = 1;
    ....
}
Optimax
  • 1,534
  • 2
  • 16
  • 23