when I call the Check
method, I put "5" in parenthesis. I do it because it requires an integer to call the method. Does this have anything to do with user input, or is it a necessary thing to be able to call method?
How can I not put something in parenthesis but still be able to call the Check
method?
static void Main(string[] args)
{
Check(5);
Console.Read();
}
public static void Check(int myInt)
{
Console.WriteLine("Enter a number to checkc if it is odd or even number: ");
string userInput = Console.ReadLine();
int userInputNum;
int parsedValue;
if (Int32.TryParse(userInput, out userInputNum))
{
parsedValue = userInputNum;
}
else
{
parsedValue = 0;
Console.WriteLine("You did not enter a number or anything.");
}
if ((parsedValue % 2) == 0)
{
Console.WriteLine("It is a even number.");
}
else
{
Console.WriteLine("It is an odd number.");
}
}
>Solution :
The declaration of public static void Check(int myInt)
method states it accepts a mandatory int
parameter and calls it myInt
.
Because of this, wherever you want to call the Check
method, you have to provide a value for this parameter: Check(5)
, Check(100)
, Check(Random.Shared.NextInt())
and so on.
It is illegal to call the method without a value for its parameter, i.e. Check()
.
Now, the method Check
accepts a mandatory parameter int myInt
, but it doesn’t actually use it anywhere. Since the parameter is useless, you may as well remove it from the method requirements.
public static void Check()
{
...
Once you remove the int myInt
parameter from the method signature, you no longer have to provide a value for that parameter when you call the method. You call parameterless methods with simply Check()
in your code.