今天在用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是值类型,系统会给所有属性只赋默认值.
本文介绍了一个关于使用Struct时遇到的编译错误:字段未完全分配前返回控制权给调用者。通过调整构造函数调用无参数构造函数的方式解决了问题。

被折叠的 条评论
为什么被折叠?



