The answer is written by myself, I don’t know if it’s right
1. Please write the output result (knowledge point: virtual override)
2
Answer:
x=5,y=0
x=6,y=-1
Execution process:
B b=new B();
first call the constructor of A,
then call the virtual method PrintFields() in the constructor of A,
call the overriding method PrintFields();
x=5,y=0;
then call the constructor of B, at this time y= -1;
b.PrintFields();
5+=1;
x=6,y=-1;
3. Investigate the reference type value type and string
Answer: 10 21 0 str sting being converted
public class A
{
private string str = "Class1.str";
private int i = 0;
public static void StringConvert(string str)
{
str = "string being converted";
}
public static void StringConvert(A c)
{
c.str = "string being converted";
}
static void Add(int i)
{
i++;
}
static void AddWithRef(ref int i)
{
i++;
}
static void Main(string[] args)
{
int i1 = 10;
int i2 = 20;
string str = "str";
A c = new A();
Add(i1);
AddWithRef(ref i2);
Add(c.i);
//由于string类型的不变性,函数内部会创建一块新的内存来存放形参str,不会改变实参str,外部打印的是实参str,所以答案为“str”
StringConvert(str);
StringConvert(c);
Console.WriteLine(i1);
Console.WriteLine(i2);
Console.WriteLine(c.i);
Console.WriteLine(str);
Console.WriteLine(c.str);
Console.ReadLine();
}
}