Custom setter but auto getter [duplicate] - c#

This question already has answers here:
How to avoid stack overflow errors when defining set accessor in C#
(4 answers)
Closed 6 years ago.
I have an object with a property for which I want to create a custom setter and also keep the automatic getter:
public class SomeObject
{
public int SomeProp { get; set; }
public Nullable<short> MyProp
{
get
{
return MyProp;
}
set
{
if (value != null)
{
SomeProp = SomeWork(value);
MyProp = value;
}
}
}
}
The problem is that I get a Stackoverflow error on the getter. How do I implement a property where I keep the getter as it is but only modify the setter?

You need to use a backing field for your property. You're getting a SO error because you're recursively referencing your MyProp property in its own getter resulting in infinite recursion.
private short? _myProp;
public short? MyProp
{
get
{
return _myProp;
}
set
{
if (value != null)
{
SomeProp = SomeWork(value);
_myProp = value;
}
}
}

NO, it's not possible. Either you make it a complete auto property (OR) define both the getter and setter in which case you will have to provide the backing field since compiler won't create one for you.

You get the exception because you're essentially doing infinite recursion in your property.
Unfortunately what you're describing isn't possible in C# yet, you need to define the backing field yourself.

You cannot do that. You either use custom getter setters (both, not single) or you stick to auto getters.
In your code getter of your property is trying to access getter of itself (which is going to access the same getter and so on and so on)
So it's an endless loop which is going to destroy all the world and bring the end of humanity

Related

What is the purpose of private member with public getter and setter? c# [duplicate]

This question already has answers here:
Any reason to use auto-implemented properties over manual implemented properties?
(7 answers)
Closed 5 years ago.
what is the different between writing the getter and setter directly like this:
public string Name {get; set;}
and like this:
private string _name;
public string Name
{
get
{
return this._name;
}
set
{
this._name = value;
}
}
I saw that in lots of codes. why they use a private member than a public getter and setter.
is it for performance or privacy or what is the point?
thank you.
what is the different between writing the getter and setter directly
like this
public string Name {get; set;}
They're essentially the same.
The code below you're basically creating the private field and providing both getters and setters for it, which of course does the intended idea. However, the language implementors decided they could make the life of a programmer easier by providing a shorter syntax where you can create a private field and at the same time provide a getter or setter or both.
private string _name;
public string Name
{
get
{
return this._name;
}
set
{
this._name = value;
}
}
So, in C# 3 they(language implementors) came up with the idea of making the syntax shorter by enabling a programmer to simultaneously create a private field and at the same time provide a getter or setter or both.
Behind the scenes, all that happens for the code below is the compiler creates a private field and also provides a getter and setter for it. So, basically, it's shorter more concise syntax to achieve the same task as the example above.
auto-implemented property
public string Name {get; set;}
There is none.
The thing is: auto-implemented properties weren't available until C# 3 (if you look at the documentation referenced: it goes back to VS 2008 which was released with C# 3), and not all code was written in the C# 3 era. Also, not all developers are aware of this feature. If I would stumble across this kind of code, I would rewrite it to use auto-implemented properties.
An property is just a short hand and will create at the background an public get method and a public set method and a private field to store the value.
Example Code
// example property
public string Name { get; set; }
// at run time it is the same as:
private string Name;
public string GetName(){
return this.Name;
}
public string SetName(string name){
this.Name = name;
}
See Image :
The sample class only has an property in code Name.
If you use Reflection to get all the members off the Sample class you will see that at run time set_name() and get_name() methods are generated.
These methods are not specified in in code.
Short answer, there isn't a difference. The compiler will convert the "auto" property to that style regardless, it's just saving you the writer a few keystrokes. It really only comes into play when you start working with DataBinding or having to do something else in the Set portion.
private string _name;
public string Name
{
get
{
return this._name;
}
set
{
this._name = value;OnPropertyChange();
}
}
In WPF/XAML/DataBinding, this would let anyone subscribed to this object know that a property with the name "Name" has changed and it should reflect so in the UI.
The first one is called an auto-implemented property.
Second one is used when you want to add some custom code logic that validates the value in your setter.
You can control what happens in the getter & setters, whereas if the member was public, the variable could be modified directly.
private string _name;
public string Name
{
get
{
return this._name + " likes chocolate";
}
set
{
this._name = value;
}
}
Here, your private _name always stays the same, but anyone accessing Name will get _name + " likes chocolate".
In the setter, you could do some validation.

Why do we need backing fields in C# properties?

