Action<T> 泛型委托

1.Action<T> 泛型委托

这个委托很好用, 不用独立的定义声明一个委托了.  

下面的委托代码程序还是在.net 1.x时学会的呢, 当时觉得别扭些, 但最后习惯也就习惯了, 最后还保存成模板拷贝来拷贝去的.

 

 

        public delegate void DelegateMessage(string username, decimal score); 
        static void Main(string[] args)
        {
            DelegateMessage messageTarget = ShowWindowsMessage;
            messageTarget("lzd", 100);

            Console.ReadKey();
        }

        private static void ShowWindowsMessage(string username, decimal score)
        {
            Console.WriteLine(username + score);
        }

 

重构原有的代码, 如下:

 

例如:

        static void Main(string[] args)
        {
            Action<string, decimal> messageTarget = ShowWindowsMessage;
            messageTarget("lzd", 100);

            Console.ReadKey();
        }

        private static void ShowWindowsMessage(string username, decimal score)
        {
            Console.WriteLine(username + score);
        }

 

posted on 2011-11-28 18:00  BobLiu  阅读(741)  评论(0编辑  收藏  举报