y2k
y2k

Reputation: 65996

Why do you use private variables with C# getter/setters?

I see this all the time:

    private int _myint;

    public int MyInt
    {
        get
        {
            return _myint;
        }
        set
        {
            _myint = value;
        }
    }

To me this seems identical to:

    public int MyInt{ get; set; }

So why does everyone do the former... WHY THE PRIVATE VAR AT ALL?!

Upvotes: 11

Views: 14683

Answers (9)

AGoodDisplayName
AGoodDisplayName

Reputation: 5653

An example to expand on what @BFree is saying:

private int _Whatever; 
public int Whatever
{
    get {return _Whatever;}
    set 
    {
        if(value != _Whatever)
        {  
          // It changed do something here...
          // maybe fire an event... whatever

        } 

        _Whatever = value;

    }
}

Upvotes: 6

bristows
bristows

Reputation: 746

If you look at the output produced by the compiler with a tool like reflector a private field has been added

Upvotes: 1

hunter
hunter

Reputation: 63512

I'd like to see

public int MyInt{ get; private set; }

more, ;)

but @BFree nailed it

Upvotes: 6

Mike Mooney
Mike Mooney

Reputation: 11989

That's the old way to do it. The way you prefer ("automatic properties") it a relatively new construct in the language. A few years ago, we always had to use private variables.

There may be other reasons to use private variables as well, though not in the simple example you provide. If, for example, you needed to intialize the property with a default value, you can't really do that cleanly with automatic properties; instead you need to initialize in the constructors.

Upvotes: 2

Mark Kinsella
Mark Kinsella

Reputation: 205

public int MyInt{ get; set; }

was a feature added in C# 3.0 called Automatic Properties. C# 2.0 does not support this and requires the private variable with the explicit getters and setters. Therefore, a lot of older code or backwards compatible code will use the explicit getters and setters.

Upvotes: 1

Jordan Parmer
Jordan Parmer

Reputation: 37164

Before C# 3, that was the only way to do it. Implicitly typed properties were not yet available. People still wanted to abstract away the private member. If developers are still doing this in C# 3, they either aren't aware of the new changes or need to provide custom get/set logic.

Upvotes: 2

No Refunds No Returns
No Refunds No Returns

Reputation: 8336

Do you like someone you don't know messing with your private parts? I hope not. This is a way to provide what is essentially a proxy for something you own but don't want to cede control over. If you decide you want to validate that int's are positive, you can start doing that if you code as shown.

C# makes this transparent now with automatic properties.

Upvotes: 2

John Saunders
John Saunders

Reputation: 161773

First of all, the syntax you used is new. It didn't exist in earlier versions of C#.

Second of all, you need the private variable if you're going to have a default value, or lazy-load the result.

Upvotes: 5

BFree
BFree

Reputation: 103742

First of all, this is new to C# 3.0, it wasn't around before that. Second, if you want to add any custom logic to your getter and setters, you have no choice. So yes, in your example where there's no custom logic, it's the same thing (in fact the compiler generates that for you behind the scenes) but if you want to raise an event for example, or anything like that, you have to be explicit about it.

Upvotes: 35

Related Questions