プログラマーな日々

I'm a Cloud Architect and Developer, and a trainee of the network engineer training course. I have certified by AWS and Cisco (SAA/SOA/DVA/CCNA). I especially have highly motivated to reduce toils.

スタートアップロジックをカプセル化する

Applicationクラスのサブクラスを作成して、スタートアップロジックをカプセル化します。

カプセル化前

using System;
using System.Windows;

class Program {
    [STAThread]
    static void Main() {
        Application app = new Application();
        Window w = new Window();
        w.Title = "Hello World";
        w.Show();
        app.Run(w);
    }
}

カプセル化後

using System;
using System.Windows;

namespace HelloWorld {
    class Program {
        [STAThread]
        static void Main() {
            MyApp app = new MyApp();
            app.Run();
        }        
    }
    
    class MyApp : Application {
        public MyApp() {
            Window w = new Window();
            w.Title = "Hello World";
            w.Show();
        }
    }
}