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
source to share
Yes, just return the current object (this) and you can chain as many as you want. It is also called free interface
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.
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 .
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))
);
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));
}
}
source to share
Yes, and there are some good examples of how this can be done. Take a look at Fluent NHibernate for example .
source to share