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 and set properties

Get and set properties

Scheduled Pinned Locked Moved C#
question
6 Posts 6 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.
  • P Offline
    P Offline
    prabhu dot net
    wrote on last edited by
    #1

    hai,what is the main difference between "methods" and "get" "set" properties? In properties we can assign and return values , but the same thing is possible in methods also and even calling the properties and methods is also similar.my question is why we go for properties?? thank u:rolleyes:

    dot net !!! definetely a revolution

    L U G 3 Replies Last reply
    0
    • P prabhu dot net

      hai,what is the main difference between "methods" and "get" "set" properties? In properties we can assign and return values , but the same thing is possible in methods also and even calling the properties and methods is also similar.my question is why we go for properties?? thank u:rolleyes:

      dot net !!! definetely a revolution

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, you dont need properties, but they make things easier and more readable. which statement do you prefer:

      myForm.Width+=100;

      myForm.SetWidth(myForm.GetWidth()+100);

      :)

      Luc Pattyn


      try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


      1 Reply Last reply
      0
      • P prabhu dot net

        hai,what is the main difference between "methods" and "get" "set" properties? In properties we can assign and return values , but the same thing is possible in methods also and even calling the properties and methods is also similar.my question is why we go for properties?? thank u:rolleyes:

        dot net !!! definetely a revolution

        U Offline
        U Offline
        Urs Enzler
        wrote on last edited by
        #3

        - Some features of C# rely on properties: i.e. design support and heavy use of attributes - Properties are inlined (if I'm not mistaken) - property link a set and get "method" together. - properties are used i.e. in xml serialization (properties are serialized by default, fields not (and methods of course, too)) - I think it makes code more readable then properties stand for the state of an object where as methods stand for actions on this state.

        -^-^-^-^-^- no risk no funk

        L M 2 Replies Last reply
        0
        • P prabhu dot net

          hai,what is the main difference between "methods" and "get" "set" properties? In properties we can assign and return values , but the same thing is possible in methods also and even calling the properties and methods is also similar.my question is why we go for properties?? thank u:rolleyes:

          dot net !!! definetely a revolution

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          Properties and methods are semantically different. The normal behaviour of a property is that it represents a single value in the object. Getting the value should not change the state of the object. Setting the value should not change the state of the object in any other way than is reasonable to expect for the change of that property. A method on the other hand may take any form, contain a simple or complex process, and change the state of the object in any way.

          --- single minded; short sighted; long gone;

          1 Reply Last reply
          0
          • U Urs Enzler

            - Some features of C# rely on properties: i.e. design support and heavy use of attributes - Properties are inlined (if I'm not mistaken) - property link a set and get "method" together. - properties are used i.e. in xml serialization (properties are serialized by default, fields not (and methods of course, too)) - I think it makes code more readable then properties stand for the state of an object where as methods stand for actions on this state.

            -^-^-^-^-^- no risk no funk

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Urs Enzler wrote:

            - Properties are inlined (if I'm not mistaken)

            The IL generates get_ and set_ methods regards

            1 Reply Last reply
            0
            • U Urs Enzler

              - Some features of C# rely on properties: i.e. design support and heavy use of attributes - Properties are inlined (if I'm not mistaken) - property link a set and get "method" together. - properties are used i.e. in xml serialization (properties are serialized by default, fields not (and methods of course, too)) - I think it makes code more readable then properties stand for the state of an object where as methods stand for actions on this state.

              -^-^-^-^-^- no risk no funk

              M Offline
              M Offline
              Mike Dimmick
              wrote on last edited by
              #6

              Urs Enzler wrote:

              Properties are inlined (if I'm not mistaken)

              The JIT may choose to inline any method that's short enough. If a property setter or getter method is particularly long, it probably won't be inlined - the fact that it's a property is irrelevant. The CLR doesn't really 'understand' properties or events - the compiler is responsible for compiling a call to the correct underlying functions. In C# they're always called set_Property, get_Property and add_Event, remove_Event respectively (for a property called Property and an event named Event). The metadata links them into a single property or event. An standard event written in C# also has a field of the appropriate delegate type which is invisible to the user. It's possible to write an event which has custom add and remove methods; Windows Forms does this, making use of a list so that for example Control (which has 69 events in .NET 2.0) does not need to have 69 delegate fields unnecessarily bloating the memory footprint of each object. The feature is so useful that the implementation mostly lives in System.ComponentModel.Component's Events property.

              Stability. What an interesting concept. -- Chris Maunder

              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