C#复习笔记(2)

Posted on 2006-08-29 22:28  raekwon  阅读(125)  评论(0)    收藏  举报
using System;
 
namespace _10_Relations
 
{
  
/// <summary>
  
/// Summary description for Class1.
  
/// </summary>

  class ComparingRelations
  
{
   
/// <summary>
   
/// The main entry point for the application.
   
/// </summary>

   [STAThread]
   
static void Main(string[] args)
   
{
    
// compare 2 values for equality
    int a = 12;
    
int b = 12;
    Console.WriteLine( a 
== b );
    Console.WriteLine( (
object)a == (object)b );
    
// a与b不相等是因为a与b都是值类型,本身存储相关数据
    string c = "hello";
    
string d = "hello";
    Console.WriteLine( (
object) c==(object) d );
    
// c与d不相等是因为string c与d都是reference类型,存储的都是相同的静态字符数组的地址
    ClassCompare x = new ClassCompare();
    ClassCompare y;
    x.val 
= 1;
    y 
= x;
    Console.WriteLine( x 
== y );
    
// changing 1 object also changes the other
    x.val = 2;
    Console.WriteLine( y.val.ToString() );
   }

  }

  
class ClassCompare
  
{
   
public int val = 0;
  }

 }


博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3