Private Set

Private Set



πŸ›‘ ALL INFORMATION CLICK HERE πŸ‘ˆπŸ»πŸ‘ˆπŸ»πŸ‘ˆπŸ»

































Private Set


Sign up with email
Sign up




Sign up with Google



Sign up with GitHub



Sign up with Facebook




Asked
7 years, 5 months ago


Active
1 year, 5 months ago


c# asp.net interface getter-setter


2,911 3 3 gold badges 16 16 silver badges 35 35 bronze badges


8,963 17 17 gold badges 50 50 silver badges 91 91 bronze badges


73.2k 12 12 gold badges 137 137 silver badges 171 171 bronze badges



It doesn't seem to complain if the setter is public either even if the interface only contains a getter.

–  Mike Cheel
Jan 31 '18 at 19:18



@MikeCheel Thats because the interface only defines the minimum methods/accessors. You're free to add more for when you're using the object directly. Though when using an object as the interface type only those methods/accessors defined in the interface are useable.

–  Marcello Nicoletti
Feb 26 '18 at 21:40


213k 32 32 gold badges 387 387 silver badges 414 414 bronze badges

Staff Engineer, Front End (Contractor - Remote)
JPMorgan Chase Bank, N.A. Moscow, Russia
The Remote Company No office location
Senior/Lead FullStack JavaScript developer

Stack Overflow

Questions
Jobs
Developer Jobs Directory
Salary Calculator
Help
Mobile
Disable Responsiveness


Products

Teams
Talent
Advertising
Enterprise



Company

About
Press
Work Here
Legal
Privacy Policy
Terms of Service
Contact Us



Stack Exchange Network

Technology
Life / Arts
Culture / Recreation
Science
Other


Join Stack Overflow to learn, share knowledge, and build your career.
I've created an interface with some properties.
If the interface didn't exist all properties of the class object would be set to
However, this isn't allowed when using an interface,so can this be achieved and if so how?
In interface you can define only getter for your property
However, in your class you can extend it to have a private setter -
Interface defines public API. If public API contains only getter, then you define only getter in interface:
Private setter is not part of public api (as any other private member), thus you cannot define it in interface. But you are free to add any (private) members to interface implementation. Actually it does not matter whether setter will be implemented as public or private, or if there will be setter:
Setter is not part of interface, so it cannot be called via your interface:

By clicking β€œPost Your Answer”, you agree to our terms of service , privacy policy and cookie policy

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo Β© 2021 Stack Exchange Inc; user contributions licensed under cc by-sa . revΒ 2021.2.2.38474


c# - understanding private setters - Stack Overflow
How do you implement a private setter when using an interface?
c# - .NET Properties - Use Private Set or ReadOnly Property?
private ( set ) | Medium
C# Properties (Get and Set )

The best answers are voted up and rise to the top


Asked
9 years, 9 months ago


Active
4 years, 7 months ago


623 1 1 gold badge 5 5 silver badges 5 5 bronze badges



public string Name { get; protected set; } through inheritance.

–  samis
Apr 2 '18 at 16:21


30.8k 8 8 gold badges 98 98 silver badges 125 125 bronze badges



Just adding this because the question also has a vb.net tag, but in vb.net you need to specify a backer if you use private on either either get or set. So in vb.net it's actually less work to make the property readonly I think.

–  user643192
Feb 26 '13 at 7:08



I never knew about that private set . :-)

–  Afzaal Ahmad Zeeshan
Sep 12 '14 at 16:27



An update for those reading this answer in 2016. C# 6.0 has introduced readonly auto-properties, which allow you to have a readonly property without a backing field: public string Name { get; } . If you don't want a mutable property, that's the preferred syntax now.

–  Alexey
Feb 22 '16 at 14:04



One very good reason not to use private set is that it's not as immutable as we like to pretend it is. If you want to implement a truly immutable class, read only is a must.

–  RubberDuck
Jul 7 '16 at 0:04



May be a performance reason to NOT use readonly. Seems to cause unnecessary copying of structs when accessing methods of a readonly struct field. codeblog.jonskeet.uk/2014/07/16/…

–  Triynko
Jul 31 '19 at 21:52





461 4 4 silver badges 7 7 bronze badges



While I agree with your answer, your example could use some improvement. You may want to make a comment where the compiler error would occur.

