Readonly configuration property
After extensive debugging session, I found that the problem was that I called the setter of the readonly property. Is there a trick to trigger a compiler warning when this happens? Because the marking of the closure does not work.
Greetings,
CA
To see what I mean:
private readonly List<SomeObject> _budget;
public IEnumerable<SomeObject> Budget
{
get
{
return _budget;
}
}
Access to
A.Budget=new SomeObject();
no compiler {error, warning, message}
a source to share
You mixed something here. In your example, the compiler will give an error if you try to do A.Budget=new SomeObject();
if a property Budget
in the class A
has no setter. From here, I can only guess what your problem is.
My guess is that you would like the property-wrapped collection Budget
to be read-only. This is probably why you made it IEnumerable<SomeObject>
and the private member is List<SomeObject>
. So even if you don't have a setter, you can still do (A.Budget as List<SomeObject>).Add(bla)
. To disallow this, you can use List.AsReadOnly like this:
private readonly List<SomeObject> _budget;
public ReadOnlyCollection<SomeObject> Budget
{
get
{
return _budget.AsReadOnly();
}
}
a source to share
If your object has a setter, it is not read-only. Remove the installer to make it read-only.
public string MyProperty // not read-only
{
get { return myPropertyBacking; }
set { myPropertyBacking = value; }
}
public string MyProperty // read-only
{
get { return myPropertyBacking; }
}
a source to share
As it doesn't make sense, but if you meant something like this for your example ...
private readonly List<SomeObject> _budget;
public List<SomeObject> Budget
{
get
{
return _budget;
}
}
Access to
A.Budget.Add(new SomeObject());
This will work without error.
EDIT
bitbonk last edit is exactly where I was going with this! I was just waiting for confirmation of the problem.
a source to share