-1

With string class we are capable of calling methods one after another like this:

originalString.TrimStart().TrimEnd().Replace("Hello","Hi")

I am wondering how does it works? And what this sort of operation called in .net framework.

I would appreciate if you could give me an example to create a class like this.

Thanks,

cyrus-d
  • 749
  • 1
  • 12
  • 29

6 Answers6

6

Every of this method returns new string, so basicaly this are methods called on string returning string. You can create your own class, create methods and run it like this.

Very important is to notice that string is immutable, so calling for e.g. Trim on string won't change it.

public string Trim()
{
  return this.TrimHelper(2);
}

this is how Trim looks like, no magic just simple method.

If your class won't be immutable best way is to create extension methods for your class. You can read more about extension methods here.

Example with normal methods

public class Test
{
    public int Prop {get;set;}

    public Test DoStuf()
    {
        Prop=1;
        return this;
    }

    public Test DoOtherStuff()
    {
       return new Test();
    }
}

and use it:

var test = new Test();
test.DoStuff().DoOtherStuff();
Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105
6

This is nothing special. Many of the methods of the string class return a string, so you can call other methods on the returned value.

For example TrimStart() returns a new string where the whitespaces have been stripped at the start. Then you call TrimEnd() on that returned string.

Sometimes this is called a Fluent interface or method chaining.

Dirk
  • 10,668
  • 2
  • 35
  • 49
1

All of the said method returns string. so it just a type matter.

you can create your own class like this-

Class Definition

public class FooString
    {

        public FooString Test1()
        {
            return this;
        }

        public FooString Test2()
        {
            return this;
        }

        public FooString Test3(string someString)
        {
            return this;
        }

    }

Method Call

FooString test = new FooString();
test.Test1().Test2().Test3("Some string");
Ramashankar
  • 1,598
  • 10
  • 14
1

The principal behind what you are seeing is called Method Chaining (or also a fluent interface).

It works because the method returns the same type as its containing type (ie. TrimStart is a function on the String class, and also returns a String). This is not limited to Strings however.

One of the best modern examples I can think of is jQuery (Which is mentioned in the wikipedia article).

major-mann
  • 2,602
  • 22
  • 37
0

It's simple. String is an immutable class. Which means that every operation you do on it that transform its value will need to return a new and different instance of String (becuase the current one can't be changed). On which you can call another operation and so forth...

string newString = originalString.Replace("Hello","Hi") // new instance
int length = newString.Length // a simple property.
i3arnon
  • 113,022
  • 33
  • 324
  • 344
  • This isn't necessarily tied to immutability. You can write method-chaining methods on a mutable class, and you can write an immutable class with no method-chaining methods. – Rawling Jan 10 '14 at 10:44
  • @Rawling You can't write an immutable class that changes value without method-chaining. And that's string. – i3arnon Jan 10 '14 at 10:46
0

Simply return result with each operation, if result is of the same type, then you could call method of result, then method of result of result, etc.

public class A
{
    public A SomeMethod();
    ...
}

A.SomeMethod().SomeMethod()...
Sinatr
  • 20,892
  • 15
  • 90
  • 319