+ 1
C# When are we pass arguments
Sometimes in some methods we are using passing arguments but sometimes we are not we are directly using : for using arguments Why is it
7 ответов
+ 1
Arguments make code reusable
That means less code to write, less code to maintain
By using arguments you only need to write one method instead of three
And you can pass arguments you did not think off in the first place.
Did this amswer your question?
https://code.sololearn.com/c1a21A205A1A/?ref=app
+ 1
sneeze yes why did not you use res or out at this code
+ 1
In this example, I did not need to change the value of name. So pass by value was enough.
Ref and Out do change the value of the argument in a method.
https://code.sololearn.com/c0mzKfHb0nuM/?ref=app
+ 1
sneeze //by value : The value of A can change within method, but the outside the method the value of A is not changed
+ 1
sneeze i didnt understand why
+ 1
thank you so much sneeze i understood
0
Because some arguments are input only.
public int Add(int x, int y)
{
return x+y;
}
In this method the value of x and y are important to get the return value.
But once this method has finished you do not need x and y anymore.
public void PrintX(int x)
{
Console.WriteLine(x);
}
The method returns nothing, the method need a input value x. But this value, is only used in the method. So it does not need to be changed and returned via a reference to the main method.
General rule, try to return as less as possible