class Constants
{
public const int A = 1;
public const int B = A + 1; //常数也能修改???
}
class Test
{
static void Main()
{
Console.WriteLine("A = {0}, B = {1}", Constants.A, Constants.B);
Console.ReadLine();
}
} const不是声明为常数吗????怎么也能修改他的值》?????
············································
class Class1
{
public int Value = 0;
}
class Test
{
static void Main() {
Class1 ref2 = new Class1();
//Class1 ref2 = ref1;
Class1 ref1 =ref2 ; //实例化问题
ref1.Value = 123;
Console.WriteLine("Refs: {0}, {1}", ref1.Value, ref2.Value);
Console.ReadLine();
}
上面不管把ref1赋给ref2,还是ref2赋给ref1,怎么结果都一样,难道是同指向同样的地址???不解,C不是这样的啊!!!
{
public const int A = 1;
public const int B = A + 1; //常数也能修改???
}
class Test
{
static void Main()
{
Console.WriteLine("A = {0}, B = {1}", Constants.A, Constants.B);
Console.ReadLine();
}
} const不是声明为常数吗????怎么也能修改他的值》?????
············································
class Class1
{
public int Value = 0;
}
class Test
{
static void Main() {
Class1 ref2 = new Class1();
//Class1 ref2 = ref1;
Class1 ref1 =ref2 ; //实例化问题
ref1.Value = 123;
Console.WriteLine("Refs: {0}, {1}", ref1.Value, ref2.Value);
Console.ReadLine();
}
上面不管把ref1赋给ref2,还是ref2赋给ref1,怎么结果都一样,难道是同指向同样的地址???不解,C不是这样的啊!!!
解决方案 »
- listview中设置items的selected为true没有用?
- 一个多线程显示UI的问题
- 这是一个将图片转为二进制存储进数据库的代码,ms里面的值,为什么读入filedata的结果是NULL?
- .NET基元类型转换的问题,关于ToXXX()方法?
- C# 不显示隐藏文件
- 请问 TOP 10000 的数据放到哪里了?
- 求:学习C#设计模式的好的网站及资料!!!
- 求助!在WinForm中,怎么调整DataGrid列的宽度?
- 有个关于类成员属性的问题,一定要帮忙啊!
- 新手问题:c#中怎么样 把float x=0.98f 转换为 98%输出啊
- WinForm中ListView双击空白处没有反应??
- C#下的皮肤技术是如何实现的 谢谢
{
public const int A = 1;
public const int B = A + 1; //常数也能修改???
}
class Test
{
static void Main()
{
Console.WriteLine("A = {0}, B = {1}", Constants.A, Constants.B);
Console.ReadLine();
}
}
这里是在对常量B的值进行初始化赋值。当进行首次赋值以后,就不能再对其进行修改了。class Constants
{
public const int A = 1;
public const int B = A + 1; //常数也能修改???
}
class Test
{
static void Main()
{
Console.WriteLine("A = {0}, B = {1}", Constants.A, Constants.B);
Console.ReadLine();
}
} const不是声明为常数吗????怎么也能修改他的值》?????
············································
class Class1
{
public int Value = 0;
}
class Test
{
static void Main() {
Class1 ref2 = new Class1();
//Class1 ref2 = ref1;
Class1 ref1 =ref2 ; //实例化问题
ref1.Value = 123;
Console.WriteLine("Refs: {0}, {1}", ref1.Value, ref2.Value);
Console.ReadLine();
}
这个不是实例化的问题,而是类的赋值问题。类的赋值在c#中,默认是引用赋值,用c的话说,就是他们的指针是相同的,所以值是相同的,如果想要不同的值,要对期进行clone()或者重新实例化。
B = A + 1
就是 B = 1 + 1常数是编译器确定的。public int A = 1;
public const int B = A + 1;
这个代码绝对不能编译。
{
public const int A = 1;
public const int B = A + 1; //常数也能修改???
} A,B,1 都是常数...
将类作为参数传递,走引用传递路线。
引用传递与值传递的区别是,引用传递传递引用地址,值传递传递值。
况且定义后,在程序中,该常量不得出现在等号的左边。也就是不能再次赋值或者说修改其值。从LZ的代码中,看并没有出现修改常量的值的语句。