[C#基礎] 屬性實現
[C#基礎] 屬性實現
本篇要實現屬性實現的範例,歡迎有興趣的同好,意起來C/P一下。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/*
http://msdn.microsoft.com/zh-tw/library/ms228368(v=vs.110).aspx
http://blog.darkthread.net/blogs/darkthreadtw/archive/2008/06/06/c-3-auto-imp-prop.aspx
*/
namespace CS_Property_Console
{
class Property
{
private int _value;
public int Value
{
get
{
return _value;
}
set
{
if (value < 0)
value = 0;
_value = value;
}
}
}
class Program
{
static void Main(string[] args)
{
Property p1 = new Property();
p1.Value = 10;
Console.WriteLine("p1.Value=" + p1.Value);
p1.Value = -5;
Console.WriteLine("p1.Value=" + p1.Value);
Console.ReadKey(true);
}
}
}
|