Action<T> 泛型委托

本文介绍如何在.NET中使用Action<T>泛型委托来简化代码编写过程,通过重构现有代码示例,展示其在实际应用中的优势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值