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. Coding Standard Question

Coding Standard Question

Scheduled Pinned Locked Moved C#
questioncsharp
20 Posts 9 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.
  • D donovan solms

    Which is better according to coding standard for c#? this: public bool isDraggable { get { return _draggable; } set { _draggable = value; } } or this: public bool isDraggable{ get; set; } without using private members? Or is this a matter of personal choice?

    rather have something you don't need, than need something you don't have

    P Offline
    P Offline
    pmarfleet
    wrote on last edited by
    #3

    The first code snippet would be a reasonable way to implement accessors for a property in a class. The second snippet looks like a property declaration for an Interface. I suggest you read up on the differences between classes and interfaces if you do not understand the difference between these two pieces of code.

    Paul Marfleet

    D 1 Reply Last reply
    0
    • D donovan solms

      Which is better according to coding standard for c#? this: public bool isDraggable { get { return _draggable; } set { _draggable = value; } } or this: public bool isDraggable{ get; set; } without using private members? Or is this a matter of personal choice?

      rather have something you don't need, than need something you don't have

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #4

      donsolms wrote:

      Which is better according to coding standard for c#? this: public bool isDraggable { get { return _draggable; } set { _draggable = value; } } or this: public bool isDraggable{ get; set; } without using private members? Or is this a matter of personal choice?

      Well - the 1st is a better coding standard because it actually does something. The second doesn't actually accomplish anything. You would normally use the second one in an interface.

      Deja View - the feeling that you've seen this post before.

      1 Reply Last reply
      0
      • P pmarfleet

        The first code snippet would be a reasonable way to implement accessors for a property in a class. The second snippet looks like a property declaration for an Interface. I suggest you read up on the differences between classes and interfaces if you do not understand the difference between these two pieces of code.

        Paul Marfleet

        D Offline
        D Offline
        donovan solms
        wrote on last edited by
        #5

        pmarfleet wrote:

        The second snippet looks like a property declaration for an Interface.

        it does, but you can use it in a normal class as well

        rather have something you don't need, than need something you don't have

        P 1 Reply Last reply
        0
        • D donovan solms

          pmarfleet wrote:

          The second snippet looks like a property declaration for an Interface.

          it does, but you can use it in a normal class as well

          rather have something you don't need, than need something you don't have

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #6

          donsolms wrote:

          it does, but you can use it in a normal class as well

          You can - but it doesn't actually accomplish much.

          Deja View - the feeling that you've seen this post before.

          R 1 Reply Last reply
          0
          • D donovan solms

            Which is better according to coding standard for c#? this: public bool isDraggable { get { return _draggable; } set { _draggable = value; } } or this: public bool isDraggable{ get; set; } without using private members? Or is this a matter of personal choice?

            rather have something you don't need, than need something you don't have

            M Offline
            M Offline
            Matthew Cuba
            wrote on last edited by
            #7

            donsolms wrote:

            Which is better according to coding standard for c#? this: public bool isDraggable { get { return _draggable; } set { _draggable = value; } } or this: public bool isDraggable{ get; set; } without using private members? Or is this a matter of personal choice?

            Hi, For Orcas, I thought that these were essentially identical, that the compiler would generate some private fields for you. Perhaps I've missed something - I'm living in a .NET 2.0 world - but, that was my understanding of it. I suspect someone will correct me if I'm wrong. Hope that helps.

            It isn't enough to do well in life. One must do good when and where one can. Otherwise, what's the point?

            D P S 3 Replies Last reply
            0
            • M Matthew Cuba

              donsolms wrote:

              Which is better according to coding standard for c#? this: public bool isDraggable { get { return _draggable; } set { _draggable = value; } } or this: public bool isDraggable{ get; set; } without using private members? Or is this a matter of personal choice?

              Hi, For Orcas, I thought that these were essentially identical, that the compiler would generate some private fields for you. Perhaps I've missed something - I'm living in a .NET 2.0 world - but, that was my understanding of it. I suspect someone will correct me if I'm wrong. Hope that helps.

              It isn't enough to do well in life. One must do good when and where one can. Otherwise, what's the point?

              D Offline
              D Offline
              donovan solms
              wrote on last edited by
              #8

              that's what I thought as I'm in Orcas. I'll just stick to my usual style. Thanks for everyone's feedback

              rather have something you don't need, than need something you don't have

              D 1 Reply Last reply
              0
              • D donovan solms

                that's what I thought as I'm in Orcas. I'll just stick to my usual style. Thanks for everyone's feedback

                rather have something you don't need, than need something you don't have

                D Offline
                D Offline
                Dan Neely
                wrote on last edited by
                #9

                as a general comment if you're asking about something new in a beta/just released version it's generally best to indicate it to avoid confusion.

                -- If you view money as inherently evil, I view it as my duty to assist in making you more virtuous.

                1 Reply Last reply
                0
                • M Matthew Cuba

                  donsolms wrote:

                  Which is better according to coding standard for c#? this: public bool isDraggable { get { return _draggable; } set { _draggable = value; } } or this: public bool isDraggable{ get; set; } without using private members? Or is this a matter of personal choice?

                  Hi, For Orcas, I thought that these were essentially identical, that the compiler would generate some private fields for you. Perhaps I've missed something - I'm living in a .NET 2.0 world - but, that was my understanding of it. I suspect someone will correct me if I'm wrong. Hope that helps.

                  It isn't enough to do well in life. One must do good when and where one can. Otherwise, what's the point?

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

                  Matthew Cuba wrote:

                  the compiler would generate some private fields

                  Ew, I wouldn't want that. Having the compiler create a default constructor is one thing, but generating fields? Yuck!

                  1 Reply Last reply
                  0
                  • M Matthew Cuba

                    donsolms wrote:

                    Which is better according to coding standard for c#? this: public bool isDraggable { get { return _draggable; } set { _draggable = value; } } or this: public bool isDraggable{ get; set; } without using private members? Or is this a matter of personal choice?

                    Hi, For Orcas, I thought that these were essentially identical, that the compiler would generate some private fields for you. Perhaps I've missed something - I'm living in a .NET 2.0 world - but, that was my understanding of it. I suspect someone will correct me if I'm wrong. Hope that helps.

                    It isn't enough to do well in life. One must do good when and where one can. Otherwise, what's the point?

                    S Offline
                    S Offline
                    Scott Dorman
                    wrote on last edited by
                    #11

                    Matthew Cuba wrote:

                    For Orcas, I thought that these were essentially identical

                    That is true, .NET 3.5 introduced the idea of automatic properties. The only drawback is that it doesn't provide any way to do validation and you must always have both a get and a set.

                    Scott.


                    —In just two days, tomorrow will be yesterday. [Forum Guidelines] [Articles] [Blog]

                    1 Reply Last reply
                    0
                    • D donovan solms

                      Which is better according to coding standard for c#? this: public bool isDraggable { get { return _draggable; } set { _draggable = value; } } or this: public bool isDraggable{ get; set; } without using private members? Or is this a matter of personal choice?

                      rather have something you don't need, than need something you don't have

                      S Offline
                      S Offline
                      Scott Dorman
                      wrote on last edited by
                      #12

                      My preference would almost always be the first example. The second example is good if you can guarantee that you will never need to do anything more advanced in the property getter or setter. Both of thees example are equivalent, with the second one allowing the compiler to automatically generate the backing variable. I have used both styles, and generally only use the second for very simple classes and structs. For future reference, you should be clear that you are referring to a .NET 3.5 (Orcas) feature, especially with this question as the second example is also the way a property is defined in an interface.

                      Scott.


                      —In just two days, tomorrow will be yesterday. [Forum Guidelines] [Articles] [Blog]

                      P R 2 Replies Last reply
                      0
                      • S Scott Dorman

                        My preference would almost always be the first example. The second example is good if you can guarantee that you will never need to do anything more advanced in the property getter or setter. Both of thees example are equivalent, with the second one allowing the compiler to automatically generate the backing variable. I have used both styles, and generally only use the second for very simple classes and structs. For future reference, you should be clear that you are referring to a .NET 3.5 (Orcas) feature, especially with this question as the second example is also the way a property is defined in an interface.

                        Scott.


                        —In just two days, tomorrow will be yesterday. [Forum Guidelines] [Articles] [Blog]

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

                        Scott Dorman wrote:

                        with the second one allowing the compiler to automatically generate the backing variable.

                        <rant> Man, that's gotta be about the dumbest thing I've heard Microsoft do recently. Does that mean even the class itself is required to use the property? I hope the calls will be inlined. </rant> On the other hand, maybe they're trying to entice more VB programmers to C#, that's a worthwhile goal.

                        P S 2 Replies Last reply
                        0
                        • P PIEBALDconsult

                          Scott Dorman wrote:

                          with the second one allowing the compiler to automatically generate the backing variable.

                          <rant> Man, that's gotta be about the dumbest thing I've heard Microsoft do recently. Does that mean even the class itself is required to use the property? I hope the calls will be inlined. </rant> On the other hand, maybe they're trying to entice more VB programmers to C#, that's a worthwhile goal.

                          P Offline
                          P Offline
                          Pete OHanlon
                          wrote on last edited by
                          #14

                          PIEBALDconsult wrote:

                          maybe they're trying to entice more VB programmers to C#, that's a worthwhile goal

                          That's polluting the gene pool - we wouldn't want that.

                          Deja View - the feeling that you've seen this post before.

                          1 Reply Last reply
                          0
                          • P Pete OHanlon

                            donsolms wrote:

                            it does, but you can use it in a normal class as well

                            You can - but it doesn't actually accomplish much.

                            Deja View - the feeling that you've seen this post before.

                            R Offline
                            R Offline
                            Robert Rohde
                            wrote on last edited by
                            #15

                            You are wrong. What it does is to implicitely create a private field which is accessed (I think this was introduced with .Net 3.5). Up to there you could just create a public field instead but this one has the advantage that you can replace it with a real property/field pair at any time without having to change anything on accessing classes. Robert

                            P 1 Reply Last reply
                            0
                            • S Scott Dorman

                              My preference would almost always be the first example. The second example is good if you can guarantee that you will never need to do anything more advanced in the property getter or setter. Both of thees example are equivalent, with the second one allowing the compiler to automatically generate the backing variable. I have used both styles, and generally only use the second for very simple classes and structs. For future reference, you should be clear that you are referring to a .NET 3.5 (Orcas) feature, especially with this question as the second example is also the way a property is defined in an interface.

                              Scott.


                              —In just two days, tomorrow will be yesterday. [Forum Guidelines] [Articles] [Blog]

                              R Offline
                              R Offline
                              Robert Rohde
                              wrote on last edited by
                              #16

                              Scott Dorman wrote:

                              The second example is good if you can guarantee that you will never need to do anything more advanced in the property getter or setter.

                              Well you can change it to a "real" property and field combo any time you want. Accessing classes won't notice the difference. Robert

                              S 1 Reply Last reply
                              0
                              • R Robert Rohde

                                You are wrong. What it does is to implicitely create a private field which is accessed (I think this was introduced with .Net 3.5). Up to there you could just create a public field instead but this one has the advantage that you can replace it with a real property/field pair at any time without having to change anything on accessing classes. Robert

                                P Offline
                                P Offline
                                Pete OHanlon
                                wrote on last edited by
                                #17

                                Robert Rohde wrote:

                                You are wrong. What it does is to implicitely create a private field which is accessed (I think this was introduced with .Net 3.5). Up to there you could just create a public field instead but this one has the advantage that you can replace it with a real property/field pair at any time without having to change anything on accessing classes.

                                Only from .NET 3.5 onwards. At the time of replying to this post, we had no idea that the poster was using Orcas and not asking questions about current versions of .Net.

                                Deja View - the feeling that you've seen this post before.

                                1 Reply Last reply
                                0
                                • P PIEBALDconsult

                                  Scott Dorman wrote:

                                  with the second one allowing the compiler to automatically generate the backing variable.

                                  <rant> Man, that's gotta be about the dumbest thing I've heard Microsoft do recently. Does that mean even the class itself is required to use the property? I hope the calls will be inlined. </rant> On the other hand, maybe they're trying to entice more VB programmers to C#, that's a worthwhile goal.

                                  S Offline
                                  S Offline
                                  Scott Dorman
                                  wrote on last edited by
                                  #18

                                  In theory, I agree with you. In practice, however, I don't mainly because there are already a lot of cases where Microsoft is generating code for you. If you use any .NET remoting or serialization, the JIT is compiling an entire assembly on-the-fly at runtime for you; the using statement, ~T (finalizer), anonymous delegates, and generics all generate code at compile time on your behalf. There really isn't a way around it anymore...if you use .NET at somepoint Microsoft is generating code for you either at compile time, run time, or both.

                                  PIEBALDconsult wrote:

                                  Does that mean even the class itself is required to use the property?

                                  I'm not sure what you mean by this. In the class, when you want to reference the property you use it just like you would any other property. The only difference is that there isn't an excplict backing variable that you could access instead, so you always use the property.

                                  PIEBALDconsult wrote:

                                  I hope the calls will be inlined.

                                  How would this matter? I don't believe they are, since this is really just more syntatic sugar, the compiler treats them just like it would any other property.

                                  Scott.


                                  —In just two days, tomorrow will be yesterday. [Forum Guidelines] [Articles] [Blog]

                                  1 Reply Last reply
                                  0
                                  • R Robert Rohde

                                    Scott Dorman wrote:

                                    The second example is good if you can guarantee that you will never need to do anything more advanced in the property getter or setter.

                                    Well you can change it to a "real" property and field combo any time you want. Accessing classes won't notice the difference. Robert

                                    S Offline
                                    S Offline
                                    Scott Dorman
                                    wrote on last edited by
                                    #19

                                    Robert Rohde wrote:

                                    Well you can change it to a "real" property and field combo any time you want. Accessing classes won't notice the difference.

                                    Yes, you can change it to a "real" property anytime you want without affecting the callers. My only issue with automatic properties is that it forces both a get and a set on the property and they must both be at the same access level.

                                    Scott.


                                    —In just two days, tomorrow will be yesterday. [Forum Guidelines] [Articles] [Blog]

                                    P 1 Reply Last reply
                                    0
                                    • S Scott Dorman

                                      Robert Rohde wrote:

                                      Well you can change it to a "real" property and field combo any time you want. Accessing classes won't notice the difference.

                                      Yes, you can change it to a "real" property anytime you want without affecting the callers. My only issue with automatic properties is that it forces both a get and a set on the property and they must both be at the same access level.

                                      Scott.


                                      —In just two days, tomorrow will be yesterday. [Forum Guidelines] [Articles] [Blog]

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

                                      Another issue is newbies who learn the lazy way and never learn the "better" way, and therefore wind up giving more access to their classes because they don't know any better.

                                      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