I've seen the following code in various places:
namespace My.name.space
{
class myClass
{
public CustomObject Name
{
get { return new CustomObject (this.Dog); }
set { return; }
}
}
}
What is the purpose of set { return; }?
I don't understand what purpose set return would serve.
I would think you could just remove the set accessor completely.
None. It's somebody who doesn't quite know that a read-only property can be expressed much more simply by not including the set
public Derp MuhDerp { get { return _derp; } }
Interesting point brought up by CSharpie in a comment...
If you have to have a set because it is defined in an interface, you can add the set but omit the return:
public Derp MuhDerp { get { return _derp; } set { } }
Of course, if the interface defines a setter, you should probably make sure it works as expected :)
It is basically to give the illusion that there is a setter, well there is, but does nothing. But it was probably done to keep some interface happy or a parent class:
public CustomObject Name
{
get { return new CustomObject( this.Dog ); }
set { return; } // does absolutely nothing
}
Here is a class:
public abstract class A {
public abstract void DoWork();
public abstract string SomeProperty { get; set; }
}
Here is giving the illusion that it is implementing the abstract interface, but it really is not implementing everything:
public class B : A {
public override string SomeProperty
{
get
{
return "whatever";
}
set
{
return; // keep interface happy
}
}
public override void DoWork() {
// I am not doing nothing but compiler is happy
}
}
That code also breaks Liskov Substitution Principle.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With