using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace delete
{
class Program
{
static void Main(string[] args)
{
liweike a=new liweike();
Console.WriteLine(a.MyIntProp);
}
}
class liweike
{
private int li = 22;
private int myInt = 112;
public int MyIntProp
{
get
{
return myInt;
}
set
{
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace delete
{
class Program
{
static void Main(string[] args)
{
liweike a=new liweike();
Console.WriteLine(a.MyIntProp);
}
}
class liweike
{
private int li = 22;
private int myInt = 112;
public int MyIntProp
{
get
{
return myInt;
}
set
{
try
{
if (value >= 0 && value <= 10)
myInt = value;
else
throw (new ArgumentOutOfRangeException());
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
}
}
问一下这段代码为什么不抛出异常并且不执行catch语句?还有就是value这个关键字怎么知道指的是private int myInt而不是private int li ?
解决方案:40分
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace delete
{
class Program
{
static void Main(string[] args)
{
liweike a = new liweike();
a.MyIntProp = 100; //这样才是执行了 set
Console.WriteLine(a.MyIntProp);
}
}
class liweike
{
private int li = 22;
private int myInt = 112;
public int MyIntProp
{
get
{
return myInt;
}
set
{
try
{
if (value >= 0 && value <= 10)
myInt = value;
else
throw (new ArgumentOutOfRangeException());
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
}
}
