Default Visibility for C# Classes and Members (Fields, Methods, etc.)

Default visibility for C# classes and members (fields, methods, etc.)?

All of the information you are looking for can be found here and here (thanks Reed Copsey):

From the first link:

Classes and structs that are declared directly within a namespace (in other words, that are not nested within other classes or structs) can be either public or internal. Internal is the default if no access modifier is specified.

...

The access level for class members and struct members, including nested classes and structs, is private by default.

...

interfaces default to internal access.

...

Delegates behave like classes and structs. By default, they have internal access when declared directly within a namespace, and private access when nested.


From the second link:

Top-level types, which are not nested in other types, can only have internal or public accessibility. The default accessibility for these types is internal.

And for nested types:


Members of Default member accessibility
---------- ----------------------------
enum public
class private
interface public
struct private

Is a C# field private by default?

Yes, it is private by default. More discussion around it in this and this question.

Isn't interface public by default?

Interface members are public, but the interface itself will default to internal if no accessibility is set, in the same way a class will.

As far as the members being public, that's not exactly 'by default': they are always public, and attempting to set an accessibility on them will result in a compilation error.

Accessibility inside a class

Please have a read of http://msdn.microsoft.com/en-us/library/ms173121.aspx

Struct members, including nested classes and structs, can be declared as public, internal, or private. Class members, including nested classes and structs, can be public, protected internal, protected, internal, or private. The access level for class members and struct members, including nested classes and structs, is private by default. Private nested types are not accessible from outside the containing type.

(emphasis mine).

Private & Public declarations in C#

No. You need to specify visibility for each member.

private is default for members, so it is safe to omit it (unless your coding guidelines tell you must specify). More details/links - Default visibility for C# classes and members (fields, methods, etc)?

Should I use internal or public visibility by default?

I believe in blackboxes where possible. As a programmer, I want a well defined blackbox which I can easily drop into my systems, and have it work. I give it values, call the appropriate methods, and then get my results back out of it.

To that end, give me only the functionality that the class needs to expose to work.

Consider an elevator. To get it to go to a floor, I push a button. That's the public interface to the black box which activates all the functions needed to get the elevator to the desired floor.

How to access to a list of a class globally

if you are on the same assembly you can use internal:

namespace Example
{
internal class TechCollection : List<Tech>
{
}
}

or if you want to access from anywhere use public:

  namespace Example
{
public class TechCollection : List<Tech>
{
}
}

Order of items in classes: Fields, Properties, Constructors, Methods

According to the StyleCop Rules Documentation the ordering is as follows.

Within a class, struct or interface: (SA1201 and SA1203)

  • Constant Fields
  • Fields
  • Constructors
  • Finalizers (Destructors)
  • Delegates
  • Events
  • Enums
  • Interfaces (interface implementations)
  • Properties
  • Indexers
  • Methods
  • Structs
  • Classes

Within each of these groups order by access: (SA1202)

  • public
  • internal
  • protected internal
  • protected
  • private

Within each of the access groups, order by static, then non-static: (SA1204)

  • static
  • non-static

Within each of the static/non-static groups of fields, order by readonly, then non-readonly : (SA1214 and SA1215)

  • readonly
  • non-readonly

An unrolled list is 130 lines long, so I won't unroll it here. The methods part unrolled is:

  • public static methods
  • public methods
  • internal static methods
  • internal methods
  • protected internal static methods
  • protected internal methods
  • protected static methods
  • protected methods
  • private static methods
  • private methods

The documentation notes that if the prescribed order isn't suitable - say, multiple interfaces are being implemented, and the interface methods and properties should be grouped together - then use a partial class to group the related methods and properties together.

Should you use the private access modifier if it's redundant?

I think explicity stating private helps in readability. It won't allow for a programmer to interpret its visibility differently.



Related Topics



Leave a reply



Submit