Method overload in C#

Asked

Viewed 89 times

1

I am learning about overload methods in C# and would like to create a simple program that reads a user’s age and name, but would like to create two methods with the same name (overload) where depending on whether the user has typed their age or not, will show age (if typed) or a message if nothing has been typed (I want to make the age be entered optionally by the user).

I created this little code, but it’s not working:

    class Program
    {
        static void Main(string[] args)
        {
            string numero; string letra,resultado;
            Console.WriteLine("Digite um numero: ");
            numero = Console.ReadLine();
            Console.WriteLine("Digite uma letra: (opcional)");
            letra = Console.ReadLine();
            resultado = MostraNaTela(numero, letra);
            Console.WriteLine("O resultado é");
        }
        public static void MostraNaTela(string num) 
        {

            Console.WriteLine("O numero é {0}",num, "Não foi preenchido letra");

        }
        public static void MostraNaTela(string num2, string letr2)
        {
            Console.WriteLine(num2);
            Console.WriteLine(letr2);
        }
     } 

1 answer

1


The problem isn’t really about overload, it’s about using the method. In fact I do not know if the method is wrong or use, because conceptually this method should not be used so for more than one reason.

I improved the code because it has several problems, some not serious and some is just because it’s an old way of programming or making it more readable. I didn’t solve all the problems, because it doesn’t make much sense to make a code like this.

The main problem is trying to save a result in a method that returns nothing. Just look at the error that the compiler gives that shows it. Nor would it make sense to return something in a method that the name indicates will only show something. So I had to change the order to try to make sense. But every idea seems wrong, rethink this and worry first about more basic questions of code for after mastering this part go to overload which is a more advanced concept and does not even seem to make sense in this case.

See all the details I’ve changed.

using static System.Console;

public class Program {
    public static void Main() {
        WriteLine("Digite um numero: ");
        var numero = ReadLine();
        WriteLine("Digite uma letra: (opcional)");
        var letra = ReadLine();
        WriteLine("O resultado é");
        MostraNaTela(numero, letra);
    }
    public static void MostraNaTela(string num) => WriteLine($"O numero é {num} Não foi preenchido letra");

    public static void MostraNaTela(string numero, string letra) {
        WriteLine(numero);
        WriteLine(letra);
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.