A
A
andoral2017-03-05 17:25:39
OOP
andoral, 2017-03-05 17:25:39

How is it customary for programmers: to use a property for a variable or to give a parameter in each method?

Two examples of solving one problem, which one is commonly used?:
1)

class A
{
    int x;
    public int X{ get { return x;} set { x = value; } }

    public void Method()
    {
         Console.WriteLine( x ) ;
    }
}

2)
class A
{
    public void Method( int x )
    {
         Console.WriteLine( x ) ;
    }
}

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Stanislav Makarov, 2017-03-05
@andoral

Two examples of solving one problem

These are not the same task, they are different tasks. In the first case, the result of the method call depends on the state of the object; in the second case, it depends on the parameters passed to it. In the first case, the class encapsulates the state; in the second case, you don't need the class.
These are so different examples that it is not even clear how to explain this difference to you.

A
Anton Papin, 2017-03-05
@i_light

It's a matter of concept. Class properties that belong semantically to its object - make them properties. Data not related to the object - pass as parameters.
For example, for the theoretical class "apple", the properties - "size", "weight", "color" - belong to the apple, we make them properties of the class. The theoretical "split" method in this class requires the "number of parts" parameter - obviously not a property of the apple.

A
Armenian Radio, 2017-03-05
@gbg

The first is called OOP, the second is structural. The choice depends on the development approach adopted

S
SpyDeX, 2017-03-07
@SpyDeX

Depends on the context, if this parameter is part of the information about the class, then sv-in,
if the f-ii parameter is an external factor, then as a method parameter.
For example, there is a hand, it has a load capacity of 10 kg, this is the power of the hand.
there is a task - to lift a bag of 8 kg - this is a parameter of the method.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question