C#属性访问器错误

3 c#

我必须在C#2008中编写一个地址簿程序.它应该询问用户该人的姓名,电子邮件和收藏夹颜色(仅通过枚举中的颜色).然后它应该保存联系人以供将来参考.

这是产生错误的代码:

class Contact
{
    string Name; //This string represents the person's Name.
    string Email; //This string represents the person's Email.

    System.Drawing.KnownColor Favoritecolor
    {
        get;
    }
    static void Request()
    //  This function requests the user to type in information about the person.
    {
        Console.WriteLine("Please enter the person's name, e-mail, and favorite color");
        Console.Write; string Name; string Email; ;
        Console.ReadLine();
    }
}
Run Code Online (Sandbox Code Playgroud)

错误是:

'Lab02.Program.Contact.Favoritecolor': property or indexer must have at least one accessor
Run Code Online (Sandbox Code Playgroud)

Bra*_*don 10

 System.Drawing.KnownColor Favoritecolor
 {
     get;
     set;
 }
Run Code Online (Sandbox Code Playgroud)

现在你有了一个关于FavoriteColor属性的get,但是没有设置它的位置,因此它永远不会返回实际值.

如果要实现自动属性,则需要添加一个集.否则创建一个支持字段并返回该字段.

 private System.Drawing.KnownColor _favoriteColor = someValue;
 System.Drawing.KnownColor Favoritecolor
 {
     get { return _favoriteColor; }
 }
Run Code Online (Sandbox Code Playgroud)