what is a callback function?

Answer Posted / m.shanmuga sundaram

Definition:
***********
Functions that are triggered when an associated event
happens.

example
*******

a) In C language function pointer is used as a callback
functions.

b) In C++ virtual function are used as a callback function.

c) In c# delegate keyword is used to create a call back
function.

example in c#
*************

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
public delegate int execute(int a, int
b); //declare a delegate method

static void Main(string[] args)
{
Program p = new Program();

execute ex = new execute(p.add);
Console.WriteLine(ex(2,3)); //add method is
executed

ex = new execute(p.subtract);
Console.WriteLine(ex(2, 3)); //subtract method
is executed
}

public int add(int a, int b)
{
return (a + b);
}

public int subtract(int a, int b)
{
return (a - b);
}
}
}

Is This Answer Correct ?    11 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What does the keyword “virtual” declare for a method or property?

526


What is window application in c#?

494


What is the difference between overriding and overloading in systemverilog?

452


What is default access specifier for class in c#?

473


Name the property of the textbox which cannot be changed at runtime?

517






What is difference between a function and a method?

491


How do I open the console?

603


What is a data set in c#?

505


What is difference between array and list in c#?

464


What is difference between virtual and override in c#?

456


What is an escape sequence? Name some string escape sequences in c#.

516


What is single dimensional array in c#?

492


Write a short note on interface?

580


Is constructor a static method?

487


Can we change static variable value in c#?

487