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

what are delegates and how to use them c#

public delegate void MyDelegate(string text);
Comment

what are delegates and how to use them c#

delegate result-type identifier ([parameters])
Comment

what are delegates and how to use them c#

MyDelegate d = new MyDelegate(ShowText);
Comment

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

delegates in c#

[modifier] delegate [return_type] [delegate_name] ([parameter_list]);
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 :: c# list initialize 
Csharp :: add to ienumerable 
Csharp :: action c# 
Csharp :: c# sort array by value 
Csharp :: change canvas color uwp c# 
Csharp :: add rotation 
Csharp :: Entity framwork update parent entity added new sub entity 
Csharp :: exit form esc winforms 
Csharp :: How to remove an element from Array List in C#? 
Csharp :: Unity Input Key Message 
Csharp :: c# null check 
Csharp :: csharp-for-loop 
Csharp :: c# odp.net close session 
Csharp :: remove loading bars devexpress on callback 
Csharp :: setxkbmap 
Csharp :: clear highlight winforms treeview 
Csharp :: core ui switch 
Csharp :: Boolean Literals 
Csharp :: loop code for X seconds 
Csharp :: conditional middleware .net core 
Csharp :: mysql executeScalar only if successful 
Csharp :: process method in scala 
Csharp :: hur delar man upp en e post på string c# 
Csharp :: how to add onclick event dynamically in unity 
Csharp :: ${1##*[! ]} 
Csharp :: where are the viwes in .net core publish 
Csharp :: backcolor app winform C3 
Csharp :: list.SkipWhile in c# 
Csharp :: C++ program obtein volume in windows 
Csharp :: how to move an object with addforce 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =