this()无参构造函数的小技巧

时间:2019-08-21
本文章向大家介绍this()无参构造函数的小技巧,主要包括this()无参构造函数的小技巧使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
在开发中,类中的this()是对应无参构造函数。所以在调用有参构造函数的时候,想加载默认的无参构造函数可在方法体后面添加:this()。
public partial class MessageWindow : Window
    {
        public MessageWindow()
        {
            InitializeComponent();
            this.Loaded += IssueMessageWindow_Loaded;
        }
        private void IssueMessageWindow_Loaded(object sender, RoutedEventArgs e)
        {
            //实现默认代码
        }
        public MessageWindow(EasyButton easy, string msgInfo) 
        {
            InitializeComponent();
            this.Loaded += IssueMessageWindow_Loaded;
            //实现逻辑
        }
        public MessageWindow(EasyButton easy, string msgInfo, int timeout)
        {
            InitializeComponent();
            this.Loaded += IssueMessageWindow_Loaded;
            //实现逻辑
        }
        public MessageWindow(string msgInfo, int timeout)
        {
            InitializeComponent();
            this.Loaded += IssueMessageWindow_Loaded;
            //实现逻辑
        }
  }
更改为:
 public partial class MessageWindow : Window
    {
        public MessageWindow()
        {
            InitializeComponent();
            this.Loaded += IssueMessageWindow_Loaded;
        }
        private void IssueMessageWindow_Loaded(object sender, RoutedEventArgs e)
        {
            //实现默认代码
        }
        public MessageWindow(EasyButton easy, string msgInfo) : this()
        {
            //实现逻辑
        }
        public MessageWindow(EasyButton easy, string msgInfo, int timeout) : this()
        {
            //实现逻辑
        }
        public MessageWindow(string msgInfo, int timeout) : this()
        {
            //实现逻辑
        }
  }

原文地址:https://www.cnblogs.com/liezhong/p/11389171.html