ferics2
ferics2

Reputation: 5432

When to use the Value keyword in C#

I am using a custom serializer in c# in order to serialize/deserialize an object with a dictionary. However during the deserialization the dictionary object was not being set correctly. See code:

public DictionarySerializer<String, Point> jointDictionary
    {
        get { return _jointDictionary; }
        set { _jointDictionary = jointDictionary; }
    }

The jointDictionary object coming back was empty however in the debugger thread I noticed a "value" object that had the contents of my dictionary. Changing my code to the following fixed my issue:

public DictionarySerializer<String, Point> jointDictionary
    {
        get { return _jointDictionary; }
        set { _jointDictionary = value; }
    }

I have read about the "value" keyword and understand that it is a reserved word in C# to specify the value that the client it trying to use to set the object. So my question is, why wouldn't the jointDictionary reference work as in my first attempt? And what is the correct usage for the value keyword?

Upvotes: 2

Views: 2206

Answers (6)

John Gardner
John Gardner

Reputation: 25146

if think you of the get and set methods as any other functions, value makes a little more sense.

get { return _jointDictionary; } 
set { _jointDictionary = value; } 

is basicly c# syntactic sugar for:

DictionarySerializer<String, Point> get()
{ 
    return _jointDictionary; 
} 

void set( DictionarySerializer<String, Point> value ) 
{ 
    _jointDictionary = value; 
} 

since the type of the return value in the getter, and the value arg in the setter are declared upfront in the declaration of the property, it is avoiding redundant information.

unless you need the private class variable (either for validation or notifications), there's an even simpler syntax of just:

DictionarySerializer<String, Point> JointDictionary { get; set; }

in this case, the complier is automatically generating the private member and the code for the getter and setter.

Upvotes: 0

Daniel Mošmondor
Daniel Mošmondor

Reputation: 19966

Your first attempt was wrong since referencing the property within itself would return the value of the property before the property was set.

So by:

set { _jointDictionary = jointDictionary; }

you actually did:

_jointDictionary = _jointDictionary;

because of:

get { return _jointDictionary; }

Upvotes: 0

porges
porges

Reputation: 30580

This code:

public DictionarySerializer<String, Point> jointDictionary
{
    get { return _jointDictionary; }
    set { _jointDictionary = jointDictionary; }
}

Does the following when set is called:

  1. jointDictionary is read via get
  2. get returns _jointDictionary
  3. _jointDictionary is set to what was returned

So the set method effectively does nothing, and _jointDictionary will remain with the value it started with (in this case, null).

Upvotes: 1

Mike Christensen
Mike Christensen

Reputation: 91666

The first implementation is recursive.

_jointDictionary is set to jointDictionary, which calls the getter which returns _jointDictionary which has not been set yet.

Thus, the result is blank.

It's as if you did:

_jointDictionary = _jointDictionary;

The value keyword allows you to access the value being passed into the setter, which is what you want here.

Upvotes: 1

Steffan Donal
Steffan Donal

Reputation: 2344

If it did work, it would be illogical, as you would be 'essentially' creating a loop situation, where you are attempting to assign a variable to the value of the variable that you are trying to assign.

In short, 'Aliens'.

Upvotes: 0

JaredPar
JaredPar

Reputation: 755141

The keyword value represents the value which is being passed to the property. You should always use it within the property setter.

In the case you used jointDictionary you were binding to the property getter. This makes

set { _jointDictionary = jointDictionary; }

Compiles to

set { _jointDictionary = this.jointDictionary; } 

Which since this accesses the property getter it really becomes

set { _jointDictionary = _jointDictionary; }

Upvotes: 10

Related Questions