Real World Scenario
 
 Consider a scenario where you have to create an application, having recharge and  utility bill payment functionality. 
![utility]()
 
 In order to create an application, you have to create Recharge class which accepts a series of values like Service, Operator, Circle, Mobile and Amount and then  called the DoRecharge method. But there is a problem associated with this  approach. One doesn’t know what to do with the Recharge object once it has  finished doing the recharge. Can it be reused to do another recharge or should  it be held to know the status of recharge transaction?
 
- namespace TrainWreckPattern  
- {  
-     public class Recharge   
-     {  
-         public string Service  
-         {  
-             get;  
-             set;  
-         }  
-         public string Operator {  
-             get;  
-             set;  
-         }  
-         public string Circle {  
-             get;  
-             set;  
-         }  
-         public string Mobile {  
-             get;  
-             set;  
-         }  
-         public double Amount {  
-             get;  
-             set;  
-         }  
-         public void DoRecharge() {  
-               
-         }  
-     }  
- }  
- namespace TrainWreckPattern  
- {  
-     class Program  
-     {  
-         static void Main(string[] args)   
-         {  
-             Recharge objRecharge = newRecharge();  
-             objRecharge.Service = "Topup";  
-             objRecharge.Operator = "Vodafone";  
-             objRecharge.Circle = "Gujarat";  
-             objRecharge.Mobile = "9769496026";  
-             objRecharge.Amount = 100;  
-             objRecharge.DoRecharge();  
-         }  
-     }  
- }  
 To figure out the above troubles, we are going to use cascade or train wreck  pattern. This improves readability of a code because it avoids repeated  references to the instance. So, we can call multiple methods to be called on the  same object. This concept was first introduced in Smalltalk programming  language. 
 Here, I have made couple of changes in the code. I have removed all properties,  created a couple of methods like Service, Operator etc. and each one returns  instance of class itself except DoRecharge() method.  
- namespace TrainWreckPattern  
- {  
-     public class Recharge  
-     {  
-         public Recharge Service(string service)  
-         {  
-             return this;  
-         }  
-         public Recharge Operator(string operators) {  
-             return this;  
-         }  
-         public Recharge Circle(string circle) {  
-             return this;  
-         }  
-         public Recharge Mobile(string mobile) {  
-             returnthis;  
-         }  
-         public Recharge Amount(double amount) {  
-             returnthis;  
-         }  
-         public void DoRecharge() {  
-               
-         }  
-     }  
- }  
Now we can call DoRecharge method in the following way: 
- namespace TrainWreckPattern  
- {  
-     clas sProgram  
-     {  
-         static void Main(string[] args)   
-         {  
-             new Recharge().Service("Topup").Operator("Vodafone").Circle("Gujarat").Mobile("9769496026").Amount(100).DoRecharge();  
-         }  
-     }  
- }  
If you look at the above code closely, the beauty of the code is that individual  method returns this, so we can invoke each method without making a new line.  This is the correct way to use Cascade pattern or method cascading. This  pattern is a very useful if object has a very lengthy expression value because  it removes the need of using temporary variable or need to either list the  expression repeatedly 
 So, using such pattern we can remove verbosity from code. Now, after looking at the above code, some of you might be thinking that we have been writing such code a million times particularly when we are writing LINQ query. 
- string[] operators =  
-   {  
-     "Airtel",  
-     "Idea",  
-     "Vodafone",  
-     "Uninor"  
- };  
- IEnumerable < string > query = from x in operators  
- where x.Contains("a")   
- orderby x.Length   
- select x.ToUpper();   
That’s absolutely right. This pattern particularly applied in fluent interfaces.  An interface is said to be fluent if it returns itself from one or more its  method. This allow users to chain calls together. A fluent interface is an  implementation of an object oriented API that aims to provide more readable  code. All the LINQ queries including above and others use this concept.
 We can also achieve the same thing using lambda: 
- public static void DoRecharge(Action < Recharge > action)  
- {  
-     Recharge recharge = newRecharge();  
-     action(recharge);  
-       
- }  
- Recharge.DoRecharge((recharge) =>  
- {  
-     recharge.Service("Topup").Operator("Vodafone").Circle("Gujarat").Mobile("9769496026").Amount(100);  
- });  
 So fundamentally, cascade or train wreck pattern is just syntactic sugar which  makes our code more fluid and understandable for reading.
 
Read more articles on C#: