Fields, properties, and Methods in C # classes

Source: Internet
Author: User

Person class

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceclasslibrary1{classPerson {//here is the field used to store data, private can write not write        Private string_name; int_age; Char_gender; //here is the property, itself is not value, used to assign values and values to the field method, but not called the method          Public stringName {Get{return_name;} Set{_name =value;} }          Public intAge {//Judging when the value is taken             Get             {                 if(_age <0|| _age > -)                 {                     return_age =0; }                 return_age; }             Set{_age =value;} }          Public CharGender {Get{return_gender;} //be judged when assigning values             Set {                 if(Value! ='male'&& Value! ='female') {Value='male'; } _gender=value; }         }         Public voidXingwei () {Console.WriteLine ("name: {0}, Age: {1}, Gender: {2}", This. Name, This. Age, This.        Gender); }    }}

Class1 class

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceclasslibrary1{ Public classClass1 {Static voidMain (string[] arr) {Person XinRen=NewPerson (); Xinren.name="Zhang Fei"; Xinren.age= the; Xinren.gender='Spring';            Xinren.xingwei ();        Console.readkey (); }    }}

Fields, properties, and Methods in C # classes

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.