有没有办法检查 C# 中的静态对象是否为空?

Is there a way to check if a static object is null in C#?

我需要检查我的 viewModel class 的端点值是否为空,所以我尝试了这个:

   public static SettingsViewModel settingsViewModel { get; set; }

    public App()
    {
        InitializeComponent();

        if (UseMockDataStore)
            DependencyService.Register<MockDataStore>();
       
        MainPage = new AppShell();

        if(settingsViewModel.Endpoint==null)
        {
            Shell.Current.DisplayAlert("Endpoint Not Found", "There's no endpoint found in settings, please enter a valid endpoint", "OK");
        }
    }

但是 'Object reference not set to an instance of an object' 的异常被抛出。有没有其他方法可以在应用程序启动时检查我的 SettingsViewModel 的端点值是否为空?感谢您的帮助!

if(settingsViewModel.Endpoint==null)

此行仅在 settingsViewModel 为 null 时抛出该异常。这是一个错误并且您想修复它,或者如果此时这是一个真正的选择, 你必须在你的陈述中考虑到它:

if (settingsViewModel?.Endpoint == null)

这只会检查是否有模型和端点。它不关心哪个为空。