질문:C\#연속 할당 에 기반 한 면접 문제(해답)

4710 단어 C#연속 할당
질문:C\#연속 할당 에 기반 한 면접 문제 소개 msdn , =         :
image 
      ( =)                      、       ,         。
          (                     )。
    
int x,y,z;X = y = z = 1;
1:z=1, 1   z,    1,
2:y=(z=1),    1   y,   1,
3:x=(y=(z=1)),    2   1   x。
const int x=1;short y;object z;z=y=x;
.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }
       x const,  const      ,  
Z=y=x;          z=y=1。
1:y=1,  y short,  1    short,      1(short);
2: y=1     ,   1(short)   z,  z 1(short)      ,
GetType  System.Int16.
      ,       const int x=1  const  ,    :
int x=1;short y;object z;z=y=x;
.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }
  x Int32,y Int16,  Int32       Int16.            :
clip_image002

class C
 {
     private string x;
     public string X
     {
         get { return x ?? ""; }
         set { x = value; }
     }
 }

static void Main()
{
    C c = new C();
    object z;
    z = c.X = null;

    //
    System.Console.WriteLine(z == null);
    System.Console.WriteLine(c.X == null);
}

관건 은 분석:z=c.X=null;
1:c.X=null;c 의 setX 방법 을 호출 하여 x=null 을 설정 하고 null 을 값 으로 되 돌려 줍 니 다.
2:z=(c.X=null);c.X=null 이 null 로 되 돌 아 왔 기 때문에 null 을 z 에 할당 합 니 다.z 는 null 입 니 다.
3:Console.WriteLine(z=null),true 로 돌아 가기;
4:Console.Write Line(c.X=null),c 의 getX 방법 을 호출 하여 방법 을 되 돌려 줍 니 다.
잘 했 어?

좋은 웹페이지 즐겨찾기