不错的quiz

using System;
using System.Collections.Generic;

namespace abcd
{
classProgram
{
Ss;
structS
{
publicintx;//publicintx=0编译错误?解释为什么
}


Cc
=newC();
classC
{
publicintx=0;
}


voidprint()
{
Console.WriteLine(
"{0}\t{1}",s.x,c.x);
}


voidFoo1(Ss,Cc)
{
s.x
++;
c.x
++;
print();
}


voidFoo2(Ss,Cc)
{
s
=newS();
c
=newC();
print();
}


voidFoo3(refSs,refCc)
{
s.x
++;
c.x
++;
print();
}


voidFoo4(refSs,refCc)
{
s
=newS();
c
=newC();
print();
}


voidTest()
{
Foo1(s,c);
Foo2(s,c);
Foo3(
refs,refc);
Foo4(
refs,refc);
//输出结果,解释为什么:
//01
//01
//12
//00

List
<S>l1=newList<S>();
l1.Add(s);
//l1[0].x++;编译错误?解释为什么

S[]l
=newS[1];
l[
0]=newS();
l[
0].x++;
//数组OK,解释为什么

List
<C>l2=newList<C>();
l2.Add(c);
l2[
0].x++;
//OK,解释为什么
}


staticvoidMain()
{
newProgram().Test();
}

}

}


解释每一个为什么,并且画出对象在内存中的布局变化图!

你可能感兴趣的:(C++,c,C#)