Home » .Net

Explain Cascaded Method call in C# with an Example

Learn: What is Cascaded function call in C#? How to call functions; explain the concept of cascaded function call using an example/program in C#.Net?

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().

But, 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.

Consider the program:

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

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.





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.