NOTE: This is not a question about auto-implemented properties. Auto-implemented properties are about properties without logic in the getters and setters while I stated in my code very clearly that there is some logic. This is not a duplicate of this question nither, since the question is really different, and so is the answer.
I see a lot of this:
private int _age;
public int Age
{
get
{
if(user.IsAuthorized)
{
return _age;
}
}
set
{
if(value >= 0 && value <= 120)
{
_age = value;
}
else
{
throw new ArgumentOutOfRangeException("Age","We do not accept immortals, nor unborn humans...");
}
}
}
But why do we need the backing field? Why no returning the Property itself?
public object Age
{
get
{
if(user.IsAuthorized)
{
return Age;
}
}
set
{
if(value >= 0 && value <= 120)
{
Age = value;
}
else
{
throw new ArgumentOutOfRangeException("Age","We do not accept immortals, nor unborn humans...");
}
}
}
Well, returning property itself leads to Stack Overflow exception:
public object Property
{
get
{
return Property;
}
set
{
Property = value;
}
}
Imagine
MyObject o = new MyObject();
// cause Stack Overflow exception
o.Property = null;
It easy to see why:
setting Property = null calls set
which call Property = value; which in turn calls set
which call Property = value;... and so on.
So if property stores some value the value should be stored in a field (you need a field), we can't use a property to store itself. If you want to shorten the code, put it like this (auto properties):
public object Property {
get; // let .Net create a backing field for you
set;
}
The property there is actually just two hidden methods: get_Property and set_Property. If you return Property from the getter of Property you are in fact causing an infinite recursive loop which will lead to a stack overflow exception: you are calling the method get_Property from the method get_Property, which in turn calls get_Property and so forth.
EDIT: As clarified in the comments, the property is only the two methods and nothing more. There is nothing to hold data; the backing field is what actually contains the data. If you want to store Age as data in the instance, something must hold that data which is why you need the backing field.
Auto-properties automatically create the backing field:
public object Property { get; set; }
In all other cases, the property is just two methods and all data storage is separate and you must handle it yourself.
Everytime you set a value to Property, the set-block will be called. So in your example it would be like:
Property = 10
-- Inside set: Property = Set
----- Called again set: Property = Set
----------- And so on and so o
n
But, you can do the following:
public int Property
{
get;
set;
}
public object Property
{
get
{
return Property;
}
set
{
Property = value;
}
}
This will return the property itself and end up in a recursive loop, a stack overflow. Therefore you want to return another object, for example _property.

'Short-Implement' The Getter 'Long-Implement' The Setter

