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. Shortened version of setting and getting properties

Shortened version of setting and getting properties

Scheduled Pinned Locked Moved C#
securityquestionannouncement
4 Posts 3 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.
  • B Offline
    B Offline
    Brendan Vogt
    wrote on last edited by
    #1

    Hi, Is there any difference in the following with regards to security? private int _orderId; public int OrderId {    get { return _orderId; }    set { _orderId = value; } } ...and... public int OrderId { get; set; } When should we use the one and not the other? Regards

    N D 2 Replies Last reply
    0
    • B Brendan Vogt

      Hi, Is there any difference in the following with regards to security? private int _orderId; public int OrderId {    get { return _orderId; }    set { _orderId = value; } } ...and... public int OrderId { get; set; } When should we use the one and not the other? Regards

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      .NET Enthusiast wrote:

      Is there any difference in the following with regards to security?

      No. The only difference is compiler will generate the backing field for automated properties.

      .NET Enthusiast wrote:

      When should we use the one and not the other?

      If you want more control over the getter and setter, you need to use the first one. For example, when OrderId is set, you need to fire an event. Something like,

      private int _orderId;
      public int OrderId
      {
      get { return _orderId; }
      set { _
      orderId = value;
      PropertyChanged(/* ... */);
      }
      }

      In all other cases, automated properties are just fine. :)

      Best wishes, Navaneeth

      B 1 Reply Last reply
      0
      • B Brendan Vogt

        Hi, Is there any difference in the following with regards to security? private int _orderId; public int OrderId {    get { return _orderId; }    set { _orderId = value; } } ...and... public int OrderId { get; set; } When should we use the one and not the other? Regards

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        In addition to the already correct answer you have, when using automatic properties you must have both a getter and setter. If you wish to have a read only property you have to mark the setter as private. If using your own backing field you only have to provide the getter.

        Dave
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
        Why are you using VB6? Do you hate yourself? (Christian Graus)

        1 Reply Last reply
        0
        • N N a v a n e e t h

          .NET Enthusiast wrote:

          Is there any difference in the following with regards to security?

          No. The only difference is compiler will generate the backing field for automated properties.

          .NET Enthusiast wrote:

          When should we use the one and not the other?

          If you want more control over the getter and setter, you need to use the first one. For example, when OrderId is set, you need to fire an event. Something like,

          private int _orderId;
          public int OrderId
          {
          get { return _orderId; }
          set { _
          orderId = value;
          PropertyChanged(/* ... */);
          }
          }

          In all other cases, automated properties are just fine. :)

          Best wishes, Navaneeth

          B Offline
          B Offline
          Brendan Vogt
          wrote on last edited by
          #4

          Thanks Navaneeth :)

          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