刚学了接口。。于是根据书《Head First的c#》里的例题写了个程序,其中有一段是这样
-----------------------------------------------类代码----------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication1
{
    public class TallGuy:IClown 
    {
        public string Name;
        public int Height;
        public void TalkAboutYourself()
        {
            System.Windows.Forms.MessageBox.Show("My name is" + Name + "and I'm" + Height + "inches tall.");
         }
        public string FunnyThingIHave
        {
            get;           
         }
        public void Honk()
        {
            MessageBox.Show("Test");
        }
       
           
     }
-----------------------------------------------类代码-----------------------------------------------  上边的get底下老是有一条波浪线提示有问题:错误 1 “WindowsFormsApplication1.TallGuy.FunnyThingIHave.get”必须声明主体,因为它未标记为 abstract 或 extern。自动实现的属性必须同时定义 get 访问器和 set 访问器。 E:\filessss\Visual Studio 2010\Projects\WindowsFormsApplication6\WindowsFormsApplication6\TallGuy.cs 19 13 WindowsFormsApplication6
后来我上网查了下  有说版本问题  我的是2010肯定没问题
还有就是说要在get后补充内容
于是我就补了(书上本来也有这个的)
-----------------------------------------------补充代码-----------------------------------------------
public string FunnyThingIHave
        {
            get { return "big shoes"; }
         }
-----------------------------------------------补充代码-----------------------------------------------可还是不行
-----------------------------------------------这是接口代码-----------------------------------------------using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApplication1
{
    interface IClown
    {
        void Honk();
        string FunnyThingIHave{get;}    }
}
-----------------------------------------------这是接口代码-----------------------------------------------
为什么会这样呢  而且我在书的前面也确实看到了可以只输入get;这样的  没必要输入后边的东西了啊。。搓脸中。

解决方案 »

  1.   

      public string FunnyThingIHave
      {
      get;   
      }
    =>
      public string FunnyThingIHave { get; private set; }
      

  2.   

    “WindowsFormsApplication1.TallGuy.FunnyThingIHave.get”必须声明主体,因为它未标记为 abstract 或 extern。自动实现的属性必须同时定义 get 访问器和 set 访问器。你的FunnyThingIHave没有定义set访问器,可以像1楼那样定义私有的set访问器,也可以定义public的set访问器
      

  3.   

    +1
    想简单点可以这样,
    注意必须同时定义 get 访问器和 set 访问器。
    至于修饰符就看你项目需要了
      

  4.   

    为什么要同时定义?  我在书中也看到过只设置了get;(只读)的啊   ??郁闷、、、
     
      

  5.   

    GET是获取值   SET是设置输入值  只有获取没输入也没定义能不错吗?
      

  6.   

    public string FunnyThingIHave { get; set; }
      

  7.   

    那请问只读属性是如何写呢  是{get;}这样吗??  如果是不也是没set吗  为什么编译器都不错误  
    我的ide就是2010的  书上的是2008的  但我想这两个版本对这个应该没什么影响把