tags:

views:

110

answers:

7

If there is the following code in a class, are get and set methods associated to the variable? How can I access get and set with an instance of the class?

public string Something { get; set; }
+5  A: 

This is essentially a compiler trick. When you compile the code the compiler will generate a hidden field and the necessary code to return and set the field in the get and set.

You would access this property just like you would access any other property. MyClass.Something = "bla".

Coding Gorilla
+1  A: 

This is an auto-property, which creates a backing field in the compiler, which you don't need to write code for.

get:

var str = instance.Something;

set:

instance.Something = "new value";
Jackson Pope
A: 

A backing variable complete with getter and setter methods(*) are created for you by the compiler, but you would not see them in your standard code. You would simply access the property directly.

myClass.Something = "blah"; // uses set
string myValue = myClass.Something; // uses get;

*These methods are created for properties rather they are auto-implemented or not. The compiler-generated backing variable is added to the mix in the case of an auto-implemented property.

Anthony Pegram
can't you use this code without specifying {get;set;}? I'm way cornfused
Soo
@Soo: Yes, that would definining a public field such as `public string Something;`. The difference is that with properties, you *could* (explicitly) have backing variables, you *could* have validation in a setter, you *could* have differing access modifiers for the get and set (so maybe external code could get the value but only the class itself could set it), or combinations of the above plus other things I did not mention. The preferred approach is to expose data via properties and not through fields.
Anthony Pegram
A: 

This is like the code below, but many fewer keystrokes :-)

public string Something {
    get() {
        return _Something;
    }
    set(string value) {
        _Something = value;
    }
}
Rice Flour Cookies
What language is that?
Richard Hein
I dunno, but it looks pretty darn awesome if you ask me lol
lucifer
don't forget to declare your _Something variable ;-)
Spilarix
+3  A: 

This syntax comes with .Net Framework 3.5 (automatic-property)

It's like :

private string something;
public string Something
{
     get { return something; }
     set { something = value; }
}

To access to this variable (supposed to be in a MyClass class) :

// GET
    MyClass myObj = new MyClass();
    string test = myObj.Something;
// SET
    myObj.Something = "blabla";
Spilarix
+1  A: 
  1. It's a new feature of compiler
  2. It's called Automatic Properties
  3. You don't need to define a backing store for automatic properties, the compiler does that job for you.
  4. You can not inject custom code in automatic properties, you need to revert back to normal 1.1 style of declaring properties.
  5. You can access automatic properties as you access normal properties
saurabh
A: 

The compiler turns this:

public string Something { get; set; } 

Into something like this (in IL, converted to C# for your convenience):

string _something;
public string get_Something() { return _something; }
public void set_Something(string value) { _something = value; } 

Also, the compiler turns these lines:

Something = "test";
var result = Something;

Into this:

set_Something("test");
var result = get_Something();

So you see, it's all method calls underneath (just like in Java), but it's really sweet to have the property syntax in C#. But if you try to call these methods directly, you get an error.

Jordão