Methods are type members that execute a block of statements.
A method that performs some actions, but does not return a value indicates 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 indicates 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 thevoid
keywordGetOrigin
: This will return a string value, indicated by thestring
keyword
Inside the Person
class, statically import System.Console
, and then add the following code:
// methods public void WriteToConsole() { WriteLine($"{Name} was born on {DateOfBirth:dddd, d MMMM yyyy}"); } public string GetOrigin() { return $"{Name} was born on {HomePlanet}"; }
Inside the Main
method, add the following code:
p1.WriteToConsole(); WriteLine...