C# Custom getter/setter without private variable - Stack Overflow

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

c# - Custom setter but auto getter - Stack Overflow Home Public Questions Tags Users Collectives ExploreCollectives FindaJob Jobs Companies Teams StackOverflowforTeams –Collaborateandshareknowledgewithaprivategroup. CreateafreeTeam WhatisTeams? Teams CreatefreeTeam CollectivesonStackOverflow Findcentralized,trustedcontentandcollaboratearoundthetechnologiesyouusemost. Learnmore Teams Q&Aforwork Connectandshareknowledgewithinasinglelocationthatisstructuredandeasytosearch. Learnmore C#Customgetter/setterwithoutprivatevariable AskQuestion Asked 10years,3monthsago Modified 8monthsago Viewed 98ktimes 73 8 Ilearnedc#recently,sowhenIlearnedtowriteproperties,Iwastaughttodoitlikethis: publicstringName{get;set;} Autopropertiesaregreat!ButnowI'mtryingtodosomethingalittlemorecomplicated,soIneedtowriteacustompairofaccessors. privatestring_Name; publicstringName{ get{return_Name;} set{_Name=value} } Iknowthecompilermakesaprivateinstancevariabledowninit'smurkydepthswhenoneusesautos,butI'mspoiledanddon'twantthatprivatevariablesittingaroundlookingpointless. Isthereawaytousecustomaccessorswithoutaprivatevariable? c#coding-styleaccessorautomatic-properties Share Improvethisquestion Follow editedJul23,2013at20:21 TinCan askedDec20,2011at15:15 TinCanTinCan 2,09222goldbadges2727silverbadges3737bronzebadges 11 20 No.it'snotpossible. – BalaR Dec20,2011at15:16 1 Yes!Createaprivateautoproperty!JK.Whatexactlyareyoutryingtodo-what'sthatyour'customaccessors'wouldbedoingthatdoesn'trequireaprivateproperty? – YetAnotherUser Dec20,2011at15:17 1 Whatdoyoumeanby"customaccessors"exactly?What's"custom"aboutthem? – Oded Dec20,2011at15:18 3 @OdedIthinkby"custom",hemeansallnon-autoproperties. – BalaR Dec20,2011at15:19 3 Abetterapproachtothisproblemistrackedhere:github.com/dotnet/csharplang/issues/133 – SachinJoseph Apr14,2018at21:12  |  Show6morecomments 4Answers 4 Sortedby: Resettodefault Highestscore(default) Datemodified(newestfirst) Datecreated(oldestfirst) 59 Propertiesdon'tneedbackingvariables(fields)atall.Whiletheycanbeusedforencapsulatingsimplefieldsyoucanalsousethemtoaccessotherdata. publicDecimalGrandTotal{get{returnFreightTotal+TaxTotal+LineTotal;}} or publicstringSomeStatus{get{returnSomeMethodCall();}} Ifthegoalistosimplyencapsulatesomefieldwithapropertyyouwouldneedsomesortofbackingfieldifyouarenotusingautomaticproperties. Share Improvethisanswer Follow answeredDec20,2011at15:25 jhappoldtjhappoldt 2,2862020silverbadges2424bronzebadges 2 +1Ifyoucomparethe.NETimplementationofpropertiestoalanguagelikeJava,yourealizethatthegetandsetisjustshorthandforcreatingtwodifferentmethodscalledget_PropertyNameandset_PropertyName – KyleTrauberman Dec20,2011at15:29 well,conceptuallysure.technically,defining2methodswiththosenames(inC#atleast)doesn'tresultinapropertyyoucanseewithreflection.Wishwecouldformycurrentdesire(markingonlythesetterasobsolete). – Maslow Apr3,2017at14:32 Addacomment  |  26 TheanswerisNo,youcannotdothat. Itisbecauseofrecursion.(Seelinenumbers9and7): Line1:publicstringName Line2:{ Line3:get Line4:{ Line5:returnFirstName+""+LastName; Line6:} Line7:set Line8:{ Line9:Name=value;//



請為這篇文章評分?