public-fields

In what cases should public fields be used instead of properties? [duplicate]

痞子三分冷 提交于 2020-02-06 03:58:08
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Public Data members vs Getters, Setters In what cases should public fields be used, instead of properties or getter and setter methods (where there is no support for properties)? Where exactly is their use recommended, and why, or, if it is not, why are they still allowed as a language feature? After all, they break the Object-Oriented principle of encapsulation where getters and setters are allowed and

Tools for refactoring C# public fields into properties

天大地大妈咪最大 提交于 2019-12-06 02:25:16
问题 I have a lot of C# code that uses public fields, and I would like to convert them to properties. I have Resharper, and it will do them one by one, but this will take forever. Does anyone know of an automated refactoring tool that can help with this? 回答1: Resharper does it very quickly, using Alt+PageDown / ALt+Enter (with the default key bindings). If you are at the first field, Alt+PageDown will jump to the next one (since it'll include wrapping public fields as a suggested refactoring), and

Why can't we use public fields for data binding in C#?

那年仲夏 提交于 2019-12-04 19:11:00
问题 I am aware of the advantages of using properties over fields, like being able to provide additional logic when required in the future. But I really wonder why it's not possible to use public fields for data binding or even for JSON serializers like JavaScriptSerializer class. Is there any good reason to ignore public fields in these cases? Or is it just some kind of convention? Or just to force users to use properties? 回答1: The short version is that always using properties instead of public

Why can't we use public fields for data binding in C#?

核能气质少年 提交于 2019-12-03 11:35:36
I am aware of the advantages of using properties over fields, like being able to provide additional logic when required in the future. But I really wonder why it's not possible to use public fields for data binding or even for JSON serializers like JavaScriptSerializer class. Is there any good reason to ignore public fields in these cases? Or is it just some kind of convention? Or just to force users to use properties? The short version is that always using properties instead of public (or, really, even protected ) fields has been a fundamental design choice in .NET since the very beginning.

How to access @public instance variable from another class in Objective-C?

不打扰是莪最后的温柔 提交于 2019-11-28 08:26:04
I know it's possible to define public instance variable with @public keyword. However, Objective-C syntax does not allow accessing other class' variable. What features should I expected from @public Ivar? Or how do I access other class' Ivars? Objective-C, as a superset of C, definitely does allow the access of public instance variables from outside the class's implementation. Now, the reason you may have heard that it isn't allowed is that it is highly discouraged. In most cases, if you want to access an instance variable outside an implementation context, you should be using accessors and

a way in c++ to hide a specific function

梦想的初衷 提交于 2019-11-27 22:48:13
i have an inheritance struct A : public B , i want to hide individual functions from B, is this possible? i know the opposite is possible using using BMethod in the A declaration. cheers The using keyword can be used to change visibility struct A { void method1(); }; struct B: public A { void method2(); private: using A::method1; }; If you want to selectively hide functions from B it does not make much sense to use public inheritance in the first place. Use private inheritance & selectively bring methods from B into the scope of A: struct B{ void method1(){}; void method2(){}; }; struct A :

Why are public fields faster than properties?

我怕爱的太早我们不能终老 提交于 2019-11-27 12:19:36
I was poking around in XNA and saw that the Vector3 class in it was using public fields instead of properties. I tried a quick benchmark and found that, for a struct the difference is quite dramatic (adding two Vectors together a 100 million times took 2.0s with properties and 1.4s with fields). For a reference type, the difference doesn't seem to be that large but it is there. So why is that? I know that a property is compiled into get_X and set_X methods, which would incur a method call overhead. However, don't these simple getters/setters always get in-lined by the JIT? I know you can't

How to access @public instance variable from another class in Objective-C?

随声附和 提交于 2019-11-27 02:12:40
问题 I know it's possible to define public instance variable with @public keyword. However, Objective-C syntax does not allow accessing other class' variable. What features should I expected from @public Ivar? Or how do I access other class' Ivars? 回答1: Objective-C, as a superset of C, definitely does allow the access of public instance variables from outside the class's implementation. Now, the reason you may have heard that it isn't allowed is that it is highly discouraged. In most cases, if you

a way in c++ to hide a specific function

社会主义新天地 提交于 2019-11-26 21:09:10
问题 i have an inheritance struct A : public B , i want to hide individual functions from B, is this possible? i know the opposite is possible using using BMethod in the A declaration. cheers 回答1: The using keyword can be used to change visibility struct A { void method1(); }; struct B: public A { void method2(); private: using A::method1; }; 回答2: If you want to selectively hide functions from B it does not make much sense to use public inheritance in the first place. Use private inheritance &

Why are public fields faster than properties?

余生长醉 提交于 2019-11-26 15:58:07
问题 I was poking around in XNA and saw that the Vector3 class in it was using public fields instead of properties. I tried a quick benchmark and found that, for a struct the difference is quite dramatic (adding two Vectors together a 100 million times took 2.0s with properties and 1.4s with fields). For a reference type, the difference doesn't seem to be that large but it is there. So why is that? I know that a property is compiled into get_X and set_X methods, which would incur a method call