get - C# Reference | Microsoft Docs
文章推薦指數: 80 %
C# Language Specification; See also. The get keyword defines an accessor method in a property or indexer that returns the property value or ... Skiptomaincontent Thisbrowserisnolongersupported. UpgradetoMicrosoftEdgetotakeadvantageofthelatestfeatures,securityupdates,andtechnicalsupport. DownloadMicrosoftEdge Moreinfo Contents Exitfocusmode ReadinEnglish Save Feedback Edit Share Twitter LinkedIn Facebook Email Tableofcontents ReadinEnglish Save Tableofcontents ReadinEnglish Save Feedback Edit Twitter LinkedIn Facebook Email Tableofcontents get(C#Reference) Article 09/15/2021 2minutestoread 10contributors Isthispagehelpful? Yes No Anyadditionalfeedback? FeedbackwillbesenttoMicrosoft:Bypressingthesubmitbutton,yourfeedbackwillbeusedtoimproveMicrosoftproductsandservices.Privacypolicy. Submit Thankyou. Inthisarticle Thegetkeyworddefinesanaccessormethodinapropertyorindexerthatreturnsthepropertyvalueortheindexerelement.Formoreinformation,seeProperties,Auto-ImplementedPropertiesandIndexers. ThefollowingexampledefinesbothagetandasetaccessorforapropertynamedSeconds.Itusesaprivatefieldnamed_secondstobackthepropertyvalue. classTimePeriod { privatedouble_seconds; publicdoubleSeconds { get{return_seconds;} set{_seconds=value;} } } Often,thegetaccessorconsistsofasinglestatementthatreturnsavalue,asitdidinthepreviousexample.StartingwithC#7.0,youcanimplementthegetaccessorasanexpression-bodiedmember.Thefollowingexampleimplementsboththegetandthesetaccessorasexpression-bodiedmembers. classTimePeriod { privatedouble_seconds; publicdoubleSeconds { get=>_seconds; set=>_seconds=value; } } Forsimplecasesinwhichaproperty'sgetandsetaccessorsperformnootheroperationthansettingorretrievingavalueinaprivatebackingfield,youcantakeadvantageoftheC#compiler'ssupportforauto-implementedproperties.ThefollowingexampleimplementsHoursasanauto-implementedproperty. classTimePeriod2 { publicdoubleHours{get;set;} } C#LanguageSpecification Formoreinformation,seetheC#LanguageSpecification.ThelanguagespecificationisthedefinitivesourceforC#syntaxandusage. Seealso C#Reference C#ProgrammingGuide C#Keywords Properties Feedback Submitandviewfeedbackfor Thisproduct Thispage Viewallpagefeedback Inthisarticle
延伸文章資訊
- 1Looking 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...
- 2get - C# Reference | Microsoft Docs
C# Language Specification; See also. The get keyword defines an accessor method in a property or ...
- 3C# short getter setter Code Example - Code Grepper
Whatever answers related to “C# short getter setter”. what is static setter and getter examples i...
- 4C# 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...
- 5C# 7.0 Expression Bodied Members
Lambda expressions can be written in short form without curly brackets when the statement consist...