Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

how to create a delegate in c#

//the delegate can point to a void function and takes a string parameter
delegate void Del(string str);

public void hello(string Name)
{
 	Console.WriteLine("hello " + Name) ;
}
public static void main()
{
  //you need to declare the delegate type above and give it a function
  //that matches the delegate's funcion 
  Del HelloDelegate = new Del(hello);
  HelloDelegate("IC");
}
/// (output) -> "hello IC"
Comment

delegates in c#

//A delegate is an object that knows how to call a method.
delegate int Transformer (int x);
//Transformer is compatible with any method 
//with an int return type and a single int parameter,
int Square (int x) 
{ 
  return x * x; 
}
//Or, more tersely:
int Square (int x) => x * x;
//Assigning a method to a delegate variable creates a delegate instance:
Transformer t = Square;
//You can invoke a delegate instance in the same way as a method:
int answer = t(3);    // answer is 9
Comment

C# delegate

    using System;

	public class CargoAircraft
    {
      	// Create a delegate type (no return no arguments)
        public delegate void CheckQuantity();
		// Create an instance of the delegate type
        public CheckQuantity ProcessQuantity;

        public void ProcessRequirements()
        {
          // Call the instance delegate
          // Will invoke all methods mapped
            ProcessQuantity();
        }
    }

    public class CargoCounter
    {
        public void CountQuantity() { }
    }

    class Program
    {
        static void Main(string[] args)
        {
            CargoAircraft cargo = new CargoAircraft();
            CargoCounter cargoCounter = new CargoCounter();
			
          	// Map a method to the created delegate
            cargo.ProcessQuantity += cargoCounter.CountQuantity;
          	
          	// Will call instance delegate invoking mapped methods
            cargo.ProcessRequirements();
        }
    }
}
Comment

delegate in C#

<access_modifier> delegate <return_type> DelegateName([list_of_parameters]);
Comment

C# delegate

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DelegateTest : MonoBehaviour
{
    public delegate void TestDelegate();

    private TestDelegate testDelegate;

    
    // Start is called before the first frame update
    void Start()
    {
        testDelegate = MyDelegateFunc;

        testDelegate();
    }

    // Update is called once per frame
    void Update()
    {
        
    }


    private void MyDelegateFunc()
    {
        Debug.Log("My DelegateFunc");
    }
}
Comment

c# delegate

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DelegateDemo : MonoBehaviour {
	public delegate void Del(string msg); 

	void Start(){
		Del SampleDel;

		SampleDel = PrintMsg;
		SampleDel += Debug.Log; //using plus sign we can subscribe multiple methods to trigger events. 
		SampleDel("Hello Shrinath..."); //when this is called, all the methods subscribed to this delegate gets called
	}

	void PrintMsg(string msg1){
		Debug.Log ("msg : " + msg1);
	}
}
Comment

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);
}
Comment

delegates in c#

[modifier] delegate [return_type] [delegate_name] ([parameter_list]);
Comment

c# delegate

//c# delegate
//delegates are type-safe Pointers to Methods.
Comment

C# delegates

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

namespace DelegateApp {

  /// <summary>
  /// A class to define a person
  /// </summary>
  public class Person {
    public string Name { get; set; }
    public int Age { get; set; }
  }

  class Program {
    //Our delegate
    public delegate bool FilterDelegate(Person p);

    static void Main(string[] args) {

      //Create 4 Person objects
      Person p1 = new Person() { Name = "John", Age = 41 };
      Person p2 = new Person() { Name = "Jane", Age = 69 };
      Person p3 = new Person() { Name = "Jake", Age = 12 };
      Person p4 = new Person() { Name = "Jessie", Age = 25 };

      //Create a list of Person objects and fill it
      List<Person> people = new List<Person>() { p1, p2, p3, p4 };

      //Invoke DisplayPeople using appropriate delegate
      DisplayPeople("Children:", people, IsChild);
      DisplayPeople("Adults:", people, IsAdult);
      DisplayPeople("Seniors:", people, IsSenior);

      Console.Read();
    }

    /// <summary>
    /// A method to filter out the people you need
    /// </summary>
    /// <param name="people">A list of people</param>
    /// <param name="filter">A filter</param>
    /// <returns>A filtered list</returns>
    static void DisplayPeople(string title, List<Person> people, FilterDelegate filter) {
      Console.WriteLine(title);

      foreach (Person p in people) {
        if (filter(p)) {
          Console.WriteLine("{0}, {1} years old", p.Name, p.Age);
        }
      }

      Console.Write("

");
    }

    //==========FILTERS===================
    static bool IsChild(Person p) {
      return p.Age < 18;
    }

    static bool IsAdult(Person p) {
      return p.Age >= 18;
    }

    static bool IsSenior(Person p) {
      return p.Age >= 65;
    }
  }
}
Comment

delegates in c#

[modifier] delegate [return_type] [delegate_name] ([parameter_list]);
  public   delegate    int         GeeksForGeeks    (int G, int F);
Comment

PREVIOUS NEXT
Code Example
Csharp :: get list length c# 
Csharp :: c# for statement 
Csharp :: unity find gameobject with layer 
Csharp :: HCF of list of number 
Csharp :: qtablewidget add image 
Csharp :: c# foreach object in array json 
Csharp :: c# function return 
Csharp :: how to make an ui to follow gameobject 
Csharp :: c# multiple strings are empty 
Csharp :: arcane 
Csharp :: c# add button to messagebox 
Csharp :: convert list of tuples to dictionary c# 
Csharp :: ternary operator in c# 
Csharp :: get array from column datatable c# 
Csharp :: array sort C Sharp 
Csharp :: where in used in linq c# 
Csharp :: vb.net add row to datagridview programmatically 
Csharp :: the .net core sdk cannot be located 
Csharp :: how to filter a datatable in c# 
Csharp :: unity gui style color button 
Csharp :: how to insert into a list c# 
Csharp :: mvc refresh page from controller 
Csharp :: all substrings of a string c# 
Csharp :: set margin programmatically wpf c# 
Csharp :: adding a dependency injection service in windows forms app 
Csharp :: how to close another app in system with c# 
Csharp :: c# get assembly directory 
Csharp :: c# convert string to uri 
Csharp :: unity scroll rect to bottom 
Csharp :: c# inheritance 
ADD CONTENT
Topic
Content
Source link
Name
5+4 =