C# Custom getter/setter without private variable - Stack Overflow
文章推薦指數: 80 %
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;//
延伸文章資訊
- 1Is it good idea to set custom-defined getter and setter in c# ...
- 2C# Custom getter/setter without private variable - MicroEducate
C# Custom getter/setter without private variable. by Micro Admin. I learned c# recently, so when ...
- 3c# custom getter setter Code Example
“c# custom getter setter” Code Answer · Browse Popular Code Answers by Language · Browse Other Co...
- 4Learn C# Properties: Getters and Setters at ... - Codeasy
"One of the easiest ways to understand the convenience of properties is to implement a custom set...
- 5Using Properties - C# Programming Guide | Microsoft Docs
These examples illustrate using properties in C#. ... Remarks; Examples; Hidden property example;...