• Home
  • About
    • Hanna's Blog photo

      Hanna's Blog

      I wanna be a global developer.

    • Learn More
    • Email
    • LinkedIn
    • Github
  • Posts
    • All Posts
    • All Tags
  • Projects

[C#] Delegate

05 Feb 2021

Reading time ~1 minute

Reference by [C#과 유니티로 만드는 MMORPG 게임 개발 시리즈] Part1: C# 기초 프로그래밍 입문

Code

  using System;

  namespace Delegate
  {
    class Program
    {
      delegate int OnClicked();

      static void ButtonPressed(OnClicked clickedFunction)
      {
        clickedFunction();
      }


      static int TestDelegate()
      {
        Console.WriteLine("Hello Delegate!");
        return 0;
      }
      
      static int TestDelegate2()
      {
        Console.WriteLine("Hello Delegate 2!");
        return 0;
      }

      static void Main(string[] args)
      {
        OnClicked clicked = new OnClicked(TestDelegate);
        clicked += TestDelegate2;

        ButtonPressed(clicked);
      }
    }
  }

Delegate

  • Delegate
    • The function is passed over to the factor
    • It can be chained by combining functions

Result

C# Delegate

Download



C#Game Share Tweet +1