日期:2014-05-18  浏览次数:21062 次

深入解析C#代码1.3编译不通过,不知道为什么??
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace CSharpConsole
{
  class Product
  {
  public string Name { get;private set;}
  public decimal Price { get;private set;}
  public Product(string name, decimal price)
  {
  Price = price;
  Name = name;
  }
  Product()
  {
  }
  public override string ToString()
  {
  return string.Format("{0},{1}",Name,Price);
  }
  }

  class Program
  {
  static void Main(string[] args)
  {
   
  }
  }
}


错误 1 “CSharpConsole.Product.Name.get”必须声明主体,因为它未标记为 abstract 或 extern

我是按照书上一步一步打代码的,出现了错误,不知道什么原因.我也想申明Name的主题,但是书上的例子就是这样的阿,很奇怪,难道是书上错了??

------解决方案--------------------
public string Name { get;private set;}
这种写法是C#3.0之后才有的,确保你的工程的属性中的.net版本是3.0以上