自定义默认结构作为可选参数



我有一个名为WindowProperties的结构。它有 3 个属性,一个静态 Default 和一个带有可选参数的构造函数。

public struct WindowProperties
{
public string Title;
public int Width;
public int Height;
public static WindowProperties Default => new WindowProperties("Fury Engine", 1280, 720);
public WindowProperties(string title = "Fury Engine", int width = 1280, int height = 720)
{
Title = title;
Width = width;
Height = height;
}
}

我有一个将WindowsProperties作为参数的方法

public WindowsWindow(WindowProperties props = WindowsProperties.Default)
{
Init(props);
}

这给了我一个错误"Default parameter 'props' must be a compile-time constant"

我该怎么做才能解决这个问题?

使用两个构造函数,如

public WindowsWindow()
:this(WindowsProperties.Default) 
{}
public WindowsWindow(WindowProperties props)
{
Init(props);
}

最新更新