Home » .Net

Calling Member Function using delegates in C#

In this article, we will learn how to call a member function using delegates in C#? It is similar to the static member function calling.
Submitted by IncludeHelp, on August 22, 2018

Prerequisite: Delegates in C#

We can also call a member function of a class using delegates. It is similar to static function calls, here we have to pass member function using an object on the creation of delegate.


using System;
using System.Collections;

public delegate void myDelegates();
class Sample
    public void fun()
        Console.WriteLine("Call a member function using delegate");

class Program
    static void Main()
        Sample S = new Sample();
        myDelegates del = new myDelegates(S.fun);


    Call a member function using delegate

In above example, we created the class Sample, Sample class contains a member function name fun(). And then, we created another class Program than contains Main() function. Here, we created delegate reference and passed member function using Sample class object.

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.