Access Specifiers in MSDN [duplicate] - c#

I've read around on the internet and I've heard people say
Access specifiers ::
The access specifier determines how
accessible the field is to code in
other classes. Access ranges from
totally accessible to totally
inaccessible. You can optionally
declare a field with an access
specifier keyword: public, private,
or protected.
Access Modifiers ::
You can optionally declare a field
with a modifier keyword: final or
volatile and/or static and/or
transient, abstract, etc.
Is there any difference at all? Because most definitions for access modifiers and access specifiers state the same thing.. which seems so ambiguous.

In this context, you can think of access specifiers as protection specifiers -- they specify where a variable can be accessed from. By contrast, access modifiers are completely different; they specify how variables should (or should not) be accessed; e.g. read-only, volatile, etc.
i.e., a variable can be public but read-only, or it can be private and writable -- the access specifiers have nothing to do with the modifiers.
However, I'm a little surprised that the terminology is for C#, since Microsoft actually calls public and private "access modifiers", and it calls volatile and readonly just plain "modifiers".

As far as I can see, there is no difference at all between the terms. The MS C++ documentation for example uses both terms for the same thing.

Refer from MSDN C# 4.0 document, I think you misunderstand something. C# does not has some word like "access specifier" but it call modifiers or access modifiers for modifiers that use to control access level.
Hope this help.
Update
I think, this question like you ask me that what difference between package and namespace is. It depends on what language or professor that you use or believe. Nothing is wrong on same kind like this question.
From experience (and the meaning of words), access modifier should mean about controlling access like public or private. In the other hand, Access Specifiers should be the super class of access modifier that mean it include other keyword like static, final, readonly or something like that in it.
PS. Both C#, VB.NET and Java, they use access modifier for keyword like public or private. In the other hand, C++ use access specifier for the same keyword.
Modifiers (C# 4.0)
Access Levels in Visual Basic (VB.NET 2010)
Controlling Access to Members of a Class (Java)
Access Specifiers (C++ 2010)

I believe an access specifier determines the visibility for a certain field / method.
An access modifier tells you more about the behavior and use of that field. You can say whether or not the field requires an instantiated object or if it can be overriden.

Access specifiers of a class indicates what other class variables can access in the present class.
Access modifiers of a class indicates the same functionality of access specifiers

Related

What is the default access modifier for a variable in Unity, and why?

This is a somewhat more complicated question than it might appear.
Every Unity video and tutorial that discusses access modifiers that I can find tells me that "private is the default access modifier for member variables in C#", but according to the C# documentation the default access modifier should be internal.
E.g., this official video, timestamped to the point where the speaker says this: https://youtu.be/_0oBLCJcpCs?t=224
E.g., this question on Unity Answers where the approved answer says the default is private: https://answers.unity.com/questions/1252048/private-vs-no-access-modifier-c.html
E.g., this forum thread where the answerer says the default access modifier in C# is private and cites a documentation page that says otherwise! https://forum.unity.com/threads/c-method-access-modifer.156749/
So, the question is, what is the true story of what's going on here? If I don't put an access modifier on a variable in a unity script it does act private, but what is Unity doing to make this happen!? Somehow I cannot find an explanation online.
See also this unanswered question on Unity Answers.
I think you are confused by the scope here: The default access modifier for members (fields and methods) is private, while the default access modifier for classes and interfaces is internal. That's because classes cannot be declared private (that would mean they're only visible within themselves, which doesn't make sense). An exception are inner classes (classes within classes), but for these, separate default rules apply.
It's good style to always explicitly declare the access modifier for all objects.

Accessibility levels in a single class project

I have a very simple utility program I have built in C#. It only has one namespace and one class. I am just wondering what the best practice is in regards to adding accessibility keywords to the methods/variables in this context. Is it okay to just leave the accessibility keyword out here? It seems to me that adding one will be unproductive, but the methods just feels so "naked" without one.
Best Practice is usually quoted as this:
Make everything as restrictive as possible and only unrestrict when neccessary
The defaults are this in C#
classes: internal
class members: private
Private classes unless nested are rarely useful, but never mind that.
Now the last bit is my opinion. Code readability and ease of understanding is sooooo important and so I would say that it best to explicitly put the access levels in even if they are the same as the default
Leaving them out is the same as whatever the most restrictive possible is (so private for within a class and internal for a class itself within a namespace).
Technically it makes no difference whether you explicitly write private or leave it out. As a matter of convention it's more common to be explicit (include the private) and it's good to get into the habit of following common convention, so worth doing for that matter alone.
Leaving off an access modifier when declaring a code element means the compiler will provide a default access level. In your case, the class (a top-level type) would be internal and its members would be private. This is suitable for a standalone utility which you do not expect to be referenced by any other code (internal means "only this assembly (EXE/DLL) can see this" and private means "only the class/struct that declares this can see this"). Whether you want to include or leave off the keywords is a matter of style.
Mark them as they would be used if they were in a larger application.
You never know when you might migrate this code into a larger app.
Also, 6 or 7 letters to be explicit and not have yourself or someone else later on wondering what your intentions were seems a small price to pay

Default access specifier of Main()

What is the default access specifier of Main method in C#?
If the default access specifier of static void Main() is private, then how does an external entity eg. OS invoke this method?
Any foreign process should not be able to call this method.
What is the default access specifier of Main method in C#?
The default access specifier of all methods is private.
Then how does an external entity such as the OS invoke this method?
It doesn't. The main method is invoked by the CLR. Since the CLR is the thing that is enforcing the semantics of privacy it can ignore it.
But that is not actually the right answer. The right answer is to say that your question reveals that you have a common but incorrect idea of what "private" applies to. "Private" does not mean "this method cannot be called from an external entity".
Rather, access modifiers apply to the name of the thing. That is, an access modifier determines the accessibility domain of the name of a thing: it determines the region of code in which the name could possibly mean the thing in question. The private modifier means "the accessibility domain of this entity is the entire body of the type in which it is declared". Any attempt to look up that name outside of that accessibility domain will not result in name resolution choosing the entity. Either name resolution will choose something else, or name resolution will fail.
It is absolutely possible to call a private method via some other mechanism. You can make a delegate to it and pass it around. You can use private reflection if you are sufficiently trusted. And so on. The invocation of the main method is just such a mechanism; it doesn't look up Main by name in the first place!
First, the OS knows nothing about your Main.
The CLR (which calls Main) can call it, no matter what access level you specified for it (internal, private, protected, public). You can even view and call/use all methods/types/interfaces from external libraries yourself using reflection (yes, internal and private, too).
Its default access specifier is private, although it also could be public or internal.
from msdn:
http://msdn.microsoft.com/zh-cn/library/acy3edy3
Further more, we wonder why the private method called by the external. I think that, access levels(public,internal,protected,private) defined in C# are just limited by compiler, it's a strict rule for IDE and developers, but to CLR and OS, they don't have to obey this rule, they can call any function or method from reflected address, like we could invoke method by reflecting, so the private Main method could be the entry.

C#: Why are accessibility modifiers for members optional (rather than required)?

I've read this very well written blog post by Eric Lippert, but it doesn't explain the specific case of accessibility modifiers.
Conventional stackoverflow wisdom says to always specify "private" explicitly, which would imply that members should have required the modifier.
Well It's not like there's no default modifiers set if not defined. In C# a default modifier is set even if you don't specify so at times you might want to know your default modifiers for your program to execute your way (intended result).
An enum, struct, or class will default to internal under typical conditions (in a namespace or just sitting there alone in a file), but will default to private when declared inside a struct or class.
All methods, fields, and properties has default access modifier as "Private".
Possibly the designers of C# wanted and liked "religious" fights on whether or not to include refundant access modifiers?
(In cases where only one access level is possible, access modifiers are usually disallowed: For example, a member of an interface or an enum is always public but it's not allowed to write that. A partial method is always private but you can't write that. Since a static constructor can't be called explicitly, there's no meaningful access level. An explicit interface implementation is not really visible for direct call, but is freely accessible through the interface type (if the interface type is visible from the outside). Namespaces can have no access modifier.)
The opposite rule from what you suggest, would of course be a rule saying that it is not allowed to specify a modifier that doesn't change anything. That would mean that a direct member of a namespace (including the implicit "global namespace") could not be explicitly declared internal, and that a member of a class or struct (including nested types) could not have the keyword private. Some people use this convention.
Note that getters and setters of properties and indexers cannot specify a redundant access level. The only thing allowed here is for just one of the two accessors (there must be two in that case) to specify a more strict access.
As is usually the case with this type of question, a definitive answer could only come from those who created the language (assuming it is not addressed in the documentation, of course).
But it's probably safe to assume that it is because, in the words of David Naugler, in addition to being a C-based language, "C# has been strongly influenced by Java [and] C++ and is best viewed as a descendant of both(...)."
As a side note, Anders Hejlsberg, the original author of Turbo Pascal and the chief architect of Delphi, is also the lead architect behind the creation of C#.
I realise this is sort of dodging the question, so you may now go ask why does C++ originally allow for optional visibility modifiers =p

Difference between access specifier and access modifier

I've read around on the internet and I've heard people say
Access specifiers ::
The access specifier determines how
accessible the field is to code in
other classes. Access ranges from
totally accessible to totally
inaccessible. You can optionally
declare a field with an access
specifier keyword: public, private,
or protected.
Access Modifiers ::
You can optionally declare a field
with a modifier keyword: final or
volatile and/or static and/or
transient, abstract, etc.
Is there any difference at all? Because most definitions for access modifiers and access specifiers state the same thing.. which seems so ambiguous.
In this context, you can think of access specifiers as protection specifiers -- they specify where a variable can be accessed from. By contrast, access modifiers are completely different; they specify how variables should (or should not) be accessed; e.g. read-only, volatile, etc.
i.e., a variable can be public but read-only, or it can be private and writable -- the access specifiers have nothing to do with the modifiers.
However, I'm a little surprised that the terminology is for C#, since Microsoft actually calls public and private "access modifiers", and it calls volatile and readonly just plain "modifiers".
As far as I can see, there is no difference at all between the terms. The MS C++ documentation for example uses both terms for the same thing.
Refer from MSDN C# 4.0 document, I think you misunderstand something. C# does not has some word like "access specifier" but it call modifiers or access modifiers for modifiers that use to control access level.
Hope this help.
Update
I think, this question like you ask me that what difference between package and namespace is. It depends on what language or professor that you use or believe. Nothing is wrong on same kind like this question.
From experience (and the meaning of words), access modifier should mean about controlling access like public or private. In the other hand, Access Specifiers should be the super class of access modifier that mean it include other keyword like static, final, readonly or something like that in it.
PS. Both C#, VB.NET and Java, they use access modifier for keyword like public or private. In the other hand, C++ use access specifier for the same keyword.
Modifiers (C# 4.0)
Access Levels in Visual Basic (VB.NET 2010)
Controlling Access to Members of a Class (Java)
Access Specifiers (C++ 2010)
I believe an access specifier determines the visibility for a certain field / method.
An access modifier tells you more about the behavior and use of that field. You can say whether or not the field requires an instantiated object or if it can be overriden.
Access specifiers of a class indicates what other class variables can access in the present class.
Access modifiers of a class indicates the same functionality of access specifiers

Categories