C#代码出问题了

weikeli19 2016-03-24 06:05:56
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
{

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 ?
...全文
118 9 打赏 收藏 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
weikeli19 2016-03-24
  • 打赏
  • 举报
回复
引用 7 楼 xuzuning 的回复:
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);
                }
            }
        }

    }
}
oh 非常感谢!书上也没写 太谢谢了
crystal_lz 2016-03-24
  • 打赏
  • 举报
回复
引用 6 楼 weikeli19 的回复:
[quote=引用 4 楼 starfd 的回复:] liweike a=new liweike(); Console.WriteLine(a.MyIntProp); 你那里执行set了,你只是get了而已
那怎么样才能使它set[/quote] 赋值 set 取值 get 很难理解?。。。
xuzuning 2016-03-24
  • 打赏
  • 举报
回复
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);
}
}
}

}
}
weikeli19 2016-03-24
  • 打赏
  • 举报
回复
引用 4 楼 starfd 的回复:
liweike a=new liweike(); Console.WriteLine(a.MyIntProp); 你那里执行set了,你只是get了而已
不好意思 我还是没明白
weikeli19 2016-03-24
  • 打赏
  • 举报
回复
引用 4 楼 starfd 的回复:
liweike a=new liweike(); Console.WriteLine(a.MyIntProp); 你那里执行set了,你只是get了而已
那怎么样才能使它set
  • 打赏
  • 举报
回复
liweike a=new liweike(); Console.WriteLine(a.MyIntProp); 你那里执行set了,你只是get了而已
weikeli19 2016-03-24
  • 打赏
  • 举报
回复
引用 1 楼 starfd 的回复:
为什么要异常,你又没执行set 至于第二个value根本就不是li或者myInt,它代表set进来的值
大侠 请讲清楚 详细点 我哪里没执行set了?
  • 打赏
  • 举报
回复
为什么要异常,你又没执行set 至于第二个value根本就不是li或者myInt,它代表set进来的值
qq_28928829 2016-03-24
  • 打赏
  • 举报
回复
这是属性,属性里面就是用myInt的,而且你这是只是获得属性的值,没有设置属性

111,128

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