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
.