Gol
Gol

Reputation: 131

What is this pattern called?

string s = new string("Hello World").Replace(" ","_").ToLower().ToUpper();

So you basically return from each method the modified object so can you call new methods on it.

Upvotes: 13

Views: 452

Answers (4)

BG100
BG100

Reputation: 4531

Equivenlant to:

string s = new string("Hello World");
s = s.Replace(" ","_");
s = s.ToLower();
s = s.ToUpper();

Upvotes: 0

MD Sayem Ahmed
MD Sayem Ahmed

Reputation: 29176

The answer is provided by Boldewyn, I am just writing this as a suggestion.

When chaining methods like this, try to write it as follows -

string s = new string("Hello World")
               .Replace(" ","_")
               .ToLower()
               .ToUpper();

This improves code readability.

Upvotes: 4

Andrey
Andrey

Reputation: 60105

Or Fluent Interface

Upvotes: 18

Boldewyn
Boldewyn

Reputation: 82814

Method chaining. (Wikipedia)

Upvotes: 27

Related Questions