今天在用MSBUILD编译一个项目的时候,碰到了这个跟struct相关的错误。
Backing Field for Automatically Implemented Property [Text] must be Fully Assigned Before Control is Returned to the Caller
我的代码如下,
public struct ColorText
{
public string Text { get; set; }
public ConsoleColor Color { get; set; }
public ColorText(string text, ConsoleColor color)
{
Text = text;
Color = color;
}
{
public string Text { get; set; }
public ConsoleColor Color { get; set; }
public ColorText(string text, ConsoleColor color)
{
Text = text;
Color = color;
}
最后找到了一个解决方案是调用无参构造函数。
public ColorText(string text, ConsoleColor color): this()
{
Text = text;
Text = text;
这个无参构造函数是系统定义的,无法自己定义。因为struct是值类型,系统会给所有属性只赋默认值.