For a public primitive variable; is it possible to implement the set method and 'Short-Implement' the get method?
When I say 'Short-Implement' the get I mean:
public double width { get; set { width = (isLocked) ? 0:value; } }
Instead of 'Long-Implement' the get:
public double width { get { return width; } set { width = (isLocked) ? 0:value; } }
I get a compile error when I attempt to 'Short-Implement' the get (whats the term for this btw?) and 'Long-Implement' the set. The compile error is:
`Cube.width.get' must have a body because it is not marked abstract, extern, or partial
is it possible to implement the set method and 'Short-Implement' the get method?
No, auto-implemented properties do not allow you to define any part of the getter or setter's implementation.
From Auto-Implemented Properties (C# Programming Guide):
In C# 3.0 and later, auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors
Use a backing field, and consider treating code that calls your setter when isLocked is true as erroneous and throwing an exception.
set
{
if (isLocked)
throw new InvalidOperationException("Knock that set off!");
_width = value;
}

c# property setter body without declaring a class-level property variable

Do I need to declare a class-level variable to hold a property, or can I just refer to self.{propertyname} in the getter/setter?
In other words, can I do this? (where I haven't defined mongoFormId anywhere):
public string mongoFormId
{
get
{
return this.mongoFormId;
}
set
{
this.mongoFormId = value;
revalidateTransformation();
}
}
You can either use automatic accessors or implement your own. If you use automatic accessors, the C# compiler will generate a backing field for you, but if you implement your own you must manually provide a backing field (or handle the value some other way).
private string _mongoFormId;
public string mongoFormId
{
get { return this._mongoFormId; }
set
{
this._mongoFormId = value;
revalidateTransformation();
}
}
UPDATE: Since this question was asked, C# 6.0 has been released. However, even with the new syntax options, there is still no way to provide a custom setter body without the need to explicitly declare a backing field.
You need to set a field variable and store the value there, if you're going to use custom getter and setter.
With the code you have right now you will be running into a stack overflow exception. When you assign something to mongoFormId, you'll execute the line this.MongoFormId = value;. This is an assignment to mongoFormId, resulting in executing the line this.MongoFormId = value;, and so on. It won't ever stop.
The correct way is a field:
private string _mongoFormId;
public string mongoFormId {
get { return this._mongoFormId; }
set {
this._mongoFormId = value;
revalidateTransformation();
}
}
You should have a backing variable. Take a closer look:
get { return this.mongoFormId; }
Is going to call the getter on mongoFormId, which will call that code again, and again, and again! Defining a backing variable will avoid the infinite recursive call.
Check MSDN Properties Overview
While a property definition generally includes a private data member,
this is not required. The get accessor could return a value without
accessing a private data member. One example is a property whose get
method returns the system time. Properties enable data hiding, the
accessor methods hide the implementation of the property.
You can do it both the ways.
If you want to have a class level member variable then do it this way -
public class sampleClass
{
private string _mongoFormId;
public string mongoFormId {
get { return _mongoFormId; }
set {
_mongoFormId = value;
revalidateTransformation();
}
}
}
Or do this simple in class, if no need for revalidateTransformation() execution call there
public class sampleClass
{
public string mongoFormId {get; set;}
}
This won't work since you get a recursive call to the property.
If I'm not mistaken, the result will be a StackOverflowException.
You must use a variable.
private string mongoFormId;
public string MongoFormId
{
get
{
return this.mongoFormId;
}
set
{
this.mongoFormId = value;
revalidateTransformation();
}
}
If you don't have to execute revalidateTransformation, you can use the auto-property.
This will create a backingfiled for you behind the scene.
public string MongoFormId { get; set; }
With the code you wrote, you are creating a recursive endless loop on both the get and set. The this keyword refer to the current class, not the property you are in.
So yes, you need to declare a private field. And to avoid confusion, create properties following the MSDN Naming Guideline (Use Pascal case for properties, camel case for private fields). And please do the same for your methods, it should be RevalidateTransformation instead of revalidateTransformation if you follow the C# convention instead of java's.
private string mongoFormId;
public string MongoFormId
{
get
{
return mongoFormId;
}
set
{
mongoFormId = value;
RevalidateTransformation();
}
}
public string mongoFormId {
get {
return this.mongoFormId;
}
set {
this.mongoFormId = value;
revalidateTransformation();
}
}
this way you have the Function recursive on all paths
The only way i see is to use a private data member. As other boys tells.

Shortest way of writing a C# property? [duplicate]

This question already has answers here:
Public Fields versus Automatic Properties
(14 answers)
Closed 9 years ago.
Someone told me that you could replace the following code:
private string name;
public string Name
{
get;
set;
}
with the following and suffer no ill effects:
public string Name;
I realize that the property, set like in the first example does pretty much the same as it would if I removed it and set the original attribute to publicbut is it bad programming practice to go with the second way for attributes for which you need just the basic getter and setter?
The second way isn't a property, it's a field. The reason you should always use properties for public-facing values is that converting from a field to a property constitutes a breaking change. Using a property allows you to later change the behavior of the getter or setter without breaking any code that references yours.
Keep in mind, the code
public string Foo { get; set; }
is actually equivalent to
private string foo;
public string Foo
{
get { return foo; }
set { foo = value; }
}
When you use Properties you have better control of what properties have.
private string name;
public string Name
{
get;
set;
}
is wrong it should be either
public string Name
{
get;
set;
}
or
private string name;
public string Name
{
get { return name;}
set { this.name = value;}
}
sometimes when you want variable to be set only inside class u can use
public string Name
{
get;
private set;
}
Properties combine aspects of both fields and methods. To the user of an object, a property appears to be a field, accessing the property requires exactly the same syntax. To the implementer of a class, a property is one or two code blocks, representing a get accessor and/or a set accessor. The code block for the get accessor is executed when the property is read; the code block for the set accessor is executed when the property is assigned a new value. A property without a set accessor is considered read-only. A property without a get accessor is considered write-only. A property with both accessors is read-write.
Source: http://msdn.microsoft.com/en-us/library/w86s7x04(v=vs.80).aspx
I think the shortest way is to use Auto-Implemented Properties and some referenced information about them
In C# 3.0 and later, auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors. They also enable client code to create objects. When you declare a property as shown in the following example, the compiler creates a private, anonymous backing field that can only be accessed through the property's get and set accessors.
public string Name{ get; set;}
public string Name { get; set; }
msdn :
A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily and still helps promote the safety and flexibility of methods.
A public property isn't the same as a public instance variable.
And this difference can matter.
For instance, if you're using databound asp.net controls like DataTextField from DroDownListBox, it will fail if you set it to a instance variable instead of a public property.
The shortest way of writing a property is using automatic getters and setters.
This is not quite what you've put in your question though, you've replaced a traditional property that has a backing field, with a field.
An automatic getter/setter looks like this:
public string Blah { get; set; }
This feature was introduced in C# 3, I believe. So you have to target this, or above, in order to use these.

Categories