Writing and calling methods

Methods are type members that execute a block of statements.

A method that performs some actions but does not return a value shows this by showing that it returns the void type before the name of the method. A method that performs some actions and returns a value shows this by showing that it returns the type of that value before the name of the method.

For example, you will create two methods:

  • WriteToConsole: This will perform an action (writing a line), but it will return nothing from the method, indicated by the void keyword
  • GetOrigin: This will return a string value, indicated by the string keyword

Inside the Person class, statically import System.Console, and then add the following code:

 // methods public void WriteToConsole() ...

Get C# 7 and .NET Core: Modern Cross-Platform Development - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.