Writing and calling methods
Methods are members of a type that execute a block of statements.
Returning values from methods
Methods can return a single value or return nothing.
- A method that performs some actions but does not return a value indicates this with the
void
type before the name of the method. - A method that performs some actions and returns a value indicates this with the type of the return value before the name of the method.
For example, you will create two methods:
WriteToConsole
: This will perform an action (writing some text to the console), but it will return nothing from the method, indicated by thevoid
keyword.GetOrigin
: This will return a string value, indicated by thestring
keyword.
Let's write the code.
- Inside the
Person
class, statically importSystem.Console
. - Add statements to define the two methods, as shown in the following code:
// methods public void WriteToConsole() { WriteLine($"{Name...