\

Facebook


วันศุกร์ที่ 26 เมษายน พ.ศ. 2556

เรื่องของ Delegate ในภาษา C# เรื่องนี้ ไม่เคยเจอเลยในตอนเรียน แต่มันก็ไม่ใช่ศาสตร์ขั้นสูงอะไรเลย แต่มีประโยชน์มาก เมื่อใช่ร่วมกับ Event ซึ่งตามคอนเซตที่ผมเข้าใจคือ "การรวบรวมเหตุการณ์(method) เข้าไว้เพื่อเรียกใช้งานในครั้งเดียว" ซึ่งเมทอดนั้นจะมาจากหลากหลาย class มาดูกันเลยดีกว่าว่าโค๊ดเขียนกันยังไง

ขั้นแรกก็เพียงประกาศ delegate เอาไว้...

ผมแปะโค๊ดไว้ก่อน เดี๋ยวมาอธิบายทีหลัง ตอนนี้ ไม่ว่าง ฮ่าๆๆ

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

namespace SampleDay1
{
    public class Program4
    {

        public static void Main(string[] args)
        {
            Person person1 = new Person() { name = "Sam" };
            Person person2 = new Person() { name = "Lula" };
            Person person3 = new Person() { name = "Billy" };
            Animals animal1 = new Animals() { name = "Dog" };

            Home home = new Home();
            home.OnFired += person1.PersonFired;
            home.OnFired += person2.PersonFired;
            home.OnFired += person3.PersonFired;
            home.OnFired += animal1.Bank;
            home.Fire();

            Func<int,int> fu = new Func<int,int>(DoSong);
            int result = fu.Invoke(2);

            Console.ReadLine();
        }
             public static int DoSong(int u)
            {
                Console.WriteLine("sdfdsf");
                 return 1;
            }
    }

        public class Home
        {
            public string color;
            public int height;
            public int width;

            public event OnFireHandler OnFired;

            public void Fire()
            {
                string ss = "h";
                if (OnFired != null)
                {
                    OnFired(ss);
                }
            }

            public void build(string chars)
            {
                Console.WriteLine("CH :" + chars);
            }

        }
        public class Person
        {
            public string name;
            public string PersonFired(string x)
            {
                Console.WriteLine(name + " said fire fire fire!!!"+ x);
                return null;
            }
        }
        public partial class Animals
        {
            public string name;

            public string Bank(string xx)
            { Console.WriteLine(name + "said Bank Bank Bank!!!" + xx); return null; }
        }

        public delegate string OnFireHandler(string str);
}

ไม่มีความคิดเห็น:

May be like this posts

  • Backup ไฟล์ apk จาก Bluestack อีกแนวทางการดาว์นโหลดไฟล์apk
    15/08/2014 - 0 Comments
    บทความที่เเเล้วผมแนะนำการโหลดไฟล์ apk มาตรงๆจาก Google Play Store ไปแล้ว คราวนี้ลองอีกวิธีนึง…
  • สร้าง WebApp ด้วย VueJS บน Digital Ocean ตอนที่ 2 : ติดตั้ง nodejs + npm
    18/05/2018 - 0 Comments
    NodeJS คือ JavaScript framework ที่ใช้ทำ back-end ซึ่งในเคสของผม ผมเลือกเพื่อมาเป็นตัว API สำหรับให้…
  • ฝึกทักษะการฟังภาษาอังกฤษด้วยเพลงฮิตอินเตอร์ quiz music
    31/05/2017 - 0 Comments
    เมื่อการฟังเพลงคือการฝึกฟังภาษาอังกฤษ ถ้าไปดูคลิปเพลง inter top chart บนช่อง YouTube จะพบว่า…
  • ประสบการณ์การใช้ Window 8 Blue Preview ฟรีจาก Microsoft Official
    30/03/2014 - 0 Comments
    เนื่องจาก Hard Disk ที่มาพร้อมกับโน๊ตบุ๊คตั้งแต่แรกซื้อ ถึงขีดจำกัดของมันแล้ว จู่ๆก็เล่นอ่านไฟล์ล้มเหลว…
  • วิธีเพิ่ม Domain บน Google Cloud จาก Namecheap.com
    26/05/2017 - 0 Comments
    หนังจากสร้าง website บน App engine ถึงเวลาเผยแพร่เว็บไซด์ให้คนอื่นเข้าสู่เว็บซึ่ง GCloud…
  • [โปรแกรม searchหาชื่อสินค้า] SQLite บน C# เอาไว้อ่านไฟล์ .db ใน WinForm มันง่ายมั่กๆ
    04/09/2013 - 1 Comments
    วันก่อนผมเขียนเกี่ยวกับการสร้าง app ดึงข้อมูลจากฐานข้อมูล SQL Server 2008 r2…
  • ตัวอย่าง header ที่ใช้ include ในภาษา c
    10/02/2019 - 1 Comments
    Header ของภาษา C คืออะไร  คือไลบราลี่ ที่ประกาศบริเวณหัวของไฟล์ การเริ่มเขียน logical function…