Chaining Method in C #

I really don't know what it's called in C #. But I want to add functionality to my class in order to add several elements at the same time.

myObj.AddItem(mItem).AddItem(mItem2).AddItem(mItem3); 
+48
c #
Jul 13 '09 at 14:31
source share
9 answers

The specified technique is called chain methods. It is commonly used when creating DSL or smooth interfaces in C #.

A typical template is for your AddItem () method to return an instance of the class (or interface) of which it is a part. This allows you to bind subsequent calls to it.

 public MyCollection AddItem( MyItem item ) { // internal logic... return this; } 

Some alternatives to the method chain for adding items to a collection include:

Using params syntax to allow multiple elements to be passed to your method as an array. Useful if you want to hide the creation of an array and provide the syntax of variable arguments to your methods:

 public void AddItems( params MyItem[] items ) { foreach( var item in items ) m_innerCollection.Add( item ); } // can be called with any number of arguments... coll.AddItems( first, second, third ); coll.AddItems( first, second, third, fourth, fifth ); 

Providing an overload of type IEnumerable or IEnumerable so that multiple elements can be passed along with your collection class.

 public void AddItems( IEnumerable<MyClass> items ) { foreach( var item in items ) m_innerCollection.Add( item ); } 

Use the .NET 3.5 collection initializer syntax. The class must provide a single parameter Add( item ) , implement IEnumerable, and must have a default constructor (or you must call a specific constructor in the initialization statement). Then you can write:

 var myColl = new MyCollection { first, second, third, ... }; 
+89
Jul 13 '09 at 14:36
source share

Use this trick:

 public class MyClass { private List<MyItem> _Items = new List<MyItem> (); public MyClass AddItem (MyItem item) { // Add the object if (item != null) _Items.Add (item) return this; } } 

It returns the current instance, which will allow you to chain method calls (thus, adding multiple objects at the same time).

+30
Jul 13 '09 at 14:33
source share

"Actually, I have no idea what this is called in C #"

Free API; StringBuilder is the most common .NET example:

 var sb = new StringBuilder(); string s = sb.Append("this").Append(' ').Append("is a ").Append("silly way to") .AppendLine("append strings").ToString(); 
+12
Jul 13 '09 at 14:37
source share

Others answered in terms of a direct chain of methods, but if you are using C # 3.0, you might be interested in collection initializers ... they are available only when the constructor is called, and only if your method has the corresponding Add and implements IEnumerable , but then you can do:

 MyClass myClass = new MyClass { item1, item2, item3 }; 
+10
Jul 13 '09 at 14:37
source share

Why aren't you using the params ?

 public void AddItem (params MyClass[] object) { // Add the multiple items } 
+5
Jul 13 '09 at 14:37
source share

What about

 AddItem(ICollection<Item> items); 

or

 AddItem(params Item[] items); 

You can use them like this

 myObj.AddItem(new Item[] { item1, item2, item3 }); myObj.AddItem(item1, item2, item3); 

This is not a chain of methods, but adds several elements to your object in a single call.

+3
Jul 13 '09 at 14:36
source share

You can add an extension method to support this if your class inherits from ICollection:

 [TestClass] public class UnitTest1 { [TestMethod] public void CanChainStrings() { ICollection<string> strings = new List<string>(); strings.AddItem("Another").AddItem("String"); Assert.AreEqual(2, strings.Count); } } 
 public static class ChainAdd { public static ICollection<T> AddItem<T>(this ICollection<T> collection, T item) { collection.Add(item); return collection; } } 
+2
Mar 25 '15 at 10:31
source share

If your element acts like a list, you can implement an interface like iList or iEnumerable / iEnumerable.

Regardless, the key to the call chain, as you want, returns the desired object.

 public Class Foo { public Foo AddItem(Foo object) { //Add object to your collection internally return this; } } 
+1
Jul 13 '09 at 14:35
source share

Something like that?

 class MyCollection { public MyCollection AddItem(Object item) { // do stuff return this; } } 
+1
Jul 13 '09 at 14:35
source share



All Articles