Chapter 4
Let Me Say This about this
IN THIS CHAPTER
Passing an object to a method
Comparing class and instance methods
Understanding this
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 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.