Question Error CS0161

BrunoB

Well-known member
Joined
Nov 19, 2022
Messages
96
Programming Experience
Beginner
Hello, according to the official documentation I should remove the word return to solve the problem, but it is absurd, because if I want to show it, how will I remove the return? How do I solve it? Thank you.
1673636341340.png
 
C#:
namespace DESAFIOPOO1
{
class Program
{
static void Main(string[] args)
{

Telefono t1 = new Telefono();
t1.setModelo( "Motorola");
string mod = t1.getModelo();

Console.WriteLine("el modelo es" + " mod");
Console.ReadKey();


}
}
}
 
C#:
private string Modelo;
        //private string Marca;
        //private int NumeroTelefonico;
        //private int CodigoOperador;

        public string setModelo(string m)
        {
            Modelo = m;
            
        }

        public string getModelo()
        {

            return Modelo;
        }


    }
}
 
Your method signature on line 6 says you will return a string, so you need to return a string.
 
C#:
private string Modelo;
        //private string Marca;
        //private int NumeroTelefonico;
        //private int CodigoOperador;

        public string setModelo(string m)
        {
            Modelo = m;
          
        }

        public string getModelo()
        {

            return Modelo;
        }


    }
}

You code should simply be:

C#:
    public string Modelo {get; set;}

We do not write get/set methods in C#, we use properties. Why write 20 lines when you can write 1?

Please leave your Java at the door
 
You code should simply be:

C#:
    public string Modelo {get; set;}

We do not write get/set methods in C#, we use properties. Why write 20 lines when you can write 1?

Please leave your Java at the door
tHa ha ha you're right, but the instruction for the exercise says that I should do it like this.
 
Your method signature on line 6 says you will return a string, so you need to return a string.
But why then does the official c# error documentation say that I should comment out the return line? Cuadno for it to work just needs to be uncommented
 
It says uncomment, then both if and else will have a return, so all code paths will return a value.
 
But why then does the official c# error documentation say that I should comment out the return line? Cuadno for it to work just needs to be uncommented
The official documentation is showing how to cause the error, and how to modify the problem code to fix the error

The code says "int Main" which means "I faithfully promise this method will always return an integer". C# checks you are keeping your promise; it looks at the code and tries to find a way out of the method that does not return an integer. It finds one, because in the case that the else is performed, no return keyword is encountered. C# is not an AI and it does not look at the condition of the if and think "oh, the i variable is 5 and the if clause demands it be less than 10, and nothing changes the i variable so the else will never be used, so the code is ok" - it just looks at the code and sees "there is an if that does have a return and an else that does not, so the promise is broken and that method might not return what it promised. Throw an error"
 
Last edited:
It says uncomment, then both if and else will have a return, so all code paths will return a value.
If i is greater than 10, it enters the else and if the return is commented, the return of the else will not be executed, only the one of the if has the possibility of being executed, because it is uncommented
1673694889993.png
 
The official documentation is showing how to cause the error, and how to modify the problem code to fix the error

The code says "int Main" which means "I faithfully promise this method will always return an integer". C# checks you are keeping your promise; it looks at the code and tries to find a way out of the method that does not return an integer. It finds one, because in the case that the else is performed, no return keyword is encountered. C# is not an AI and it does not look at the condition of the if and think "oh, the i variable is 5 and the if clause demands it be less than 10, and nothing changes the i variable so the else will never be used, so the code is ok" - it just looks at the code and sees "there is an if that does have a return and an else that does not, so the promise is broken and that method might not return what it promised. Throw an error"
shouldn't it be the other way around? If the code promises to return an integer, shouldn't C# and your compiler just check that it returns an integer?
 
It does check. Recall that it gave you an error when you wrote this:
C#:
public string SetModelo(string m)
{
    _modelo = m;
}

Now if what you are saying that it shouldn't give you an error in this case:
C#:
public string SetModelo(string m)
{
    int i = 5
    if (i < 10)
    {
        _modelo = m;
        return _modelo;
    }
}
Unfortunately the C# compiler doesn't do an deep static analysis (yet). It doesn't know that i will always be less than 10.

Interestingly, though, the C# compiler is getting smarter. This now compiles without error in VS2022. (I vaguely recall trying out something like this in the early days of C# and it gave an error.)
C#:
public string SetModelo(string m)
{
    const int i = 5;
    if (i < 10)
    {
        _modelo = m;
        return _modelo;
    }
}

Compiler writers have to play a careful balancing game of static analysis and code optimizations vs. compile time performance. Looks like they have put in some static analysis of the case of using const values in the latest iterations of the compiler.
 
shouldn't it be the other way around? If the code promises to return an integer, shouldn't C# and your compiler just check that it returns an integer?
The error is that some paths through the code do not return a value. This is not allowed
 

Latest posts

Back
Top Bottom