Attributes and methods cannot be used as ref or out parameter values.

Source: Internet
Author: User

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.