C #: code style - places for participants

I know that private fields are first. Where should the other members be located according to the C # code style?

I mean: private / protected / internal / public fields / properties / methods / events. I always posted events in the last part of the lesson. Sometimes I use a private method near a public property that uses it. I do not think this is a suitable place, but I cannot choose a place for it and for many other participants.

+3
source share
6 answers

According to StyleCop (style check done by Microsoft), the correct order is:

Adjacent elements of the same type should be arranged in the following order by access level:

  • the public
  • interior
  • protected internal
  • reserved

- SA1202

SA1201, (.. , - ,...).

StyleCop : http://stylecop.codeplex.com/releases/view/44839

+5

(. , " " ). .

, , , , ( , , , ) #region - #endregion.

, , , , , , .

+4

,

. , . , .

:

// Class layout based on accessibility
class Purchasing
{
    #region Main

    #region Public

    #region Internal

    #region Protected

    #region Private

    #region Extern

    #region Designer Generated Code
}

:

o .

o , .

o , Visual Studios, . , .

+1

, .

0

Do what ever suits you, just be consistent.

0
source

I think that if you do not mess up your code, confuse everything and combine all this, you can choose which order you prefer.

As long as you agree with this, there is no correct order.

0
source

Source: https://habr.com/ru/post/1796259/


All Articles