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操作符创建一块新的内存。
C# 函数传递对象作为参数时都是采用引用的方式
最新推荐文章于 2025-10-03 08:57:46 发布
本文探讨了C#中如何通过对象引用修改Student类的name属性。实例展示了如何在fun方法中,通过引用传递改变Student对象的姓名。输出结果说明了对对象属性的直接操作效果。

2472

被折叠的 条评论
为什么被折叠?



