加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C#属性访问器错误

发布时间:2020-12-15 23:52:45 所属栏目:百科 来源:网络整理
导读:我必须在C#2008中编写一个地址簿程序.它应该询问用户该人的姓名,电子邮件和收藏夹颜色(仅通过枚举中的颜色).然后它应该保存联系人以供将来参考. 这是产生错误的代码: class Contact{ string Name; //This string represents the person's Name. string Emai
我必须在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();
    }
}

错误是:

'Lab02.Program.Contact.Favoritecolor': property or indexer must have at least one accessor

解决方法

System.Drawing.KnownColor Favoritecolor
 {
     get;
     set;
 }

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

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

private System.Drawing.KnownColor _favoriteColor = someValue;
 System.Drawing.KnownColor Favoritecolor
 {
     get { return _favoriteColor; }
 }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读