The C# is statically typed at compile time, after a variable is declared, it cannot be declared again or assigned to a value of another type, unless that type can be converted implicitly into the type of the variable.
The signature of the method Verificar() specifies that the return type is bool. On the line...
return numero;
...is returned numero who’s kind int where the error occurs:
error CS0029: Cannot implicitly Convert type 'int' to 'bool'
You can identify two errors in your code:
- The attempt to return an integer in place of a logical value.
- His method
Verificar() does not cover all the possibilities of
numerical entries. If numero is equal to zero the method
can classify because the checks are numero > 0 and numero < 0 and both do not include the case numero == 0.
Looking at the documentation of comparison operators C# it appears that relational operators ==, <, >, <= and >= compare their operands and return a bool.
Aware of this simply simplify the method Verificar() using a comparison that covers the entire numerical spectrum supported by the type int.
using static System.Console;
namespace F1
{
class Program
{
static int Main(string[] args)
{
int numero;
Write("Digite um número: ");
numero = int.Parse(ReadLine());
WriteLine($"{Verificar(numero)}");
ReadKey();
return 0;
}
public static bool Verificar(int numero)
{
// Se numero for maior ou igual a zero retorna true caso contrário retorna false.
return numero >= 0;
}
}
}
Due to the simplicity of the operation performed within the 'Check()' method, it is possible to delete this method and transfer this comparison numero >= 0 inwarddo corpo do método Main().
using static System.Console;
namespace F1
{
class Program
{
static int Main(string[] args)
{
int numero;
Write("Digite um número: ");
numero = int.Parse(ReadLine());
WriteLine($"{numero >= 0}");
ReadKey();
return 0;
}
}
}
I had not seen that I already had an answer. They say the same thing. I ask, the correct thing is to remove my answer?
– Augusto Vasques
There’s no reason, yours gives a different explanation.
– Maniero