I have a property defined as...
public List<Obj> Objs { get; set; }
What I would like to be able to do is to put some logic into the get method, so it would look something like...
public List<Obj> Objs
{
get
{
if (Objs == null)
{
Objs = new List<Obj>();
}
if (Objs.Count < 1)
{
Objs.Add(new Obj());
}
return Objs;
}
set { Objs = value; }
}
Now when I do this I get an error telling me that the Function is recursive on all paths.
Is there a way to do this without creating a private backing field?
You have to make a private field:
private List<Obj> _objs;
public List<Obj> Objs
{
get
{
if (_objs== null)
{
_objs= new List<Obj>();
}
if (_objs.Count < 1)
{
_objs.Add(new Obj());
}
return _objs;
}
set { _objs= value; }
}
Why is it impossible? Lets make the same thing in Java:
private List<Obj> objs;
public List<Obj> getListObjs()
{
...
// Recursion
return getListObjs();
}
No there's no way of doing it without a backing field. Unrelated to the question but related to the situation. You should in general not expose a setter for a collection but only a getter. If you have a setter you are very often exposing internal state of the object, that should be kept hidden.
Your property refers to itself in the definition of the get
part of the property. This is illegal as it would cause the getter to end up in an endless loop. You either have a auto-implemented property (your first example), or a property with a backing field (which is automatically generated by the compiler for auto-implemented properties). You need to add a (preferrably private) field as a backing store for your property:
private List<Obj> objs;
public List<Obj> Objs
{
get
{
if (objs == null)
{
objs = new List<Obj>();
}
if (objs.Count < 1)
{
objs.Add(new Obj());
}
return objs;
}
set { objs = value; }
}
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