none
Advantages of int x vs int X { get; set; } code? RRS feed

  • Question

  • Hello,

     I was playing around with Buffered Graphics class and wondered something...

    That something of course is the title of my post. Can anyone tell me if there

    is any advantages?

     

     Thanks

    Sunday, April 15, 2018 4:24 AM

Answers

  • https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/using-properties

    <copied>

    Properties combine aspects of both fields and methods.

    Properties have many uses: they can validate data before allowing a change; they can transparently expose data on a class where that data is actually retrieved from some other source, such as a database; they can take an action when data is changed, such as raising an event, or changing the value of other fields.

    Auto-implemented properties provide simplified syntax for simple property declarations.

    <end>

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties

    Properties provide encapsulation in an object, hiding. 

    https://en.wikipedia.org/wiki/Encapsulation_(computer_programming)

    <copied>

    Encapsulation is one of the fundamentals of OOP (object-oriented programming). It refers to the bundling of data with the methods that operate on that data.<sup class="reference" id="cite_ref-Rogers01_5-1" style="background-color:transparent;color:#222222;font-family:sans-serif;font-size:9.26px;font-style:normal;font-variant:normal;font-weight:400;letter-spacing:normal;line-height:9.26px;orphans:2;text-align:left;text-decoration:none;text-indent:0px;text-transform:none;-webkit-text-stroke-width:0px;white-space:nowrap;word-spacing:0px;">[5]</sup> Encapsulation is used to hide the values or state of a structured data object inside a class, preventing unauthorized parties' direct access to them. Publicly accessible methods are generally provided in the class (so-called getters and setters) to access the values, and other client classes call these methods to retrieve and modify the values within the object.

    <end>

    https://www.safaribooksonline.com/library/view/programming-c/0596001177/ch04s07.html

    <copied>

    Properties provide a simple interface to the client, appearing to be a member variable. They are implemented as methods, however, providing the data hiding required by good object-oriented design.

    <end>

    • Marked as answer by User3DX Sunday, April 15, 2018 5:39 PM
    Sunday, April 15, 2018 6:11 AM

All replies

  • Are you asking about a pubic variable/field vs. a pubic property? Or declaring a property as a auto-implementing property?

    As far as the auto-implementing:

    • You are creating a property instead of a field.
    • You can later change the property by writing code for the Get/Set without changing the interface. I.e. you won't break code that already uses the class or DLL.
    • Interfaces can only define a property. Not a field. A auto-implementing property can satisfy an interface.


    Mike Smith TechTrainingNotes.blogspot.com
    Books: SharePoint 2007 2010 Customization for the Site Owner, SharePoint 2010 Security for the Site Owner

    Sunday, April 15, 2018 5:03 AM
  • https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/using-properties

    <copied>

    Properties combine aspects of both fields and methods.

    Properties have many uses: they can validate data before allowing a change; they can transparently expose data on a class where that data is actually retrieved from some other source, such as a database; they can take an action when data is changed, such as raising an event, or changing the value of other fields.

    Auto-implemented properties provide simplified syntax for simple property declarations.

    <end>

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties

    Properties provide encapsulation in an object, hiding. 

    https://en.wikipedia.org/wiki/Encapsulation_(computer_programming)

    <copied>

    Encapsulation is one of the fundamentals of OOP (object-oriented programming). It refers to the bundling of data with the methods that operate on that data.<sup class="reference" id="cite_ref-Rogers01_5-1" style="background-color:transparent;color:#222222;font-family:sans-serif;font-size:9.26px;font-style:normal;font-variant:normal;font-weight:400;letter-spacing:normal;line-height:9.26px;orphans:2;text-align:left;text-decoration:none;text-indent:0px;text-transform:none;-webkit-text-stroke-width:0px;white-space:nowrap;word-spacing:0px;">[5]</sup> Encapsulation is used to hide the values or state of a structured data object inside a class, preventing unauthorized parties' direct access to them. Publicly accessible methods are generally provided in the class (so-called getters and setters) to access the values, and other client classes call these methods to retrieve and modify the values within the object.

    <end>

    https://www.safaribooksonline.com/library/view/programming-c/0596001177/ch04s07.html

    <copied>

    Properties provide a simple interface to the client, appearing to be a member variable. They are implemented as methods, however, providing the data hiding required by good object-oriented design.

    <end>

    • Marked as answer by User3DX Sunday, April 15, 2018 5:39 PM
    Sunday, April 15, 2018 6:11 AM
  • It is about OOP. Each field cannot be access directly but through getter and setter method only. It means you need create Get/Set method for each field. Property creates this method instead of you. You can see it in call stack, when you get break point into property. Called method is fieldname_get()/fieldname_set().

    But what is get/set method? Get method should return value of field and set method should assign value into field. It does not mean set method is mandatory. Set value into field could be private operation when you want field is read-only. 

    You can change access to property. If you need private/protected access to set "method" only you can write private/protected set, etc...
    • Edited by Petr B Sunday, April 15, 2018 6:41 AM
    Sunday, April 15, 2018 6:39 AM
  • Hello,

     I was playing around with Buffered Graphics class and wondered something...

    That something of course is the title of my post. Can anyone tell me if there

    is any advantages?

     

     Thanks

    In my opinion not much as long as you are creating small programs. Properties come around when you are using XMLDoc and want to use them in a designer or things like that.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/xmldoc/xml-documentation-comments

    Be aware that accessing by using a property is seen as cleaner coding. 

    As you ask this about speed, you would think without properties would everything go faster. However the Jit compiler makes that they are accessed both at the same speed. 

     

    Success
    Cor

    Sunday, April 15, 2018 11:25 AM