Contrary to 3dd's opinion, you should not call Addition method in the ReadInput method, because the ReadInput method, as its name suggests, is only responsible for reading inputs, Also, the Addition method's only task is to perform addition and send the result to the caller so that the caller can use as it wants. and if you call Addition inside ReadInput, you have violated the SRP principle.,
You can optimize your code as follows. Consider the output for the ReadtInput method and the input and output for the Addition method.
you can use tuples or create a separate model, I will write both for you.
with Tuple :
public class HelloWorld
{
public (int number1, int number2) ReadInput()
{
Console.WriteLine("Insert a first number:");
var num1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Insert a second number");
var num2 = Convert.ToInt32(Console.ReadLine());
return (num1, num2);
}
public int Addition(int num1, int num2)
{
Console.WriteLine("Addition");
int sum = num1 + num2;
return sum;
}
}
How to use :
internal class Program
{
static void Main(string[] args)
{
HelloWorld helloWorld = new HelloWorld();
var result = helloWorld.ReadInput();
var sumResult = helloWorld.Addition(result.number1, result.number2);
Console.WriteLine("The sum is " + sumResult);
}
}
If you don't want to use tuple, you create a model like below, the changes are as follows :
public class ReadInputModel
{
public int Number1 { get; set; }
public int Number2 { get; set; }
}
And the ReadInput method changes as follows
public ReadInputModel ReadInput()
{
Console.WriteLine("Insert a first number:");
var num1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Insert a second number");
var num2 = Convert.ToInt32(Console.ReadLine());
return new ReadInputModel
{
Number1 = num1,
Number2 = num2
};
}
Just pay attention to this point, I only optimized you Code, but still the principles of creating and designing methods are not well followed,for example, you should not do Console.WriteLine in the ReadInput method and its only task should be to read information ,and other things which I think cannot be explained here, it is better to study the principles of Solid well.
I recommend reading these articles:
Solid
SOLID Principles In C#
Csharp best practices dangers of violating solid principles in csharp