Get and set methods in C #

Source: Internet
Author: User

Public class Photo {

Private int _ id;
Private int _ albumid;
Private string _ caption;

Public int PhotoID {get {return _ id ;}
Set {this. _ id = value ;}}

Public int AlbumID {get {return _ albumid ;}
Set {this. _ albumid = value ;}}

Public string Caption {get {return _ caption ;}
Set {this. _ caption = value ;}}

Public Photo (int id, int albumid, string caption ){
_ Id = id;
_ Albumid = albumid;
_ Caption = caption;
}

}
In general, get is used to obtain the value. set is used to set the value.

For example, get {return _ id ;}
If you call this get method, you can obtain this. _ id = value (returned value) of the return method)
Call method: Console. Write (object _ id); (output value of this attribute)

For example, set {this. _ id = value ;}
If you call this set method, You can reset the value of _ id, Which is the value you set.
Call method: object. _ id = "New Value"; (attaches a new value to this attribute)

 

Related Article

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.