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. Best Way

Best Way

Scheduled Pinned Locked Moved C#
comtoolsquestionlearning
2 Posts 2 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.
  • S Offline
    S Offline
    sreejith ss nair
    wrote on last edited by
    #1

    Hi, I would like to describe two way of declaring properties (1) public string Get_Set { get { return get_set; } set { get_set=value; } } (2) public string Get_Set { get { return get_set; } set { if(get_set!=value) get_set=value; } } I would like to clear few things in property declaration. a) From the above two property declaration , which one is best while considering efficient resource utilization. b) If the above given examples are not good enough , then which one is best way of declaring a property. Comment with detailed description will be highly appreciable. Sreejith Nair [ My Articles ]

    L 1 Reply Last reply
    0
    • S sreejith ss nair

      Hi, I would like to describe two way of declaring properties (1) public string Get_Set { get { return get_set; } set { get_set=value; } } (2) public string Get_Set { get { return get_set; } set { if(get_set!=value) get_set=value; } } I would like to clear few things in property declaration. a) From the above two property declaration , which one is best while considering efficient resource utilization. b) If the above given examples are not good enough , then which one is best way of declaring a property. Comment with detailed description will be highly appreciable. Sreejith Nair [ My Articles ]

      L Offline
      L Offline
      Luis Alonso Ramos
      wrote on last edited by
      #2

      Hello, For simplicity, if it's only setting a variable and not doing something else, I'd use the first case:

      public string MyProperty
      {
          get { return myProperty;  }
          set { myProperty = value; }
      }
      

      If setting the property implies other operations, then, depending on them, I would check the previous value. For example, if when a property is set to false I need to remove an event handler, I would check before that the property was not already false, so I don't remove the handler twice. But for simple properties that only set variables, no need to check. -- LuisR


      Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!

      The amount of sleep the average person needs is five more minutes. -- Vikram A Punathambekar, Aug. 11, 2005

      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