Chapter 3

We Have Our Methods

IN THIS CHAPTER

check Defining a method

check Passing arguments to a method

check Getting results back

check Reviewing the WriteLine() method

Programmers need to be able to break large programs into smaller chunks that are easy to handle. For example, the programs contained in previous chapters of this minibook reach the limit of the amount of programming information a person can digest at one time.

remember C# lets you divide your class code into chunks known as methods. A method is equivalent to a function, procedure, or subroutine in other languages. The difference is that a method is always part of a class. Properly designed and implemented methods can greatly simplify the job of writing complex programs.

Defining and Using a Method

Consider the following example:

class Example{ public int anInt; // Nonstatic public static int staticInt // Static public void InstanceMethod() // Nonstatic { Console.WriteLine("this is an instance method"); } public static void ClassMethod() // ...

Get C# 7.0 All-in-One For Dummies 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.