access-modifiers

Are there any reasons to use private properties in C#?

白昼怎懂夜的黑 提交于 2019-11-26 21:15:05
I just realized that the C# property construct can also be used with a private access modifier: private string Password { get; set; } Although this is technically interesting, I can't imagine when I would use it since a private field involves even less ceremony : private string _password; and I can't imagine when I would ever need to be able to internally get but not set or set but not get a private field: private string Password { get; } or private string Password { set; } but perhaps there is a use case with nested / inherited classes or perhaps where a get/set might contain logic instead of

Why can't I access C# protected members except like this?

倖福魔咒の 提交于 2019-11-26 18:52:49
This code: abstract class C { protected abstract void F(D d); } class D : C { protected override void F(D d) { } void G(C c) { c.F(this); } } Generates this error: Cannot access protected member 'C.F(D)' via a qualifier of type 'C'; the qualifier must be of type 'D' (or derived from it) What in the world were they thinking? (Would altering that rule break something?) And is there a way around that aside from making F public? Edit: I now get the reason for why this is (Thanks Greg ) but I'm still a bit perplexed as to the rational; given: class E : C { protected override void F(D d) { } } Why

How to make a property protected AND internal in C#?

我的梦境 提交于 2019-11-26 17:55:17
Here is my shortened abstract class: abstract class Report { protected internal abstract string[] Headers { get; protected set; } } Here is a derived class: class OnlineStatusReport : Report { static string[] headers = new string[] { "Time", "Message" } protected internal override string[] Headers { get { return headers; } protected set { headers = value; } } internal OnlineStatusReport() { Headers = headers; } } The idea is, I want to be able to call Report.Headers from anywhere in the assembly, but only allow it to be set by derived classes. I tried making Headers just internal, but

What is the equivalent of Java's final in C#?

大憨熊 提交于 2019-11-26 16:50:42
What is the equivalent of Java's final in C#? Noldorin The final keyword has several usages in Java. It corresponds to both the sealed and readonly keywords in C#, depending on the context in which it is used. Classes To prevent subclassing (inheritance from the defined class): Java public final class MyFinalClass {...} C# public sealed class MyFinalClass {...} Methods Prevent overriding of a virtual method. Java public class MyClass { public final void myFinalMethod() {...} } C# public class MyClass : MyBaseClass { public sealed override void MyFinalMethod() {...} } As Joachim Sauer points

Can I override a private method in Java?

守給你的承諾、 提交于 2019-11-26 16:35:52
问题 I know I can use reflection to invoke a private method, and to get or set the value of a private variable, but I want to override a method. public class SuperClass { public void printInt() { System.out.println("I am " + getClass() + ". The int is " + getInt()); } private int getInt() { return 1; } } public class SubClass extends SuperClass { public static void main(String[] args) { (new SubClass()).printInt(); } public int getInt() { return 2; } } I want the main method in SubClass to print

Practical uses for the “internal” keyword in C#

試著忘記壹切 提交于 2019-11-26 15:36:56
Could you please explain what the practical usage is for the internal keyword in C#? I know that the internal modifier limits access to the current assembly, but when and in which circumstance should I use it? Ash Utility or helper classes/methods that you would like to access from many other classes within the same assembly, but that you want to ensure code in other assemblies can't access. From MSDN (via archive.org): A common use of internal access is in component-based development because it enables a group of components to cooperate in a private manner without being exposed to the rest of

What is the meaning of the planned “private protected” C# access modifier?

 ̄綄美尐妖づ 提交于 2019-11-26 15:10:00
问题 As part of the Roslyn documentation on GitHub, there's a page called Language feature implementation status, with planned language features for C# and VB. One feature I couldn't wrap my head around was private protected access modifier: private protected string GetId() { … } There is also a page of C# Language Design Notes, which explains many new features, but not this one. Eric Lippert said in a comment: Your error is in thinking of the modifiers as increasing restrictions. The modifiers in

MVC Set accessibility level on a method called from ajax

最后都变了- 提交于 2019-11-26 14:50:01
问题 I would like to protect my public method from being called by a user. Because I'm calling the action from an ajax script I can't use any access modifiers, (private, protected etc). Also, [HttpPost] doesn't stop the user from doing a fake request. Anyone got a solution? Thanks 回答1: Create an action filter that allows action methods to be called by AJAX only namespace MyFilters { [AttributeUsage(AttributeTargets.Method)] public class AjaxOnlyAttribute : ActionFilterAttribute { public override

Default access modifier for a Java constructor

霸气de小男生 提交于 2019-11-26 13:50:16
问题 Can anybody explain what the default access modifier is for an explicit no-arg constructor (and other constructors)? 回答1: Constructors are the same as methods in this respect - if you don't give an explicit public, private or protected then the constructor gets the default "package private" visibility. It can be called from within the same class or from any other class in the same package, but not from subclasses in a different package (so if a class has only package-visible constructors then

What is the difference between Dim, Global, Public, and Private as Modular Field Access Modifiers?

大城市里の小女人 提交于 2019-11-26 12:58:52
In VB6/VBA, you can declare module-level variables outside of a specific Sub or Function method. I've used Private and Public before inside modules and understand them like so: Public - visible to all code inside the module and all code outside the module, essentially making it global. Private - visible only to code inside the module. I've noticed that you can use Dim and Global as modifiers for modular variables. Are Dim and Global different from Private and Public , respectively, when used as access modifiers on modular fields? If so, how are they different? Joe Jordan Dim and Private work