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. get set property

get set property

Scheduled Pinned Locked Moved C#
question
4 Posts 4 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
    lakhwinder ghuman
    wrote on last edited by
    #1

    what is the use of get set properties in C :) #

    K U 2 Replies Last reply
    0
    • L lakhwinder ghuman

      what is the use of get set properties in C :) #

      K Offline
      K Offline
      King Julien
      wrote on last edited by
      #2

      Properties are a better way of controlling your private member variables. get and set accessors are used to facilitate this access control. assume you have a private member private int count; Now you want to assign some value to the variable and need to read it whenever required. You can safely reveal your count variable to external world using a property declaration like this:

      public int Counter
      {
      get
      {
      return count;
      }

      set
      {
      count = value;
      }
      }

      "There are a few reasons to use properties, instead of public fields. One is that properties can be virtual. Another is that you can make the setters for a property private. Another is that properties have a 'special' meaning to things that inspect classes at runtime. There are frameworks for conveniently talking to databases and for reading and writing objects to and from XML and all sorts of other things -- and they automatically look at the object's properties (and not private fields or other things) to see how to do their job." For better clarification i would suggest you to buy a C# book and spend some time in learning the basics..... rather than asking the same in the forums.....

      Have a Happy Coding.....

      OriginalGriffO 1 Reply Last reply
      0
      • K King Julien

        Properties are a better way of controlling your private member variables. get and set accessors are used to facilitate this access control. assume you have a private member private int count; Now you want to assign some value to the variable and need to read it whenever required. You can safely reveal your count variable to external world using a property declaration like this:

        public int Counter
        {
        get
        {
        return count;
        }

        set
        {
        count = value;
        }
        }

        "There are a few reasons to use properties, instead of public fields. One is that properties can be virtual. Another is that you can make the setters for a property private. Another is that properties have a 'special' meaning to things that inspect classes at runtime. There are frameworks for conveniently talking to databases and for reading and writing objects to and from XML and all sorts of other things -- and they automatically look at the object's properties (and not private fields or other things) to see how to do their job." For better clarification i would suggest you to buy a C# book and spend some time in learning the basics..... rather than asking the same in the forums.....

        Have a Happy Coding.....

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        To add to this most excellent reply, they also aid with encapsulation. Because you expose a method rather than a field with get, set, the implementing class can replace the internal implementation of the property without affecting the outside world. If your class keeps a hard count of elements in a tree (say) and exposes that via "public int count" then it must retain that count for all time. If it is exposed via

        public int Count
        {
        get { return count; }
        set {count = value; }
        }

        then it is at liberty to dispose of the "count" field and replace it with a new implementation at will. This will not affect classes relying on the "Count". Further, it allow you to error check the assignments:

        public int Count
        {
        get { return count; }
        set
        {
        if ((value < 100) && (count >= 0))
        {
        count = value;
        }
        else
        {
        throw new Exception(string.Format("Value out of range: {0}", value));
        }
        }
        }

        But I agree - read a book, because if you don't know this stuff, there are a lot of other complexities you need to be aware of before you go much further! Good luck.

        No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        1 Reply Last reply
        0
        • L lakhwinder ghuman

          what is the use of get set properties in C :) #

          U Offline
          U Offline
          User 4467139
          wrote on last edited by
          #4

          1 word: Encapsulation

          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