C#委托实例,+=,-=以及Null的处理。

例子:

定义一个枚举,进行日志筛选

    public enum Severity
    {
        Verbose,
        Trace,
        Information,
        Warning,
        Error,
        Critical
    }

把错误消息输出到控制台,并记录到txt文本。 这里采用+=的形式,进行委托注册。

委托内注册方法

    static void Main(string[] args)
        {
       
           //委托方法的注册
            Logger.WriteMessage += LoggingMethods.LogToConsole;
            FileLogger f = new FileLogger(@"Log.txt");
            Logger.LogMessage(Severity.Error, "Delegete", "错误状态");
            Console.ReadKey();
        
        
        }

输出到控制台

//处理错误消息
  public static class Logger
    {
        public static Action<string> WriteMessage;

        public static Severity LogLevel { get; set; } = Severity.Warning;

        public static void LogMessage(Severity s, string component, string msg)
        {
            if (s < LogLevel)
                return;

            var outputMsg = $"{DateTime.Now}\t{s}\t{component}\t{msg}";
            WriteMessage(outputMsg);

        }


    }

//输出到控制台
  public static class LoggingMethods
    {
        public static void LogToConsole(string message)
        {
            Console.Error.WriteLine(message);
        }
    }

输出到txt

  public class FileLogger
    {
        private readonly string logPath;
        public FileLogger(string path)
        {
            logPath = path;
            //注册
            Logger.WriteMessage += LogMessage;
        }

        public void DetachLog() => Logger.WriteMessage -= LogMessage;
        // make sure this can't throw.



        private void LogMessage(string msg)
        {
            try
            {
                using (var log = File.AppendText(logPath))
                {
                    log.WriteLine(msg);
                    log.Flush();
                }
            }
            catch (Exception)
            {
                // Hmm. We caught an exception while
                // logging. We can't really log the
                // problem (since it's the log that's failing).
                // So, while normally, catching an exception
                // and doing nothing isn't wise, it's really the
                // only reasonable option here.
            }
        }
    }

空委托的处理方式

  public static void LogMessage(string msg)
        {
            WriteMessage?.Invoke(msg);
        }

当左操作数(本例中为 WriteMessage)为 null 时,null 条件运算符(?.)会短路,这意味着不会尝试记录消息。

不会在 System.Delegate 或 System.MulticastDelegate 的文档中列出 Invoke() 方法。 编译器将为声明的所有委托类型生成类型安全的 Invoke 方法。 在此示例中,这意味着 Invoke 只需要一个 string 参数,并且有一个无效返回类型。

总结:

那么现在可以得出这样的结论了:无返回值的委托,你给它注册多少个方法,它就执行多少个方法,而有返回值的委托,同样注册多少个方法就执行多少个方法,但返回的是最后一个方法的返回值。

2个字符长度的比较的小例子:

  public delegate int Comparison<in T>(T left, T right);
        private static int CompareLength(string left, string right) =>left.Length.CompareTo(right.Length);
   static void Main(string[] args)
        {
            var a = CompareLength("234124", "234124");
            Console.WriteLine(a);
            Console.ReadKey();
        
        
        }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值