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# properties

C# properties

Scheduled Pinned Locked Moved C#
questioncsharp
3 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Hi, If we use Properties in our Class we would need one "local" variable to actually hold the value and one to actually be accessed from the outside... private string name; // local public string Name // access outside {  get  {   return name;  }  set  {   name = value;  } } So basically my question is... are there TWO variables or only ONE... Is the public one just for asthetic reasons? If there are two it would be wasting space everytime we use a Property wouldn't it? Also the "value" parameter - is that always assumed in Properties, cause I don't see any explicit mention of it anywhere? Thanks. Mohnish

    A S 2 Replies Last reply
    0
    • L Lost User

      Hi, If we use Properties in our Class we would need one "local" variable to actually hold the value and one to actually be accessed from the outside... private string name; // local public string Name // access outside {  get  {   return name;  }  set  {   name = value;  } } So basically my question is... are there TWO variables or only ONE... Is the public one just for asthetic reasons? If there are two it would be wasting space everytime we use a Property wouldn't it? Also the "value" parameter - is that always assumed in Properties, cause I don't see any explicit mention of it anywhere? Thanks. Mohnish

      A Offline
      A Offline
      Anand Amirineni
      wrote on last edited by
      #2

      I think ONE. ( may be this approach can help us) As we know , this the way of accessing member variables( state of the class) through methods(get,set) without breaking the client code. Often memeber variables (State) need to be computed.(ex: get and set to/from DB),then this way of accessing will be useful. I think this the trade-off.(If we really wasting space) Check your MSIL using ILDASM.exe( NET FWk/SDK/ bin). in your example, NAME is property and name is member variable. Next, value is implicit parameter for "set" method CLR reads your program as set(value) { membervariable = value; } Thanks, Anand.

      1 Reply Last reply
      0
      • L Lost User

        Hi, If we use Properties in our Class we would need one "local" variable to actually hold the value and one to actually be accessed from the outside... private string name; // local public string Name // access outside {  get  {   return name;  }  set  {   name = value;  } } So basically my question is... are there TWO variables or only ONE... Is the public one just for asthetic reasons? If there are two it would be wasting space everytime we use a Property wouldn't it? Also the "value" parameter - is that always assumed in Properties, cause I don't see any explicit mention of it anywhere? Thanks. Mohnish

        S Offline
        S Offline
        Sito Dekker
        wrote on last edited by
        #3

        In your example there is only ONE real member variable: name. Indeed, "Name" just exists just for aesthetic reasons. Your example doesn't yet show the advantages of using Properties. These arise if you add e.g. bounds-checking code to your get and/or set accessors. Sito Dekker

        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