Vistual studio 2008的一个很有意思的'Bug'
2009-11-04 16:07 宗哥 阅读(3981) 评论(73) 编辑 收藏 举报申明:这个不是C#的bug,觉得是IDE的一个bug,至少这种命名方式在工程中也比较常见,用this可以区分参数和类成员的问题,另外用变量命名方式也可以解决,这种情况我也向学生解释了如果不加this是遵循就近原则。所以不要在攻击我了。不要攻击我,也不要攻击我的学生,^_^.我只是讨论这个算不算IDE的bug问题。
最近有学生问我,对象的字段值竟然自己会变,我觉得不可能,于是她演示代码给我看:
呵呵,有点意思,代码如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
先不管代码意义如何,毕竟是学生用来练习构造方法和属性和字段的,下面是调用代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Student s = new Student(20,"徐晋丽");
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Student s = new Student(20,"徐晋丽");
}
}
}
,有趣的现象出现了,请注意字段的浮动值:
代码并没有给这个对象的字段赋值,学生问我问什么字段name的值也是“徐晋丽”?我也奇怪,呵呵,学生真有意思,这个细节也注意到了。
我打开即时编译窗口,如下截图:请注意即使窗口的和开发窗口的值:
呵呵,我只能告诉学生说,这个是微软开发环境的Bug了。变量感知功能的值是假的,对象的实际的值在即使窗口可以看出来并没有编化。看来刘谦也曾经在微软的Vistual Studio部门呆过.
另外提醒她说,可以注意编程规范,尽量避免参数和字段名字或者属性名字一样,如果一样的化,参数访问就近原则,如果要访问类的参数的化,用this就好了。
替这个学生牛角尖的精神感动.......
声明: 本文作者:宗哥,宗子城
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明。 ...