Hey guys,

I don't really understand the usage of the get/Set/return keywords.

get-Read Only
set-Write Only(eh)
return-Returns value to calling method

Those are the definitions I have but can I have a small explanation on when to use these and how to use the get/set thank you very much.

get and set are used with properties. return is used with methods that want to pass a value back to the calling code.

public int Age {get; set;} // Automatic property, don't have to worry
                           // about where it stores the value

private int weight;

public int Weight {                      // Normal property, we have to write the code
    get {                                // and have a place to store it. This lets us
        return weight;                   // do things like add validation to make sure
    }                                    // the value is in range, etc.
    set {
        if (value > 0) weight = value;   // 'value' is the automatic name of the value
    }                                    // assigned.
} 

public int MultiplyByTwo(int x) {
    return x * 2;   // the return tells it what value we want to pass back to the
}                   // calling method. The type must match the type given before
                    // the method name, in this case, an int

public void UseStuff() {
    Weight = 43;                         // Assigning to the property automatically
                                         // calls the 'set' method of the property.
    int result = MultiplyByTwo(Weight);  // Here we are calling the method defined
}                                        // above and passing it the value of the 
                                         // Weight property (get is automatically
                                         // called here). The value returned is assigned
                                         // to the result variable.

I didnt understand them either, until i found this youtube video:
Click Here

Thank you guys for all your assistance!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.