hey guys,
I`m a beginner in c# and I have a problem in using set/get in classes.
actually I didn`t get why do we use them,can anyone please introduce them briefly for me?

You don't need to use set or get. You could write functions named setFoo or getFoo instead of using a property Foo:

class Point {
    double x, y;
    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }
    public double GetX() { return x; }
    public void SetX(double x) { this.x = x; }
    public double GetY() { return y; }
    public void SetY(double y) { this.y = y; }
}

But that's a real pain -- you'd rather write pt.Y = 3; and be able to write things like pt.Y += 5; , instead of pt.SetY(pt.GetY() + 5); .

So instead C# has properties:

class Point {
    double x, y;
    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }
    public double X {
        get { return x; }
        set { x = value; }
    }
    public double Y {
        get { return y; }
        set { y = value; }
    }
}

Inside the setter, the keyword 'value' is the variable containing the value that is getting assigned to the property Y.

The pattern of having properties directly backed by fields is so common that in C# 3, shortcut syntax was added.

class Point {
    public Point(double x, double y) {
        X = x;
        Y = y;
    }
    public double X { get; set; }
    public double Y { get; set; }
}

There are a few reasons to use properties, instead of public fields. One is that properties can be virtual. Another is that you can make the setters for a property private. Another is that properties have a 'special' meaning to things that inspect classes at runtime. There are frameworks for conveniently talking to databases and for reading and writing objects to and from XML and all sorts of other things -- and they automatically look at the object's properties (and not private fields or other things) to see how to do their job.

A reason to use get/set is so that your class contains control over the input

For example

Exam results shoul dbe between 0-100

No exact type exists to hold only 0-100, so, if the class has a set which validates the data, it cant ever have an issue where the data has been rigged

commented: yes +9

Thanks!It really helped!

In the above class:

class Point {
    public Point(double x, double y) {
        X = x;
        Y = y;
    }
    public double X { get; set; }
    public double Y { get; set; }
}

the copiler will throw an error as no implementation is provided to set and get in the:
public double X { get; set; }
public double Y { get; set; }
lines.

Why is that???
Thank you

Hi Serban, welcome here.
Did you read the excellent explanation of Rashakil Fol to it's full extend?
The handy syntax public double X { get; set; } can only be used if you have version 3.x of C#. My guess is you use a lower version. If that is not the case then post your question in a new thread.

EDIT: BTW the implementation IS provided if you use this syntax. The compiler does all the dirty work for you. One of the reasons why I love C#. In a sense I'm a lazy person....

Thank you DDanbe ...

That means that any methods can be written like that?
Eg... constructors:

public ClassConstructor(); instead of
public ClassConstructor(){}
OR
are these the same
public ClassConstructor(int param);
public ClassConstructor(int param)
{
_localClassInteger = param;
}

Sorry I dont have 3.0 on my machine to test these ...

Thank you again ....
Thread: How to use set/get ??
Forum: C#

Please don't post NEW questions on an OLD thread!
This can become very confusing to other users.
So, I won't answer your questions here.
Post a new thread with your question instead, you can always refer to old threads by using their http addresses or give a full explanation.

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.