Chapter 4

Let Me Say This about this

IN THIS CHAPTER

check Passing an object to a method

check Comparing class and instance methods

check Understanding this

check Working with local functions

This chapter moves from the static methods emphasized in Chapter 3 in this minibook to the nonstatic methods of a class. Static methods belong to the whole class, and nonstatic methods belong to each instance created from the class. Important differences exist between static and nonstatic class members.

Passing an Object to a Method

You pass object references as arguments to methods in the same way as you pass value-type variables, with one difference: You always pass objects by reference. The following small program demonstrates how you pass objects — to methods, that is:

using System; // PassObject -- Demonstrate how to pass an object to a method.namespace PassObject{ public class Student { public string name; } public class Program { public static void Main(string[] args) { Student student = new Student(); // Set the name by accessing it directly. Console.WriteLine("The first time:"); student.name = "Madeleine"; ...

Get C# 7.0 All-in-One For Dummies now with O’Reilly online learning.

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