Answers for "what are delegates in c#"

C#
6

delegates in c#

//start with Delegate

using System;
using System.IO;

class Delegate
{
  //create delegate using delegate keyword
  public delegate void HelloMethod(string str);
  //Remove delegate Keyword it will be normal method declaration
  
  public static void Main(String [] args)
  {
    //function poiting to delegate instance
     HelloMethod delegateInstance = new HelloMethod(print);
    //calling delegate which invokes function print()
    delegateInstance("Hey there! Iam delegate method");
  }
  
  public static void print(string delegateStr)
  {
    Console.WriteLine(delegateStr);
  }
}
//Information on delegate

//Delegate is a type safe function pointer which means can point a function to 
//the delegate when delegate get invokes function automatically invokes since 
//function is pointed to the delegate
Posted by: Guest on May-16-2021
0

what are delegates and how to use them c#

public delegate void MyDelegate(string text);
Posted by: Guest on December-23-2020
0

delegate declaration in c#

public delegate int PerformCalculation(int x, int y);
Posted by: Guest on December-08-2020
0

c# delegate

// Declare a delegate type
public delegate double MyDelegateType(double x);

// Define some consummer that uses functions of the delegate type
public double Consummer(double x, MyDelegateType f){
    return f(x);
}


//...


// Define a function meant to be passed to the consummer 
// The only requirement is that it matches the signature of the delegate
public double MyFunctionMethod(double x){
    // Can add more complicated logic here
    return x;
}
// In practice now
public void Client(){
    double result = Consummer(1.234, x => x * 456.1234);
    double secondResult = Consummer(2.345, MyFunctionMethod);
}
Posted by: Guest on May-16-2021
0

what are delegates and how to use them c#

myDelegate d1 = new myDelegate(Method1);myDelegate d2 = new myDelegate(Method2);myDelegate multicastDelegate = (myDelegate)Delegate.Combine(d1, d2);multicastDelegate.Invoke();
Posted by: Guest on December-23-2020
0

what are delegates and how to use them c#

delegate result-type identifier ([parameters])
Posted by: Guest on December-23-2020

Code answers related to "what are delegates in c#"

C# Answers by Framework

Browse Popular Code Answers by Language