C# .NET class getter/setter shorthand - MSDN
文章推薦指數: 80 %
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
延伸文章資訊
- 1C# .NET class getter/setter shorthand - MSDN
I remember a friend of mine showing me a nifty shorthand in .NET where you could identify a class...
- 2C# short getter setter Code Example - IQCode.com IQCode
C# short getter setter. A-312. public string fname {get; set;}. Add Own solution. Log in, to leav...
- 3Learn C# Properties: Getters and Setters at ... - Codeasy
Discover the best practices with properties in C Sharp language - C# Properties vs Public fields....
- 4Looking for a short & simple example of getters/setters in C# ...
I think a bit of code will help illustrate what setters and getters are: public class Foo { priva...
- 5C# short getter setter Code Example - Code Grepper
Whatever answers related to “C# short getter setter”. what is static setter and getter examples i...