您的位置:首页 > 职场人生

Question:基于C#连续赋值的面试题(解答)

2018-10-12 14:08 465 查看
题目在这里:Question:基于C#连续赋值的面试题介绍

在msdn中,对=号操作符的说明如下:
赋值运算符 ( =) 将右操作数的值存储在左操作数表示的存储位置、属性或索引器中,并将值作为结果返回。
操作数的类型必须相同(即右操作数必须可以隐式转换为左操作数的类型)。
首先来看
int x,y,z;X = y = z = 1;
.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; }
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.所以这段代码无法通过编译:
接着考虑第二题:


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.WriteLine(c.X==null),调用c的getX方法,方法返回””,所以c.X==null,返回False。

你都做对了吗?

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: