Reference type Object

Source: Internet
Author: User

1. Modify the properties of an object within a method, and you can modify the object directly

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Data;usingSystem.Text;usingSystem.Threading.Tasks;namespacetest{classProgram {Static voidMain (string[] args) {Attendance a=NewAttendance ("AAA");            AA (a);            Console.WriteLine (A.name);        Console.ReadLine (); }         Public Static  voidAA (attendance a) {//A = new attendance ("dddd");A.name ="Eee"; }    }     Public classAttendance { PublicAttendance (stringName) {             This. Name =Name; }         Public stringName {Get;Set; }  PublicDateTime Attendancedate {Get;Set; } }}

The result is the Eee

2. Assigning a similar instance to the object directly within the method will find that the object has not changed

  classProgram {Static voidMain (string[] args) {Attendance a=NewAttendance ("AAA");            AA (a);            Console.WriteLine (A.name);        Console.ReadLine (); }         Public Static  voidAA (attendance a) {a=NewAttendance ("dddd"); //a.name = "eee";        }    }

And the result is AAA.

 classProgram {Static voidMain (string[] args) {Attendance a=NewAttendance ("AAA");            AA (a);            Console.WriteLine (A.name);        Console.ReadLine (); }         Public Static  voidAA (attendance a) {a=NewAttendance ("dddd"); A.name="Eee"; }    }

or AAA?

 classProgram {Static voidMain (string[] args) {Attendance a=NewAttendance ("AAA");            AA (a);            Console.WriteLine (A.name);        Console.ReadLine (); }         Public Static  voidAA (attendance a) {A.name="Eee"; A=NewAttendance ("dddd"); }    }

The result is the Eee

3. To reassign an object within a method, you can use ref so that the object will be modified

 classProgram {Static voidMain (string[] args) {Attendance a=NewAttendance ("AAA"); AA (refa);            Console.WriteLine (A.name);        Console.ReadLine (); }         Public Static  voidAA (refattendance a) {A.name="Eee"; A=NewAttendance ("dddd"); }    }

The result is dddd.

Reference type Object

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.