Why can private member variable be changed by class instance?

前端 未结 2 874
深忆病人
深忆病人 2020-12-16 10:41
class TestClass
{
    private string _privateString = \"hello\";
    void ChangeData()
    {
        TestClass otherTestClass = new TestClass();
        otherTestCla         


        
2条回答
  •  旧巷少年郎
    2020-12-16 11:40

    This is because C# enforces class-level privacy and not object-level privacy.

    Most mainstream languages enforce the same policy, i.e. C#, C++ and Java. I think the reason are:

    1) because developers are accustomed to that kind of policy;

    2) because object-level privacy would become much too tedious in return of very few advantages.

提交回复
热议问题