Question

This might be a trivial question but I got really confused on this. I have a property with some logic in it.

private SomeObject _someProperty;
public SomeObject SomeProperty
{
    get
    {
        Some checking here,

        return _someProperty;
    }
    set
    {
        _someProperty = value;
    }
}

Now what will happen when I am going to assign something to this property.

SomeProperty = new SomeClass();

What I was thinking here that get will be called here. It words it can be said like get SomeProperty and set that property. But what I have observed is that get is not called. Only setter is called (correct me if I am wrong here). I want to know if get is not called here what is its reason.

Was it helpful?

Solution 3

No, a property's getter is not called when setting the property, as can be easily demonstrated :

static class Program
{
    static void Main(string[] args)
    {
        Foo foo = new Foo();
        foo.Number = 7;
    }
}

public class Foo
{
    private int number;
    public int Number
    {
        get
        {
            Console.WriteLine("In getter");
            return this.number;
        }

        set
        {
            Console.WriteLine("In setter");
            this.number = value;
        }
    }
}

Output:

In setter

OTHER TIPS

In simpler way to think about it.

GET: When something somewhere wants to GET value from here.

SET: When something somewhere wants to SET value here.

So, getters and setters answer to question from outside perspective. When you want to write value, SET is called. When you want to know current value, GET is called.

Properties are really just syntactic sugar for get/set methods. As the C# Programming Guide says:

A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily and still helps promote the safety and flexibility of methods.

So your example really translates to something like:

private SomeObject _someProperty;
public SomeObject get_SomeProperty()
{
    // Some checking here,

    return _someProperty;
}

public void set_SomeProperty(SomeObject value)
{
    _someProperty = value;
}

With the assignment becoming

set_SomeProperty(new SomeClass());

When thought of this way, it's clear that the getter is not called when you assign the property.

No, get is called when you are reading the value of the property, set is called when you are assigning a value to the property.

What I was thinking here that get will be called here

Why? You are clear - you SET. And SET does not do anything in GET. Ergo, get is never called.

This IS the correct behaviour of mutator methods, you dont Read the Value from accessor

You don't need value of SomeProperty in this expression. If you wrote something like SomeProperty = SomeProperty + 1; then you will need a value of SomeProperty, and get will be called.

Actually

A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they are public data members, but they are actually special methods called accessors.

as it is said in msdn. So consider it as a wrapper for something like this:

private SomeObject _someProperty;

public SomeObject getSomeProperty()
{
    //Some checking here,

    return _someProperty;
}
public void setSomeProperty(SomeObject value)
{
    _someProperty = value;
}

It should be clear now unless you have the same question about setting the fields.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top