Reader small image

You're reading from  C# 11 and .NET 7 – Modern Cross-Platform Development Fundamentals - Seventh Edition

Product typeBook
Published inNov 2022
Reading LevelIntermediate
PublisherPackt
ISBN-139781803237800
Edition7th Edition
Languages
Right arrow
Author (1)
Mark J. Price
Mark J. Price
author image
Mark J. Price

Mark J. Price is a Microsoft Specialist: Programming in C# and Architecting Microsoft Azure Solutions, with over 20 years' experience. Since 1993, he has passed more than 80 Microsoft programming exams and specializes in preparing others to pass them. Between 2001 and 2003, Mark was employed to write official courseware for Microsoft in Redmond, USA. His team wrote the first training courses for C# while it was still an early alpha version. While with Microsoft, he taught "train-the-trainer" classes to get other MCTs up-to-speed on C# and .NET. Mark holds a Computer Science BSc. Hons. Degree.
Read more about Mark J. Price

Right arrow

Writing and calling methods

Methods are members of a type that execute a block of statements. They are functions that belong to a type.

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, in the next task, 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 the void keyword.
  • GetOrigin: This will return a text value, indicated by the string keyword.

Let’s write the code:

  1. In Person.cs, add statements to define the two methods that I described earlier, as shown in the following code: ...
lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
C# 11 and .NET 7 – Modern Cross-Platform Development Fundamentals - Seventh Edition
Published in: Nov 2022Publisher: PacktISBN-13: 9781803237800

Author (1)

author image
Mark J. Price

Mark J. Price is a Microsoft Specialist: Programming in C# and Architecting Microsoft Azure Solutions, with over 20 years' experience. Since 1993, he has passed more than 80 Microsoft programming exams and specializes in preparing others to pass them. Between 2001 and 2003, Mark was employed to write official courseware for Microsoft in Redmond, USA. His team wrote the first training courses for C# while it was still an early alpha version. While with Microsoft, he taught "train-the-trainer" classes to get other MCTs up-to-speed on C# and .NET. Mark holds a Computer Science BSc. Hons. Degree.
Read more about Mark J. Price