using System;
using System.IO;
using System.Threading;
namespace ConsoleApp3
{
class Student
{
public Name name;
public string sex;
public Student(Name name,string sex)
{
this.name = name;
this.sex = sex;
}
}
class Name
{
public string nameA;
public string nameB;
public Name(string nameA, string nameB)
{
this.nameA = nameA;
this.nameB = nameB;
}
}
class Program
{
static void Main(string[] args)
{
Name name = new Name("A", "B");
Student A = new Student(name, "女");
fun(A);
Console.WriteLine($"{A.name.nameA} - {A.name.nameB}");
}
static void fun(Student a)
{
Name name = a.name as Name;
name.nameA = "C";
name.nameB = "D";
return;
}
}
}
输出结果为C-D,因为name变量是指向引用对象属性name的地址,而不是借由new操作符创建一块新的内存。