locked
Set property question RRS feed

  • Question

  • User-257409430 posted

    Hi when you have a private property:

    private string _rule;

    and then you have a getter and setter:

    public string Rule

    {

       get { return _rule; }

       set { _rule = value; }

    }

    what is value referring to and where does it get its value?

    Also, is this the same as using the new short hand public int MyProperty { get; set; }

    Thanks.

    Wednesday, March 25, 2015 12:53 PM

Answers

  • User281315223 posted

    damon2012

    private string _rule;

    and then you have a getter and setter:

    public string Rule

    {

       get { return _rule; }

       set { _rule = value; }

    }

    You can think of the private _rule value as a temporary variable. When a getter request is made to the public Rule property, it will execute the following :

    get { return _rule; }

    As you can see it returns your private _rule value. Likewise, when performing a setting operation for your public property, it will handle setting the private value :

    set { _rule = value; }

    The value property is going to refer to the value that it is being set to as seen in the example below :

    // Set the Rule property
    YourClass.Rule = "Example";

    In this case, value would be "Example" and your setter would essentially look like this :

    set { _rule = "Example"; }

    So any calls after that from the getter would return "Example" as that is the value that is currently being stored within _rule. 

    Also, is this the same as using the new short hand public int MyProperty { get; set; }

    Yes, this is what the default getter/setter resolves to essentially.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 25, 2015 1:04 PM
  • User-821857111 posted

    The value keyword is kind of like an input parameter in a method. It represents the value provided by external code to the public property which is then assigned to the private field.

    More info: https://msdn.microsoft.com/en-us/library/a1khb4f8.aspx

    Also, is this the same as using the new short hand public int MyProperty { get; set; }

    Yes. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 25, 2015 1:09 PM

All replies

  • User281315223 posted

    damon2012

    private string _rule;

    and then you have a getter and setter:

    public string Rule

    {

       get { return _rule; }

       set { _rule = value; }

    }

    You can think of the private _rule value as a temporary variable. When a getter request is made to the public Rule property, it will execute the following :

    get { return _rule; }

    As you can see it returns your private _rule value. Likewise, when performing a setting operation for your public property, it will handle setting the private value :

    set { _rule = value; }

    The value property is going to refer to the value that it is being set to as seen in the example below :

    // Set the Rule property
    YourClass.Rule = "Example";

    In this case, value would be "Example" and your setter would essentially look like this :

    set { _rule = "Example"; }

    So any calls after that from the getter would return "Example" as that is the value that is currently being stored within _rule. 

    Also, is this the same as using the new short hand public int MyProperty { get; set; }

    Yes, this is what the default getter/setter resolves to essentially.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 25, 2015 1:04 PM
  • User-821857111 posted

    The value keyword is kind of like an input parameter in a method. It represents the value provided by external code to the public property which is then assigned to the private field.

    More info: https://msdn.microsoft.com/en-us/library/a1khb4f8.aspx

    Also, is this the same as using the new short hand public int MyProperty { get; set; }

    Yes. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 25, 2015 1:09 PM