Home »
.Net »
C# Programs
C# - Cascaded Method Calls
In this tutorial, we will learn about the cascaded method calls with the help of example.
[Last updated : March 21, 2023]
Introduction
In Object Oriented programming approach, generally we call functions using its object name, for example there is an object named obj of class xyz and method name is myFun() then we can call it by using obj.myFun().
Cascaded Method Calls
In C#.Net, we can call multiple functions in a single statement; it is called cascaded method calling in C#.
We have already disused about this reference in C# (it is a reference of current object), with the help of this reference, we can achieve cascading function calling.
C# example of cascaded method calls
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1 {
class Demo {
public Demo FUN1() {
Console.WriteLine("\nFUN1 CALLED");
return this;
}
public Demo FUN2() {
Console.WriteLine("\nFUN2 CALLED");
return this;
}
public Demo FUN3() {
Console.WriteLine("\nFUN3 CALLED");
return this;
}
}
class Program {
static void Main(string[] args) {
Demo D;
D = new Demo();
D.FUN1().FUN2().FUN3();
}
}
}
Output
FUN1 CALLED
FUN2 CALLED
FUN3 CALLED
Explanation
In this program, class "Demo" contains three methods and each method is returning "this", which contains the reference of object. And by using the reference of object we can call multiple functions in a statement.
C# Basic Programs »