Justification for CLS specification : The accessibility of the property and of its accessors must be identical

StackOverflow https://stackoverflow.com/questions/7991030

  •  20-02-2021
  •  | 
  •  

Question

Was just running through the ruleset of Parasoft's code analysis tool.

public int testProperty // violation
{
  private get // not matching property accessibility
  { return _testValue; }
  set
  { _testValue = value; }
}

The fix to make them both match. The reason points to the properties section on this MSDN Page on the CLS. However the justification for this is not mentioned on the page.

I frequently use automatic properties of the form public int MyProp { get; private set;}

Is this a violation of the CLS ? RFC

Was it helpful?

Solution

If you look at a more recent version of that page (or even the .NET 2.0 version) it doesn't have that rule. Basically it went away between v1.1 and v2.0... at the same time when C# started allowing them to be specified differently :)

It was a silly rule, and a silly lack-of-feature in C# 1, IMO. It's obviously useful to be able to have a private setter and a public getter. It's pretty rare to have it the other way round, admittedly...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top