using System;
using System.Linq;
using System.Collections.Generic;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
C1 c = new C1();
Console.WriteLine(c.MyValue);
c.MyValue = 20;
Console.WriteLine(c.MyValue);
}
}
//这是我们的主窗口
class C1
{
private int theValue=10;
public int MyValue
{
set { theValue = value; }
get { return theValue; }
}
}
}
C#3.0添加了自动实现属性(automatically implemented property ),允许只声明属性而不声明后备字段。编译器会为你创建隐藏的后备字段,并且自动挂接到get和set访问器上。代码如下:
using System;
using System.Linq;
using System.Collections.Generic;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
C1 c = new C1();
Console.WriteLine(c.MyValue);
c.MyValue = 20;
Console.WriteLine(c.MyValue);
}
}
class C1
{
private int theValue=10;
public int MyValue
{
set;
get;
}
}
}