C
C
ChechenProgrammer2019-04-14 18:37:23
C++ / C#
ChechenProgrammer, 2019-04-14 18:37:23

Fields and Properties / get / set - what is it? How to understand? Is it possible to explain in a simpler/humane way?

https://www.youtube.com/watch?v=CSbix3v6AUY&list=P... I'm taking a course and I'm completely stuck here. Some kind of Get Set, accessor, etc. I don’t understand what and why and why? Can you explain a little more simply with a simple example? After all, someone who understands can easily give a simple example?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2019-04-15
@WNeZRoS

As I understand it, the fields and methods are already clear to you.
For example, you have a code that displays the area of ​​a square in the console:

void Calculate()
{
    var quad = new Quad();
    quad.Size = 10;
    Console.WriteLine(quad.Area);
}

And the class of the square itself is defined as follows:
class Quad
{
    public float Size;
    public float Area;
}

As you can see, no one calculates the area (Area) and the output will always be 0, i.e. the code doesn't work.
To fix it, someone needs this area.
(1) The most stupid option, calculate it in Calculate
void Calculate()
{
    var quad = new Quad();
    quad.Size = 10;
    quad.Area = quad.Size * quad.Size;
    Console.WriteLine(quad.Area);
}

В этом варианте нам надо править код, который использует этот класс Quad. Если этого кода много, то это куча лишней работы. И в целом, класс Quad теряет смысл, все его переменные можно в методах объявить, это будет намного оптимальнее.
(2) A slightly better option is to make the area calculation method in Quad
class Quad
{
    public float Size;
    public float Area;

    public void UpdateArea()
    {
        Area = Size * Size;
    }
}

Нам всё-равно нужно менять код использующий этот класс Quad, и добавлять вызов UpdateArea после изменения размера. Тоже не очень-то удобно.
If Area should always be considered, then it can be better to make a method?
class Quad
{
    public float Size;

    public float GetArea()
    {
        return Size * Size;
    }
}

Now we don't need to call anything extra, but we will have to go through all the old uses of the Area field and replace them with a call to the new GetArea method.
But you can make the GetArea method look like the Area field and not have to change the code that uses it. That's what Properties are for.
class Quad
{
    public float Size;

    public float Area
    {
        get { return Size * Size; }
    }
}

When using the Quad class, Area now looks like a field, but it cannot be changed because it doesn't have a set part.
You can go the other way, and recalculate the Area fields when resizing:
class Quad
{
    private float size;

    public float Size
    {
        get { return size; }
        set
        {
            size = value;
            Area = size * size;
        }
    }

    public float Area;
}

In this version, we have Area considered when changing the Size, and not when accessing the Area. Which option is better depends on the task and its requirements.
To summarize, Properties are methods similar in use to fields.
accessor = accessor = accessor - generic name for get and set

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question