Home > other >  How do we call call method continues in C#
How do we call call method continues in C#

Time:02-23

I need to call multiple methods in a single line

new ClassName().Method1().Method2("XYZ").Method3(1500);

How can I add this feature in my class.

Please find the below code snippet

Public class Person
{
  protected string _name {get;set;}

  public void SetName(string Name)
  {
    this._name=Name;
  }
  public void AddSurname(string sname)
  {
    this._name=sname this._name;
  }

  public void Display()
  {
    Console.Writeline(this._name);
  }

}

I need to call these method in a single like

new Person().SetName("John").AddSurname("Mr.").Display();

CodePudding user response:

Return the instance in your method:

new Person().SetName("").AddSurname("").Display();

class Person
{
    protected string _name {get;set;}

    public Person SetName(string Name)
    {
        this._name=Name;
        return this;
    }
    
    public Person AddSurname(string sname)
    {
        this._name=sname this._name;
        return this;
    }

    public void Display()
    {
        Console.WriteLine(this._name);
    }
}

Read this document: Method chaining.

LINQ in C#, used this style. JavaScript also uses this style frequently, like jQuery.

CodePudding user response:

You can just change the return type of those method to return a person

public class Person
{
  protected string Name;

  public Person SetName(string name)
  {
    Name=name;
    return this;
  }

  public Person AddSurname(string sname)
  {
    Name=sname this.Name;
    return this;
  }

  public void Display()
  {
    Console.Writeline(Name);
  }

}

but personally I think that the better solution would be to create a constructor that sets up a name and title

public class Person
{
  private readonly string _name;
  private readonly string _title;

  public Person(string name, string title)
  {
     _name = name;
     _title = title;
  }
  
  public void Display(){
      Console.WriteLine($"{_title}{_name}");
  }
}

CodePudding user response:

To strictly do as you ask (I.e. to explain the concept of fluent within the framework given) it's like:

public class Person
{
  protected string _name {get;set;}

  public Person SetName(string Name)
  {
    this._name=Name;
    return this;
  }
  public Person AddSurname(string sname)
  {
    this._name=sname this._name;
    return this;

  }

  public void Display()
  {
    Console.Writeline(this._name);
  }

}

You don't always have to return a Person; Display doesn't (in my mind) need to.. You can also return something else if you're looking for some kind of state transition, like AddAddress would return the address that was added and then each time you set an address item it's the address that is returned so you can carry on. Choosing some sensible point to return back to the Person is usually the tricky part (eg the AddAddress on person might return an Address and then he address knows which person it is for, and Address also has an AddAddress which adds another Address to the person and then returns the new address..)

I would consider Panagiotis' advice carefully; this is somewhat overkill/unusual for just setting data attributes - there is a whole ecosystem of stuff that understands properties well, mappers and such like, that wouldn't understand this - that means you're cutting yourself off at the knees in terms of making those things work for you.. But if this is just a demo and you're building something complex like the way EF stitches queries together from the fluently-built queryables it sees you using then it's more understandable..

CodePudding user response:

You can not do such a thing. You are trying to apply a method on "the result" of another void method, which returns nothing by definition.

Something I think you could do is create a class constructor and apply Display() method to it:

Public Person(string name, string sname)
{
this.name = name   sname;
}

and then

new Person("desiredname", "desiredsurname").Display();
  •  Tags:  
  • c#
  • Related