CSharp

พูดกันแบบชาวบ้านเลยนะ มันก็คือตัวแปรที่ชี้ไปยังที่อยู่ของ function หรือ method นั้นเอง

แต่เราจะต้องบอกคุณสมบัติของ delegate เสียก่อนครับ

คุณสมบัติก็คือ ตัว return type และ parameter list ของ function ที่ delegateจะไปชื้นั้นเองมาดูการประกาศกันเลย

delegate ret-type name(parameter-list);

เช่น

public delegate void FunctionToCall();

สังเกต return type เราไม่มีครับ เลยเป็น void ส่วน parameter ของ function เราก็ไม่มี เลยไม่ต้องประกาศอะไร

delegate ตัวนความหมายว่า มันสามารถที่จะชี้ไปที่ function ไหนก็ได้ที่ มี return type เป็น void 1และ ไม่มี parameter ครับ

ดูตัวอย่างกัน:

using System;

delegate void FunctionToCall();

class MyClass

{

public void nonStaticMethod()

{

Console.WriteLine("nonStaticMethod");

}

public static void staticMethod(

{

Console.WriteLine("staticMethod");

}

}

class MainClass

{

static void Main()

{

MyClass t = new MyClass();

/******* ประกาศตัวแปร delegate *******************/

FunctionToCall functionDelegate;

/*******บอก functionที่จะใช้งาน ********************/

functionDelegate = t.nonStaticMethod;

functionDelegate(); // ทำการสั้งให้ทำ function นั้นๆ

}

}

ผลการทำงาน: >> nonStaticMethod

จบแล้ว

>edit @ 2 Dec 2007 23:02:42 by Pom & Ja

 

edit @ 5 Dec 2007 23:24:13 by Pom & Ja

จาก​คราวที่​แล้ว​ผมเขียนเกี่ยว​กับ​ Immutability ​ใน​ Best practise in Coding http://www.thaisharp.com/Forum/Topic.aspx?topic_id=5887
แล้ว​ผม​ได้​ทิ้งคำ​ถามเชิง​ไม่​เห็น​ด้วย​ไว้​ว่า
แล้ว​ You ​ได้​ concern ​เกี่ยว​กับ​ space ​และ​ time ​ที่​ต้อง​ใช้​เพื่อ​ allocate + GC ​ตัว​ immuable object ​ไหม
http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx
เค้า​ไม่​ตอบผม​ด้วย​ภาษาคนครับ​ ​เค้าตอบมา​ด้วย​โค้ด
ตัวอย่าง​จาก​การ​ Implement Immutable Stack
เค้าบอกว่า​ Immutable stack ​นั้น​มีประสิทธิภาพเทียบ​เท่า​หรือ​มากกว่า​ Mutable stack ​ใน​บางกรณีซะอีก
"55 ​ไม่​มีทาง​ ​ขำ​กลิ้ง" ​ผมนั่งหัวเราะ​ ​เพราะ​ว่า​ Stack ​ชื่อมันก็บอก​อยู่​แล้ว​ ​มัน​เป็น​อะ​ไรที่​ต้อง​ push pop ​ๆๆ
มัน​ต้อง​เปลี่ยนแปลง​ ​การที่​จะ​มานั่งสร้าง​ instance ​ใหม่​แล้ว​ copy state ​เดิม​นั้น​ ​มันช่าง​ idiot ​จริงๆ​(ที่​แท้ผมเอง)
แต่หลัง​จาก​นั่งดูตัวอย่างการ​ Implement stack ​แล้ว​ก็​ต้อง​อุทาน​เป็น​ Quantum TV "​โอ​ ​พระ​เจ้ากล้วย​ช่วย​จอร์จมันทอดมาก"

public interface IStack<T> : IEnumerable<T>
{
IStack<T> Push(T value);
IStack<T> Pop();
T Peek();

bool IsEmpty { get; }
}

public sealed class Stack<T> : IStack<T>
{
    private sealed class EmptyStack : IStack<T>
    {
        public bool IsEmpty { get { return true; } }
        public T Peek() { throw new Exception("Empty stack"); }
        public IStack<T> Push(T value) { return new Stack<T>(value, this); }
        public IStack<T> Pop() { throw new Exception("Empty stack"); }
        public IEnumerator<T> GetEnumerator() { yield break; }
        IEnumerator IEnumerable.GetEnumerator() { return this.GetEnumerator(); }
    }
    
    private
static readonly EmptyStack empty = new EmptyStack();
    public static IStack<T> Empty { get { return empty; } }
    private readonly T head;
    private readonly IStack<T> tail;
    private Stack(T head, IStack<T> tail)
    {
        this.head = head;
        this.tail = tail;
    }
    public bool IsEmpty { get { return false; } }
    public T Peek() { return head; }
    public IStack<T> Pop() { return tail; }
    public IStack<T> Push(T value) { return new Stack<T>(value, this); }
    public IEnumerator<T> GetEnumerator()
    {
        for(IStack<T> stack = this; !stack.IsEmpty ; stack = stack.Pop())
            yield return stack.Peek();
    }
    IEnumerator IEnumerable.GetEnumerator() {return this.GetEnumerator();}
}

ถ้า​ Recursive function ​มี​ได้​แล้ว​ ​ทำ​ไม​ recusive data type ​จะ​มี​ไม่​ได้
การทำ​แบบนี้​ไอ้ตัว​ Empty stack ​นั้น​ก็​เหมือน​กับ​เป็น​ ​เงื่อนไขเริ่มต้น​ใน​ recusive function
แล้ว​ Overhead ​ใน​การสร้างมัน​จะ​มี​เท่า​กับ​ mutable ​ได้​อย่างไร

สังเกตดูว่า​ stack ​นี้​ share state ​รวม​กัน​คือ​ tail ​ของมัน​และ​ใน​เมื่อตัว​ stack ​เอง​เป็น​ immutable ​ดัง
นั้น​เราก้​ไม่​ต้อง​เป็น​ห่วงว่า​จะ​เกิด​ side effect ​จาก​การ​ share memory ​นี้
ซึ่ง​ทำ​ให้​ประหยัดมากกว่า​ mutable stack ​อีกแนะ

 

อ้างอิงจาก : http://www.thaisharp.net/Forum/Topic.aspx?topic_id=5923 

เรื่องเกิดจากว่า ได้รับหมอบหมายให้ทำ custom control ที่บริษัท แล้วไปเห็น code ที่น่าสนใจเข้า จึงนำมาบอกกันครับ

        protected static readonly object EventProcessing = new object();
        public event EventHandler Processing {
            add {
                Events.AddHandler(EventProcessing, value);
            }
            remove {
                Events.RemoveHandler(EventProcessing, value);
            }
        }
        public virtual void OnProcessing(EventArgs e) {
           EventHandler processingHandler = (EventHandler)Events[EventProcessing];
            if (processingHandler != null) {
                       processingHandler(this, e);                        
            }

        }
 

 เป็นการประกาส event ที่สวยงามมากทีเดียว