CHAPTER 9

image

Methods

Methods are reusable code blocks that will only execute when called.

Defining methods

A method can be created inside a class by typing void followed by the method’s name, a set of parentheses and a code block. The void keyword means that the method will not return a value. The naming convention for methods is the same as for classes – a descriptive name with each word initially capitalized.

class MyApp{  void MyPrint()  {    System.Console.Write("Hello World");  }}

All methods in C# must belong to a class, and they are the only place where statements may be executed. C# does not have global functions, which are methods defined ...

Get C# Quick Syntax Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.