Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# coding style question

C# coding style question

Scheduled Pinned Locked Moved C#
csharpquestiondiscussion
35 Posts 14 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gary Wheeler
    wrote on last edited by
    #1

    For static members, do you prefer

    public static Type Member;

    or

    static public Type Member;

    where public can be any of the visibility keywords. I've found I use both :-O, and I'm trying to find out if there's a best practice out there that makes sense.

    Software Zen: delete this;

    R L E J P 11 Replies Last reply
    0
    • G Gary Wheeler

      For static members, do you prefer

      public static Type Member;

      or

      static public Type Member;

      where public can be any of the visibility keywords. I've found I use both :-O, and I'm trying to find out if there's a best practice out there that makes sense.

      Software Zen: delete this;

      R Offline
      R Offline
      Richard Deeming
      wrote on last edited by
      #2

      I prefer option 1, but whichever option you pick, consistency is the key!


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      1 Reply Last reply
      0
      • G Gary Wheeler

        For static members, do you prefer

        public static Type Member;

        or

        static public Type Member;

        where public can be any of the visibility keywords. I've found I use both :-O, and I'm trying to find out if there's a best practice out there that makes sense.

        Software Zen: delete this;

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        I use both too, but tend to group the statics at the bottom of the class in it's own region.

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

        1 Reply Last reply
        0
        • G Gary Wheeler

          For static members, do you prefer

          public static Type Member;

          or

          static public Type Member;

          where public can be any of the visibility keywords. I've found I use both :-O, and I'm trying to find out if there's a best practice out there that makes sense.

          Software Zen: delete this;

          E Offline
          E Offline
          Espen Harlinn
          wrote on last edited by
          #4

          I prefer to put visibility first, and for what it's worth, I don't think I would make a static field public.

          Espen Harlinn Principal Architect, Software - Goodtech Projects & Services AS Projects promoting programming in "natural language" are intrinsically doomed to fail. Edsger W.Dijkstra

          G 1 Reply Last reply
          0
          • G Gary Wheeler

            For static members, do you prefer

            public static Type Member;

            or

            static public Type Member;

            where public can be any of the visibility keywords. I've found I use both :-O, and I'm trying to find out if there's a best practice out there that makes sense.

            Software Zen: delete this;

            J Offline
            J Offline
            jschell
            wrote on last edited by
            #5

            I prefer the following.

            static public...
            private static...

            I do it that way to make the basic nature of the items more visible to all.

            G 1 Reply Last reply
            0
            • G Gary Wheeler

              For static members, do you prefer

              public static Type Member;

              or

              static public Type Member;

              where public can be any of the visibility keywords. I've found I use both :-O, and I'm trying to find out if there's a best practice out there that makes sense.

              Software Zen: delete this;

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              I'm with Richard Deeming. Additionally, I always specify the access modifier, I never allow a default to apply -- I think the defaults should be removed from the language.

              G 1 Reply Last reply
              0
              • G Gary Wheeler

                For static members, do you prefer

                public static Type Member;

                or

                static public Type Member;

                where public can be any of the visibility keywords. I've found I use both :-O, and I'm trying to find out if there's a best practice out there that makes sense.

                Software Zen: delete this;

                B Offline
                B Offline
                BillWoodruff
                wrote on last edited by
                #7

                I agree with Richard Deeming's response focusing on "consistency." And, I emphatically agree with PIEBALDConsult's response, when he says/implies that one should always specify the scope with Public/Private, and that it would be a good thing for the .NET compiler to "enforce" this. I prefer to use: public/private static variabletype. It's interesting to me that if you write (C#, WinForms) inside the scope of a Form's class definition:

                public static bool availableOutsideFormClassScope = true;

                That ReSharper 8 will "suggest" that the use of 'public is redundant. But, if you write this:

                static bool notAvailableOutsideFormClassScope = true;

                , you have, in effect, declared this variable as private. I have never had occasion to use a Form scoped static variable, method, embedded class, etc; I only use static when I want something to be accessible outside a Form's class scope. But, perhaps if I were dealing with multiple instance of the same application, or multi-threading, there would be an "organic" need for this ? When I write applications where there are multiple independent Forms (in WinForms this can be done by modifying the Program.cs file so it calls an initializer method to "run" the application ... and that initializer creates and "shows" all Form instances ... rather than doing the usual: Application.Run(new SomeMainForm()); ... in that circumstance I usually use a ststic class defined as an an application top-level class (i.e., added via VS's Project/Add Class menu). I know there are people who feel an almost "religious zeal" about not using static anything/anywhere, perceiving the use of static whatever as a violation of the No-Globals-Strongly-Typed supreme being, and doomed to provoke the wrath of the Threading sub-deities :) I have yet to have a cause to feel such a conviction. Bill

                Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview

                G N P 3 Replies Last reply
                0
                • G Gary Wheeler

                  For static members, do you prefer

                  public static Type Member;

                  or

                  static public Type Member;

                  where public can be any of the visibility keywords. I've found I use both :-O, and I'm trying to find out if there's a best practice out there that makes sense.

                  Software Zen: delete this;

                  R Offline
                  R Offline
                  Rakesh Meel
                  wrote on last edited by
                  #8

                  i suggest frist one style.

                  1 Reply Last reply
                  0
                  • G Gary Wheeler

                    For static members, do you prefer

                    public static Type Member;

                    or

                    static public Type Member;

                    where public can be any of the visibility keywords. I've found I use both :-O, and I'm trying to find out if there's a best practice out there that makes sense.

                    Software Zen: delete this;

                    R Offline
                    R Offline
                    Ricardo Kajihara
                    wrote on last edited by
                    #9

                    I always like to use the access modifier first.

                    1 Reply Last reply
                    0
                    • G Gary Wheeler

                      For static members, do you prefer

                      public static Type Member;

                      or

                      static public Type Member;

                      where public can be any of the visibility keywords. I've found I use both :-O, and I'm trying to find out if there's a best practice out there that makes sense.

                      Software Zen: delete this;

                      C Offline
                      C Offline
                      Christopher Kenis
                      wrote on last edited by
                      #10

                      I'm with Richard Deeming here, I use the first method myself. As Richard said, consistency is the key!

                      1 Reply Last reply
                      0
                      • B BillWoodruff

                        I agree with Richard Deeming's response focusing on "consistency." And, I emphatically agree with PIEBALDConsult's response, when he says/implies that one should always specify the scope with Public/Private, and that it would be a good thing for the .NET compiler to "enforce" this. I prefer to use: public/private static variabletype. It's interesting to me that if you write (C#, WinForms) inside the scope of a Form's class definition:

                        public static bool availableOutsideFormClassScope = true;

                        That ReSharper 8 will "suggest" that the use of 'public is redundant. But, if you write this:

                        static bool notAvailableOutsideFormClassScope = true;

                        , you have, in effect, declared this variable as private. I have never had occasion to use a Form scoped static variable, method, embedded class, etc; I only use static when I want something to be accessible outside a Form's class scope. But, perhaps if I were dealing with multiple instance of the same application, or multi-threading, there would be an "organic" need for this ? When I write applications where there are multiple independent Forms (in WinForms this can be done by modifying the Program.cs file so it calls an initializer method to "run" the application ... and that initializer creates and "shows" all Form instances ... rather than doing the usual: Application.Run(new SomeMainForm()); ... in that circumstance I usually use a ststic class defined as an an application top-level class (i.e., added via VS's Project/Add Class menu). I know there are people who feel an almost "religious zeal" about not using static anything/anywhere, perceiving the use of static whatever as a violation of the No-Globals-Strongly-Typed supreme being, and doomed to provoke the wrath of the Threading sub-deities :) I have yet to have a cause to feel such a conviction. Bill

                        Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview

                        G Offline
                        G Offline
                        Gary Wheeler
                        wrote on last edited by
                        #11

                        BillWoodruff wrote:

                        I know there are people who feel an almost "religious zeal" about not using static anything/anywhere, perceiving the use of static whatever as a violation of the No-Globals-Strongly-Typed supreme being

                        I have a feeling you and I are of a vintage, Bill :). We are grizzled combat veterans of the FORTRAN wars. We remember when men were men, women created compilers[^], and you knew how to handle globals safely.

                        Software Zen: delete this;

                        B 1 Reply Last reply
                        0
                        • E Espen Harlinn

                          I prefer to put visibility first, and for what it's worth, I don't think I would make a static field public.

                          Espen Harlinn Principal Architect, Software - Goodtech Projects & Services AS Projects promoting programming in "natural language" are intrinsically doomed to fail. Edsger W.Dijkstra

                          G Offline
                          G Offline
                          Gary Wheeler
                          wrote on last edited by
                          #12

                          Espen Harlinn wrote:

                          I don't think I would make a static field public

                          I normally don't, but for the purposes of the example...

                          Software Zen: delete this;

                          E 1 Reply Last reply
                          0
                          • G Gary Wheeler

                            Espen Harlinn wrote:

                            I don't think I would make a static field public

                            I normally don't, but for the purposes of the example...

                            Software Zen: delete this;

                            E Offline
                            E Offline
                            Espen Harlinn
                            wrote on last edited by
                            #13

                            :thumbsup: Fair enough ;) But then you never know who is following these discussions, and some will surely copy your code without a second thought ...

                            Espen Harlinn Principal Architect, Software - Goodtech Projects & Services AS Projects promoting programming in "natural language" are intrinsically doomed to fail. Edsger W.Dijkstra

                            G 1 Reply Last reply
                            0
                            • J jschell

                              I prefer the following.

                              static public...
                              private static...

                              I do it that way to make the basic nature of the items more visible to all.

                              G Offline
                              G Offline
                              Gary Wheeler
                              wrote on last edited by
                              #14

                              Hmm. Some of my usage variation seems like that, like my intent was to call attention to the fact something was static.

                              Software Zen: delete this;

                              P 1 Reply Last reply
                              0
                              • P PIEBALDconsult

                                I'm with Richard Deeming. Additionally, I always specify the access modifier, I never allow a default to apply -- I think the defaults should be removed from the language.

                                G Offline
                                G Offline
                                Gary Wheeler
                                wrote on last edited by
                                #15

                                I always specify the access modifier as well. I've always distrusted my memory when it comes to default behaviors with subtle consequences. I explicitly parenthesize as well :-O.

                                Software Zen: delete this;

                                1 Reply Last reply
                                0
                                • E Espen Harlinn

                                  :thumbsup: Fair enough ;) But then you never know who is following these discussions, and some will surely copy your code without a second thought ...

                                  Espen Harlinn Principal Architect, Software - Goodtech Projects & Services AS Projects promoting programming in "natural language" are intrinsically doomed to fail. Edsger W.Dijkstra

                                  G Offline
                                  G Offline
                                  Gary Wheeler
                                  wrote on last edited by
                                  #16

                                  If they're copying the millifragment I posted, they're in more trouble than they know.

                                  Software Zen: delete this;

                                  E 1 Reply Last reply
                                  0
                                  • G Gary Wheeler

                                    If they're copying the millifragment I posted, they're in more trouble than they know.

                                    Software Zen: delete this;

                                    E Offline
                                    E Offline
                                    Espen Harlinn
                                    wrote on last edited by
                                    #17

                                    Agree, but then, on the other hand, they probably wouldn't notice ...

                                    Espen Harlinn Principal Architect, Software - Goodtech Projects & Services AS Projects promoting programming in "natural language" are intrinsically doomed to fail. Edsger W.Dijkstra

                                    1 Reply Last reply
                                    0
                                    • G Gary Wheeler

                                      Hmm. Some of my usage variation seems like that, like my intent was to call attention to the fact something was static.

                                      Software Zen: delete this;

                                      P Offline
                                      P Offline
                                      PIEBALDconsult
                                      wrote on last edited by
                                      #18

                                      Then you should have said so.

                                      1 Reply Last reply
                                      0
                                      • B BillWoodruff

                                        I agree with Richard Deeming's response focusing on "consistency." And, I emphatically agree with PIEBALDConsult's response, when he says/implies that one should always specify the scope with Public/Private, and that it would be a good thing for the .NET compiler to "enforce" this. I prefer to use: public/private static variabletype. It's interesting to me that if you write (C#, WinForms) inside the scope of a Form's class definition:

                                        public static bool availableOutsideFormClassScope = true;

                                        That ReSharper 8 will "suggest" that the use of 'public is redundant. But, if you write this:

                                        static bool notAvailableOutsideFormClassScope = true;

                                        , you have, in effect, declared this variable as private. I have never had occasion to use a Form scoped static variable, method, embedded class, etc; I only use static when I want something to be accessible outside a Form's class scope. But, perhaps if I were dealing with multiple instance of the same application, or multi-threading, there would be an "organic" need for this ? When I write applications where there are multiple independent Forms (in WinForms this can be done by modifying the Program.cs file so it calls an initializer method to "run" the application ... and that initializer creates and "shows" all Form instances ... rather than doing the usual: Application.Run(new SomeMainForm()); ... in that circumstance I usually use a ststic class defined as an an application top-level class (i.e., added via VS's Project/Add Class menu). I know there are people who feel an almost "religious zeal" about not using static anything/anywhere, perceiving the use of static whatever as a violation of the No-Globals-Strongly-Typed supreme being, and doomed to provoke the wrath of the Threading sub-deities :) I have yet to have a cause to feel such a conviction. Bill

                                        Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview

                                        N Offline
                                        N Offline
                                        Nicholas Marty
                                        wrote on last edited by
                                        #19

                                        Just to clarify:

                                        static bool notAvailableOutsideFormClassScope = true;

                                        is in fact NOT private. Neither is it public. The default is internal (scope=assembly). As a default this makes the most sense in my opinion (regardless if having a default makes sense at all) I still specify the visibility for every class (and member). Just because I don't like relying on defaults ;) Edit: See Access Modifiers (C# Programming Guide) (MSDN)[^] First paragraph of the section "Class and Struct Accessibility" Edit2: Regarding Resharper (At least in Version 6.1) you let Resharper suggest explicit access modifiers: :) -> Resharper -> Options... -> Code Editing -> Formatting Style -> Other -> Modifiers -> "Use explicit private modifier" / "Use explicit internal modifier"

                                        B 1 Reply Last reply
                                        0
                                        • B BillWoodruff

                                          I agree with Richard Deeming's response focusing on "consistency." And, I emphatically agree with PIEBALDConsult's response, when he says/implies that one should always specify the scope with Public/Private, and that it would be a good thing for the .NET compiler to "enforce" this. I prefer to use: public/private static variabletype. It's interesting to me that if you write (C#, WinForms) inside the scope of a Form's class definition:

                                          public static bool availableOutsideFormClassScope = true;

                                          That ReSharper 8 will "suggest" that the use of 'public is redundant. But, if you write this:

                                          static bool notAvailableOutsideFormClassScope = true;

                                          , you have, in effect, declared this variable as private. I have never had occasion to use a Form scoped static variable, method, embedded class, etc; I only use static when I want something to be accessible outside a Form's class scope. But, perhaps if I were dealing with multiple instance of the same application, or multi-threading, there would be an "organic" need for this ? When I write applications where there are multiple independent Forms (in WinForms this can be done by modifying the Program.cs file so it calls an initializer method to "run" the application ... and that initializer creates and "shows" all Form instances ... rather than doing the usual: Application.Run(new SomeMainForm()); ... in that circumstance I usually use a ststic class defined as an an application top-level class (i.e., added via VS's Project/Add Class menu). I know there are people who feel an almost "religious zeal" about not using static anything/anywhere, perceiving the use of static whatever as a violation of the No-Globals-Strongly-Typed supreme being, and doomed to provoke the wrath of the Threading sub-deities :) I have yet to have a cause to feel such a conviction. Bill

                                          Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview

                                          P Offline
                                          P Offline
                                          PIEBALDconsult
                                          wrote on last edited by
                                          #20

                                          Well if all you're writing is front-end stuff, then you wouldn't have many public members anyway. I write mostly back-end library stuff so a great many of my members are public static methods, but not many fields. A few examples:

                                          public static readonly System.Type BaseType = typeof(T) ;
                                          public static readonly Rational MaxValue = new Rational ( decimal.MaxValue , 1M ) ;
                                          public static event ErrorDelegate OnError ;
                                          public static ValueOf<T> operator + ( ValueOf<T> Op1 , ValueOf<T> Op2 ) ...
                                          public static System.Reflection.PropertyInfo CommandTextAttributeProperty { get ; private set ; }

                                          Consistency allowed me to find these easily.

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups