using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Demo
{
public class MyClass
{
public readonly string Name;
private int intVal;
public int Val
{
get
{
return intVal;
}
set
{
if(value >= 0 && value <= 10)
{
intVal = value;
}
else
{
throw (new ArgumentOutOfRangeException("Val", value, "值必须在0到10之间。"));
}
}
}
public override string ToString()
{
return "Name:" + Name + "\nVal:" + Val;
}
public MyClass(string newName)
{
Name = newName;
intVal = 0;
}
static void Main(string[] args)
{
MyClass objectA = new MyClass("China!");
objectA.Val = 11;
Console.WriteLine("{0}",objectA.ToString());
Console.ReadKey();
}
}
}
本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/6763517.html,如需转载请自行联系原作者