C # Allow attributes to be used as fields, but do not pass them as ref or out parameter values. When the ref and out parameter values are internally implemented, the memory address must be passed to the target method. However, because the attribute may be a virtual field that does not support fields, or it may be read-only/Write-only, it is impossible to pass the address of its basic storage. Therefore, you cannot pass attributes as ref or out parameter values. The same principle applies to method calls. To pass an attribute or method call as a ref or out parameter value, you must first copy the value to a variable and then pass the variable. After the method is called, the value of the variable is assigned back to the attribute.
Public class classmates
{
Public person [] Students {Get; set ;}
Public Person master {Get; set ;}
Public int age {Get; set ;}
}
Static void main ()
{
Classmates class1 = new classmates ();
Person [] students = new person [] {new person () {name = "Jim"}, new person () {name = "Tom "}};
Person master = new person () {name = "Juanjuan "};
Class1.students = students;
Class1.master = master;
Class1.age = 10;
Changename (ref class1.students [0]);
// Changeage (ref class1.age );
// Changemastername (ref class1.master );
Foreach (VAR student in students)
{
Console. writeline (student. Name );
}
System. Console. readkey ();
}
Static void changename (ref person P)
{
P = new person ();
P. Name = "New Guy ";
}
Static void changemastername (ref person P)
{
P = new person ();
P. Name = "new master ";
}
Static void changeage (ref int age)
{
Age = 10;
}