Account
ClassA person drives a car by telling it what to do (go faster, go slower, turn left, turn right, etc.)—without having to know how the car’s internal mechanisms work. Similarly, a method (such as Main
) “drives” an Account
object by calling its methods—without having to know how the class’s internal mechanisms work. In this sense, the class containing method Main
is referred to as a driver class. We show the Main
method and its output first, so you can see an Account
object in action.
To help you prepare for the larger programs you’ll encounter later in this book and in industry, we define class AccountTest
and its Main
method in the file AccountTest.cs
(Fig. 4.1). We define the Account
class in its own file as well (file Account.cs
, Fig. 4.2). After we present classes AccountTest
(in this section) and Account
(in Section 4.3), Section 4.4 discusses how to create and build a project that contains multiple .cs
source-code files. First, let’s walk through the AccountTest
class.
new
Typically, you cannot call a method of a class until you create an object of that class.1 Line 10
Account myAccount = new Account();
uses an object-creation expression
new Account()
to create an Account
object, then assigns it to the variable myAccount
. The variable’s type is Account
—the class we’ll define in Fig. 4.2. Keyword new
creates a new object of the specified class—in this case, Account
. The parentheses to the right of Account
are required (we’ll discuss these in Section 4.8).
Account
’s GetName
MethodThe Account
class’s GetName
method returns the account name stored in a particular Account
object. Line 13
Console.WriteLine($"Initial name is: {myAccount.GetName()}");
displays myAccount
’s initial name by calling the object’s GetName
method with the expression myAccount.GetName()
. To call this method for a specific object, you specify
the object’s name (myAccount
) followed by
the method name (GetName
) and
a set of parentheses.
The empty parentheses indicate that GetName
does not require any additional information to perform its task. Soon, you’ll see the SetName
method that does require additional information to perform its task.
When Main
calls the GetName
method:
The app transfers execution from the expression myAccount.GetName()
(line 13 in Main
) to method GetName
’s declaration (which we’ll study in Section 4.3). Because GetName
was accessed via the object myAccount
, GetName
knows which object’s data to manipulate.
Next, method GetName
performs its task—that is, it returns the myAccount
object’s name to line 13 where the method was called.
Console.WriteLine
displays the string
returned by GetName
—the name is inserted into the interpolated string
in place of the call to GetName
—then the program continues executing at line 16 in Main
.
Because we have not yet stored a name in the myAccount
object, line 13 does not display a name.
Next, lines 16–17 prompt for and input a name. Line 17
string theName = Console.ReadLine(); // read a line of text
uses Console
method ReadLine
to read the name from the user and assign it to the string
variable theName
. The user types the name (in this case, Jane Green
) and presses Enter to submit it to the app. Method ReadLine
reads a whole line, including all the characters the user types until the newline that the user typed by pressing Enter—the newline is discarded. Pressing Enter also positions the output cursor to the beginning of the next line in the console window, so the program’s next output begins on the line below the user’s input.
Account
’s SetName
MethodThe Account
class’s SetName
method stores (sets) an account name in a particular Account
object. Line 18
myAccount.SetName(theName); // put theName in the myAccount object
calls myAccounts
’s SetName
method, passing theName
’s value as SetName
’s argument. The method stores this value in the object myAccount
—we’ll see exactly where it’s stored in the next section.
When Main
calls the SetName
method:
The app transfers program execution from line 18 in Main
to method SetName
’s declaration. Because method SetName
was accessed via the myAccount
object, SetName
“knows” which object to manipulate.
Next, method SetName
stores the argument’s value in the myAccount
object (we’ll see exactly where in Section 4.3).
When SetName
completes execution, program control returns to where method SetName
was called (line 18 in Main
), then execution continues at line 21.
To demonstrate that myAccount
now contains the name the user entered, line 21
Console.WriteLine($"myAccount's name is: {myAccount.GetName()}");
calls myAccounts
’s GetName
method again. As you can see in the last line of the program’s output, the name entered by the user in line 17 is displayed. When the preceding statement completes execution, the end of Main
is reached, so the app terminates.