C # -jQuery how chaining function is possible in C #?

Since I'm new to C #, I just want to know if I can chaining functions in C # like jQuery?

JQuery example:

$("#gview tbody tr")
   .not(":first,:last")
   .filter(":odd") 
   .addClass("someclass")
   .css("border","solid 1px grey");

      

Note: I don't mean clientide script. I'm only worried about function chaining in C # or not

+2
c #


source to share


7 replies


Yes, just return the current object (this) and you can chain as many as you want. It is also called free interface



+9


source to share


Yes, you need to learn using the Builder Pattern modified to return a handled object.

Example:



public class SomeClass
{
    public SomeClass doSomeWork()
    {
        //do some work on this
        this.PropertyA = "Somethign";

        return this;
    }
}

      

This is also called the chaining design pattern.

+6


source to share


Yes you can, but as with jQuery, the functions must be created for it that you want to chain. If you create your own, just return the object that the caller should be included on. One example of chaining in C # is Fluent nHibernate .

+2


source to share


Yes. I use it regularly, for example with StringBuilder:

string s =
   new StringBuilder()
   .Append(year)
   .Append('-')
   .Append(month)
   .Append('-')
   .Append(day)
   .ToString();

      

Or with my own library for creating HTML controls:

Container.Controls.Add(
   Tag.Div.CssClass("item")
   .AddChild(Tag.Span.CssClass("date").Text(item.Date))
   .AddChild(Tag.Span.CssClass("title").Text(item.Title))
);

      

+1


source to share


yes try this

var s = 19.ToString () Replace ("1", ") .Trim (). ToString (" 0.00 ");

0


source to share


You can call one function from another, like most programming languages ​​... it all depends on how you create.

You can have an object as such:

public class DoMath
{
    private int Add2(int piNumber)
    {
        return piNumber + 2; 
    }
    private int Divideby7(int piNumber)
    {
        return Divideby7(this.Add2(piNumber));
    }
}

      

0


source to share


Yes, and there are some good examples of how this can be done. Take a look at Fluent NHibernate for example .

0


source to share







All Articles
Loading...
X
Show
Funny
Dev
Pics