当前位置: 动力学知识库 > 问答 > 编程问答 >

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

问题描述:

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

网友答案:

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...

分享给朋友:
您可能感兴趣的文章:
随机阅读: