Why does not return work? [closed]

3

I have a problem with return not working at all.

For now I'm just printing to the console, so I'm replacing return with Console.WriteLine() , but I did not always want to have to do that.

Here is an example that return does not return anything in the console but with Console.WriteLine() it 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();
    }
}
    
asked by anonymous 01.08.2017 / 00:54

2 answers

3

A method may or may not return values. Methods that return values can be called functions. And we have methods that do not return anything ( 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 notice is that this method is public , has a return of type int and receives two parameters, x and y , both of type int . What this method does is multiply the received parameters and return to the caller.

Let's look at this context:

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

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

See 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 resultado variable.

You have a method , called% with%. In your case, GetValue() returns an integer ( GetValue() ) and does not receive parameters, that's fine.

Let's look at what int does:

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

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

It returns the value of the dictionary in key 1, nothing else. It will return an integer for the caller.

Analyzing "who called" GetValue() ...

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

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

As in your case, you want to print on-screen, in console applications (which is your case), you can use the GetValue() ", of class WriteLine .

  

Console.WriteLine (int) : Writes the text representation (string) of the specified integer, followed by the current line terminator to the standard output stream.

Changing in Kids, writes the integer value in text form (type Console ) to the default output stream, which is the Console.

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

In this case, String will be evaluated and printed on the screen by classe.GetValue() .

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

Required reading: statement void .

    
01.08.2017 / 01:37
2

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

class Program {

    static void Main()
    {
        Class1 classe = new Class1();
        Console.WriteLine(classe.GetValue());
        Console.ReadKey();
    }
}
    
01.08.2017 / 00:58