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不是这样的啊!!!

解决方案 »

  1.   

    怎么没有人回复啊????大侠们帮帮忙啊~!@#¥%……&*
      

  2.   

    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();
      }
      } 
     这里是在对常量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()或者重新实例化。
      

  3.   

    无视ls道理很简单,因为 A 是常数。所以
    B = A + 1
    就是 B = 1 + 1常数是编译器确定的。public int A = 1;
    public const int B = A + 1;
    这个代码绝对不能编译。
      

  4.   

    class Constants
      {
      public const int A = 1;
      public const int B = A + 1; //常数也能修改???
      }   A,B,1 都是常数...
      

  5.   

    类(Class)是引用类型。
    将类作为参数传递,走引用传递路线。
    引用传递与值传递的区别是,引用传递传递引用地址,值传递传递值。
      

  6.   

    常量(const)只能在定义的时候赋值。
    况且定义后,在程序中,该常量不得出现在等号的左边。也就是不能再次赋值或者说修改其值。从LZ的代码中,看并没有出现修改常量的值的语句。