C# .NET class getter/setter shorthand - MSDN

文章推薦指數: 80 %
投票人數:10人

I remember a friend of mine showing me a nifty shorthand in .NET where you could identify a class's members and getters/setters in a very ... We’resorry.Thecontentyourequestedhasbeenremoved.You’llbeautoredirectedin1second. Askaquestion Quickaccess Forumshome Browseforumsusers FAQ Searchrelatedthreads RemoveFromMyForums Answeredby: C#.NETclassgetter/settershorthand ArchivedForums  >  GettingStartedwithASP.NET Question 0 Signintovote User653228039posted Irememberafriendofmineshowingmeaniftyshorthandin.NETwhereyoucouldidentifyaclass'smembersandgetters/settersinaveryconciseway. Like"get:"and"set:"wereinthesamecodeblock. AnyoneknowwhatI'mtalkingabout?  Couldsomeoneshowmeasampleclassthathasonememberandagetter/setterusingthisshorthand? Thursday,December6,20075:23PM Answers 0 Signintovote User1564875471posted   publicclassPerson { //defaultconstructor publicPerson() { } privatestring_Name; publicstringName { //setthepersonname set{this._Name=value;} //getthepersonname get{returnthis._Name;} } }    Markedasanswerby Anonymous Thursday,October7,202112:00AM Thursday,December6,20075:28PM 0 Signintovote User810302116posted Hi, trythis publicclassMyClass {     privateint_id;     publicintId     {              get{ return_id;}              set{_id=value;}       }      } Regards Markedasanswerby Anonymous Thursday,October7,202112:00AM Thursday,December6,20075:30PM Allreplies 0 Signintovote User1564875471posted   publicclassPerson { //defaultconstructor publicPerson() { } privatestring_Name; publicstringName { //setthepersonname set{this._Name=value;} //getthepersonname get{returnthis._Name;} } }    Markedasanswerby Anonymous Thursday,October7,202112:00AM Thursday,December6,20075:28PM 0 Signintovote User810302116posted Hi, trythis publicclassMyClass {     privateint_id;     publicintId     {              get{ return_id;}              set{_id=value;}       }      } Regards Markedasanswerby Anonymous Thursday,October7,202112:00AM Thursday,December6,20075:30PM 0 Signintovote User653228039posted Thatwastheone. Thankstobothofyou!   TurnsoutnewerversionsofC#supposedlysupportanevenmoreconcisedeclaration(thoughIhaven'tbeenabletoverifybecauseIamonanolderserver):   publicclassPerson { publicstringFirstname{get;set;} publicstringMiddlename{get;set;} publicstringLastname{get;set;} publicintAge{get;set;} } Nowthat'sconcise!   Thursday,December6,20075:54PM 0 Signintovote User541272378posted  IrealizeI'mwaylateonthisone,butyouarecorrect,C#v3.5hasthoseshorthanddeclarations... publicstringMyString{get;set;}    ...whichcreatesaprivatevariablewiththedefaultpublicaccessors(getters/setters),whichisbasicallyequivalentto... privatestring_myString;publicstringMyString{get{return_myString;}set{_myString=value;}} ...andunfortunatelyVB.NET(myclient'srequirement,argh)hasnosuchshortcut,whichishowIcameuponthispost...tryingtofindone(paininthe...) Friday,May8,200911:30PM 0 Signintovote User653228039posted Yeah,thatisprobablythebiggestreasonIpreferC#overVB. VBcodetendstobeprettywordyanditjustfeelslikeI'mwritinganovelsometimesinsteadofcode. :-P Monday,May11,200912:02PM 0 Signintovote User-1266111725posted thisisaquickandeasywaytodothegetterandsetterofthefieldbutIamjustwonderingwhatisthedifferencebetweenthisapproachandthetraditionalapproachinasimpleclassnotinthepolymorphism  privateint_id; publicvoidSetId(intid) { _id=id; } publicintGetId() { return_id; } Friday,May28,201012:29PM 0 Signintovote User-579121670posted publicstringName{get;set;}iswhatIuse,whenisimplyneedagetterandsetter,noneedtoevendeclaretheprivatestring_name.  Thoughifyouwanttodomorewithyourcode(checkvalidation,equations,oranything)youneedtowriteitallout privatestring_name publicvoidName(string_name) { _name+"smith"=Name; } Friday,May28,201012:46PM



請為這篇文章評分?