–  Michael Richardson
Aug 4 '14 at 18:19



NB The VB.NET syntax corresponding to the C# readonly keyword, is to apply ReadOnly to the field instead of to the property.

–  Zev Spitz
Jan 3 '19 at 9:18


1,978 2 2 gold badges 11 11 silver badges 15 15 bronze badges



I believe 'private set' has special semantics in the compiler (not simply performing as a private accessor). Is this also the case with protected set? If not, where's the semantic equivalent to protected set if private set has special semantics? I haven't been able to find any documentation explaining this.

–  Sprague
Mar 31 '12 at 9:30






+1 but I would call the method "Rename" instead of "SetName".

–  MattDavey
Oct 8 '12 at 14:07


189 1 1 silver badge 5 5 bronze badges


10.4k 1 1 gold badge 26 26 silver badges 50 50 bronze badges


2,152 12 12 silver badges 19 19 bronze badges


Highly active question . Earn 10 reputation in order to answer this question. The reputation requirement helps protect this question from spam and non-answer activity.



Software Engineering

Tour
Help
Chat
Contact
Feedback
Mobile
Disable Responsiveness


Company

Stack Overflow
For Teams
Advertise With Us
Hire a Developer
Developer Jobs
About
Press
Legal
Privacy Policy
Terms of Service



Stack Exchange Network

Technology
Life / Arts
Culture / Recreation
Science
Other


Software Engineering Stack Exchange is a question and answer site for professionals, academics, and students working within the systems development life cycle. It only takes a minute to sign up.
In what situation should I use a Private Set on a property versus making it a ReadOnly property? Take into consideration the two very simplistic examples below.
They both yield the same results. Is this a situation where there's no right and wrong, and it's just a matter of preference?
There are a couple reasons to use private set .
1) If you are not using a backing field at all and want a read-only automatic property:
2) If you want to do extra work when you modify the variable inside your class and want to capture that in a single location:
In general, though, it's a matter of personal preference. Far as I know, there are no performance reasons to use one over the other.
Use private set when you want setter can't be accessed from outside .
Use readonly when you want to set the property only once . In the constructor or variable initializer.
This makes the Name property effectively Read Only to all outside code and provides an explicit Set method. I prefer the explicit Set rather than simply using the set on the Name property because you are changing the value when setting it. Normally if you set a property value, you expect to get the same value back when you call the get later on, which would not happen if you did your ToTitleCase in the set .
However, as you said, there is no one right answer.
Don't use the second example. The whole point of using a property - even if there is nothing going on beyond the getter getting and the setter setting - is to funnel all access through that getter and setter so that if you ever need to change behaviour in the future, it's all in one place.
Your second example abandons that in the case of setting the property. If you used that approach in a large, complex class, and later needed to change the behaviour of the property, you'd be in search-and-replace land, instead of making the change in one place - the private setter.
Whenever I've needed to change the access level of a setter, I've generally changed it to either Protected (only this class and derived classes can change the value) or Friend (only members of my assembly can change the value).
But using Private makes perfect sense when you want to do other tasks in the setter besides changing the backing value. As pointed out earlier, it's good design to not reference your backing values directly but instead only access them through their properties. That ensures that later changes you make to a property are applied internally as well as externally. And there's virtually no performance penalty to referencing a property vs its backing variable.
And there's virtually no performance penalty...
But to clarify, accessing a property is slower than accessing its backing variable. A property's getter and setter are methods that require a Call and a Return, whereas a property's backing variable is accessed directly.
That's why, in cases where a property's getter may be accessed many times within a block of code, the property's value is sometimes cached first (saved in a local variable) and the local variable used instead. Of course, that assumes the property can't be changed asynchronously while the block is executing.
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo Β© 2021 Stack Exchange Inc; user contributions licensed under cc by-sa . revΒ 2021.2.2.38474


Oral And Anal Sex Pics
Teen Nudist Contest Porn
Lisey Sweet Double Penetration
Naked Girl Ru
Youporn Young Big Tits
q_auto" width="550" alt="Private Set" title="Private Set">f_auto" width="550" alt="Private Set" title="Private Set">q_auto" width="550" alt="Private Set" title="Private Set">f_auto/gigs/19370332/original/bc3daaffc25e0b70ce25e9764cd0a168afe593d7.jpg" width="550" alt="Private Set" title="Private Set">

Report Page