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