none
Backing filed in Auto-Implemented Properties RRS feed

Answers

  • Prior to the auto-implemented properites, you created properties like this:
     
    // backing field
    private string m_MyName;
    // property
    public string MyName
    {
        get { return this.m_MyName; }
        set { this.m_MyName = value; }
    }


    With the auto-implemented properties, the compiler creates the backing field for you (m_MyName in my example above), and manages it behind the scenes (you don't need to know anything at all about this backing field), so that all you need to write is this:
     
    public string MyName { get; set; }




    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    • Marked as answer by Arash_89 Tuesday, October 15, 2019 1:25 AM
    Tuesday, October 15, 2019 1:09 AM
    Moderator
  • Hello,

    If you want to see the backing fields.

    Test class

    public class Person
    {
        public int Identifier { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

    Code

    List<string> fields = typeof(Person)
    	.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
    	.Select(field => field.Name).ToList();
    
    foreach (var nameField in fields)
    {
    	Console.WriteLine(nameField);
    }

    Output

    <Identifier>k__BackingField
    <FirstName>k__BackingField
    <LastName>k__BackingField
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Arash_89 Tuesday, October 15, 2019 2:01 AM
    Tuesday, October 15, 2019 1:28 AM
    Moderator

All replies

  • Prior to the auto-implemented properites, you created properties like this:
     
    // backing field
    private string m_MyName;
    // property
    public string MyName
    {
        get { return this.m_MyName; }
        set { this.m_MyName = value; }
    }


    With the auto-implemented properties, the compiler creates the backing field for you (m_MyName in my example above), and manages it behind the scenes (you don't need to know anything at all about this backing field), so that all you need to write is this:
     
    public string MyName { get; set; }




    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    • Marked as answer by Arash_89 Tuesday, October 15, 2019 1:25 AM
    Tuesday, October 15, 2019 1:09 AM
    Moderator
  • Hello,

    If you want to see the backing fields.

    Test class

    public class Person
    {
        public int Identifier { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

    Code

    List<string> fields = typeof(Person)
    	.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
    	.Select(field => field.Name).ToList();
    
    foreach (var nameField in fields)
    {
    	Console.WriteLine(nameField);
    }

    Output

    <Identifier>k__BackingField
    <FirstName>k__BackingField
    <LastName>k__BackingField
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Arash_89 Tuesday, October 15, 2019 2:01 AM
    Tuesday, October 15, 2019 1:28 AM
    Moderator
  • So what is the differences between these?

    public string MyName { get; set; }

    And

    public string MyName;

    • Edited by Arash_89 Tuesday, October 15, 2019 2:02 AM
    Tuesday, October 15, 2019 2:02 AM
  • So what is the differences between these?

    public string MyName { get; set; }

    And

    public string MyName;

    The first is a property while the second is a field.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, October 15, 2019 2:17 AM
    Moderator
  • Greetings Arash.

    From a practical point of view there is (in my opinion) no real difference between those two.

    However, object-oriented Nazis insist that no object should ever be able to directly access another object's data. Instead, one object should only be able to 'ask' another object to provide its data, or 'ask' another object to modify its data. Using the get and set as per your first example obeys this principle (in a very strict sense) while the second example breaks it.

    Tuesday, October 15, 2019 2:31 AM
  • So what is the differences between these?

    public string MyName { get; set; }

    And

    public string MyName;

    The first example using a public property embodies encapsulation that is one of the pillars of object oriented programming, and the second one does not.

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

    <copied>

    Encapsulation, in object-oriented programming, is the bundling of data with the methods that operate on that data, or the restricting of direct access to some of an object's components. 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>

    Using a DTO is a classic example of using information hiding.

    https://en.wikipedia.org/wiki/Data_transfer_object

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/data/using-web-api-with-entity-framework/part-5

    https://www.codeproject.com/Articles/1050468/Data-Transfer-Object-Design-Pattern-in-Csharp

     https://stackoverflow.com/questions/1121848/private-setter-properties-in-c-sharp-3-0-object-initialization

    • Edited by DA924x Tuesday, October 15, 2019 3:32 AM
    • Marked as answer by Arash_89 Tuesday, October 15, 2019 6:34 PM
    • Unmarked as answer by Arash_89 Tuesday, October 15, 2019 6:35 PM
    Tuesday, October 15, 2019 3:27 AM
  • DA924x,

    Sieg! Heil!

    Sorry, I couldn't resist. I do understand what you are saying, but I still think it's very picky.

    Tuesday, October 15, 2019 3:33 AM
  • DA924x,

    Sieg! Heil!

    Sorry, I couldn't resist. I do understand what you are saying, but I still think it's very picky.

    Well find out the difference when one tries to send special characters in the parms of  a URL causing a malformed URL, becuase one doesn't understand encapsulation "information hiding",  and one should send the data in a DTO or other such custom object  preventing the malformed URL.   

    Have a rookie sit there for a few hours not knowing what the problem is and how to correct it. :)


     
    • Edited by DA924x Tuesday, October 15, 2019 4:21 AM
    Tuesday, October 15, 2019 4:20 AM