c#: getter/setter - Stack Overflow
文章推薦指數: 80 %
In C# 6: It is now possible to declare the auto-properties just as a field: public string FirstName { get; set; } = "Ropert";. Read-Only Auto-Properties
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#:getter/setter
AskQuestion
Asked
10years,8monthsago
Modified
1year,2monthsago
Viewed
293ktimes
132
20
Isawsomethinglikethefollowingsomewhere,andwaswonderingwhatitmeant.Iknowtheyaregettersandsetters,butwanttoknowwhythestringTypeisdefinedlikethis.Thanksforhelpingme.
publicstringType{get;set;}
c#getter-setter
Share
Improvethisquestion
Follow
askedJul15,2011at15:03
MayaMaya
6,7751111goldbadges3939silverbadges5252bronzebadges
5
weblogs.asp.net/dwahlin/archive/2007/12/04/…
– Snowbear
Jul15,2011at15:05
4
ThisiscalledanAuto-Property,havealookatthis:msdn.microsoft.com/en-us/library/bb384054.aspx
– Allov
Jul15,2011at15:05
Thisisthedefinitionofapropertynamed"Type",the.NETtypeofwhichisSystem.string.There'snothingmoretoit.
– Jon
Jul15,2011at15:05
2
IthinkthathemightbeconfusingthenamingoftheAuto-PropertywiththeReflectionclassSystem.Type.msdn.microsoft.com/en-us/library/system.type.aspx
– eandersson
Jul15,2011at15:21
stackoverflow.com/documentation/c%23/49/properties/3365/…
– JonSchneider
Oct4,2016at14:55
Addacomment
|
9Answers
9
Sortedby:
Resettodefault
Highestscore(default)
Datemodified(newestfirst)
Datecreated(oldestfirst)
212
ThoseareAuto-ImplementedProperties(AutoPropertiesforshort).
Thecompilerwillauto-generatetheequivalentofthefollowingsimpleimplementation:
privatestring_type;
publicstringType
{
get{return_type;}
set{_type=value;}
}
Share
Improvethisanswer
Follow
answeredJul15,2011at15:05
JustinNiessnerJustinNiessner
235k3838goldbadges399399silverbadges529529bronzebadges
3
2
@barlopassomeonecomingfromgoogle,withnoclue,whatneedstobeupdated?IsM.Hassansanswerwhatyoumean?thanks.
– Aethenosity
Feb7,2019at7:17
1
@AethenosityinretrospectIthinkit'sok..Iwasthinkinigintermsofexamplesofgettersetter.Thequestionerhasavalidcaseofgettersetterthatisfarmoresuccinct(asaoneliner/nosecondfieldnecessary)..Youcanalsowritepublicintb{get{returnb*2;}}nosecondfieldnecessary.ButIthinkwhenyouhavethesetterwithabodythenyouneedthesecondfield.Andthisoneshowsasetterwithabody.Thoughitdoesthesameasthequestioner'soneliner.
– barlop
Feb7,2019at14:38
2
@Aethenositythereisalsoc#7wherebysureyoustillneedthesecondfieldwhenthesetterhasabody,butithasa=>syntaxandnoreturnkeyword.Thoughthatwasn'twhatIhadinmind.Ihadinmindthesecondfieldbeingunnecessary,thoughIhavesincefoundthatthesecondfieldisnecessarysometimes.Intheexampleherethesecondfieldisn'tnecessary('cosadefaultsetterwithnobodywoulddothis),buttheanswererwasputtingitin(setterbodyandsecondfield),toexplaintothequestionerwhattheirlinewithoutthesecondfieldwasdoing.
– barlop
Feb7,2019at14:39
Addacomment
|
39
Thatisanauto-propertyanditistheshorthandnotationforthis:
privatestringtype;
publicstringType
{
get{returnthis.type;}
set{this.type=value;}
}
Share
Improvethisanswer
Follow
answeredJul15,2011at15:05
TeomanSoygulTeomanSoygul
25.2k66goldbadges6868silverbadges7979bronzebadges
3
4
...exceptthatthebackingfieldisnotaccessible.
– Vlad
Jul15,2011at15:09
9
...whichiswhyitiscalledthebackingfield.
– TeomanSoygul
Jul15,2011at15:11
5
Nevertheless,youshouldbeableseeitas
延伸文章資訊
- 1c#: getter/setter - Stack Overflow
In C# 6: It is now possible to declare the auto-properties just as a field: public string FirstNa...
- 2Getter and Setter In C# 簡短的要命 - 技術筆記- 痞客邦
因此才需要把過程用getter setter 封裝起來 !! (還有許多價值在此不論). 回到C# !! class Student{. public string name{get;set;}.
- 3Getters and Setters in Java Explained - freeCodeCamp
- 4c# getters and setters causing stack overflow - Unity Forum
i'm trying to make my code a bit more OO and so i added getter and setters on a few publics to li...
- 5What are getters and setters and why are they important? - C# Corner