Why doesn’t Return work?

Asked

Viewed 289 times

3

I have a problem with return not work at all.

For now I’m just printing on the console, so I’m replacing the return for Console.WriteLine(), but didn’t want to always have to do it.

Here is an example that the return does not return anything in the console but with the Console.WriteLine() works:

Class1

    class Class1 {

    Dictionary<int, int> teste = new Dictionary<int, int>
    {
        { 1, 1 },
        { 2, 2 }
    };

    public int GetValue()
    {
        return teste[1];
    }
}

Launcher

    class Program {

    static void Main()
    {
        Class1 classe = new Class1();
        classe.GetValue();
        Console.ReadKey();
    }
}
  • I don’t know if I understand what you want and what the problem is, what is the desired result? You say Console.WriteLine(), but there’s no one in the code.

2 answers

3


A method can either return values or not. Methods that return values can be called functions. And we have methods that return nothing (void).

Let’s look at the following function, Multiplicar(int x, int y).

public int Multiplicar(int x, int y)
{
    return x * y;
}

What we can see is that this method is public, has a return of the type int and receives two parameters, x and y, both of the type int. What this method does is multiply the received parameters and return to the caller.

Let’s look at that context:

// ...
int resultado = 0;
resultado = Multiplicar(10, 20);

I created a variable of the integer type, which receives the value processed by the method Multiplicar(10, 20). At the end, the result value will be 200.

Note that nothing was printed on the screen or anything, what happened was just what was asked to be done: multiply two numbers and return to who called it, which was the variable resultado.


You have a method, called GetValue(). In your case, GetValue() returns an integer (int) and does not receive parameters, fine.

Let’s look at what GetValue() ago:

Dictionary<int, int> teste = new Dictionary<int, int>
{
    { 1, 1 },
    { 2, 2 }
};

public int GetValue()
{
    return teste[1];
}

It returns the dictionary value in key 1, nothing else. It will return an integer to whoever called the function.

Analyzing "who called" GetValue()...

static void Main()
{
    Class1 classe = new Class1();
    classe.GetValue();
    Console.ReadKey();
}

In this case, the value of GetValue() is not being stored or used for anything. This value is returned but not used.

As in your case, you want to print on the screen, in console applications (which is your case), you can use the method WriteLine, class Console.

Console.Writeline(int): Saves the text representation (character string) of the specified integer, followed by the current line terminator for the standard output stream.

Swap in kids, save the value of the integer in text form (type String) to the standard output stream, which is the Console.

static void Main()
{
    Class1 classe = new Class1();
    Console.WriteLine(classe.GetValue());
    Console.ReadKey();
}

In the case, classe.GetValue() will be evaluated and printed on the screen through the Console.WriteLine.

Resuming, Console.WriteLine is one of those methods that do not have a return, void.

Must-read: instruction return.

  • 2

    I am speaking on this subject: https://answall.com/a/122434/101

2

The return does not print anything on the screen, just returns, the correct is to do this:

class Program {

    static void Main()
    {
        Class1 classe = new Class1();
        Console.WriteLine(classe.GetValue());
        Console.ReadKey();
    }
}
  • I saw in some tutorials the use of Return. For example, to demonstrate the area of a rectangle, it has an Arearearetanuglo() method and inside it has a Return (result). The value of Return goes nowhere?

  • @Nash goes to the location the function was called, as if you had put this value in place of the function. Your question is a little confusing, if improve what you are wondering can get better answers. In your code goes to rent none. In Roberto’s code the result is used as an argument Console.WriteLine() and prints its value (roughly).

Browser other questions tagged

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