What does the "cascading member access" operator (..) do?

100 views Asked by At

In the operator documentation, it says this:

x..y - cascading member access. Results in x, which is useful for chaining together method calls.

What does this mean? How is this used? Searching for "cascading member access" only meaningfully served me back that excerpt from the Beef documentation.

1

There are 1 answers

0
Ruzihm On BEST ANSWER

When it says it "results in x", that means after any resolving any calls to y, which is consistent with how the null conditional operator (?.) works. What that means, is that instead of doing something like this:

let s = scope String();
s.AppendF("2 + 3 = {}", 2+3);
Console.WriteLine(s);

...you can use String()..AppendF(...) instead of String().AppendF(...), and since that expression will resolve (again, after the AppendF is called) to the left side of the operator, which is the same as s, it can all be put into the WriteLine call:

Console.WriteLine(scope String()..AppendF("2 + 3 = {}.", 2+3));

And the "chaining" bit refers to use multiple cascading member operators in a row to sequentially call members on the same object in the same expression. For example, you can do this:

Console.WriteLine(scope String()..AppendF("Two + three = {}.", 2+3)..ToUpper());

...instead of this:

let s = scope String();
s.AppendF("Two + three = {}.", 2+3);
s.ToUpper();
Console.WriteLine(s);