Answer the question
In order to leave comments, you need to log in
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 ) ;
}
}
class A
{
public void Method( int x )
{
Console.WriteLine( x ) ;
}
}
Answer the question
In order to leave comments, you need to log in
Two examples of solving one problem
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.
The first is called OOP, the second is structural. The choice depends on the development approach adopted
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 questionAsk a Question
731 491 924 answers to any question