Академический Документы
Профессиональный Документы
Культура Документы
Robert J. Oberg
Student Guide
Revision 1.0
C# Essentials
Rev. 1.0
Student Guide
Information in this document is subject to change without notice. Companies, names and data
used in examples herein are fictitious unless otherwise noted. No part of this document may be
reproduced or transmitted in any form or by any means, electronic or mechanical, for any
purpose, without the express written permission of Object Innovations.
Product and company names mentioned herein are the trademarks or registered trademarks of
their respective owners.
string ............................................................................................................................. 46
Classes and Structs........................................................................................................ 47
CHotel Class ................................................................................................................. 48
Hotel Struct ................................................................................................................... 49
Test Program................................................................................................................. 50
RaisePrice Methods ...................................................................................................... 51
Comparing Struct and Class.......................................................................................... 52
Arrays............................................................................................................................ 53
One Dimensional Arrays............................................................................................... 54
System.Array ................................................................................................................ 55
Jagged Arrays................................................................................................................ 56
Rectangular Arrays ....................................................................................................... 57
foreach for Arrays ......................................................................................................... 58
Boxing and Unboxing ................................................................................................... 59
Output in C#.................................................................................................................. 60
Formatting..................................................................................................................... 61
Formatting Example...................................................................................................... 62
Exceptions..................................................................................................................... 63
Exception Example ....................................................................................................... 64
Checked Integer Arithmetic.......................................................................................... 65
Throwing New Exceptions ........................................................................................... 66
finally ............................................................................................................................ 67
System.Exception.......................................................................................................... 68
Lab 2 ............................................................................................................................. 69
Summary....................................................................................................................... 70
Chapter 3 Object-Oriented Programming in C# ................................................... 73
C# Object-Oriented Features ........................................................................................ 75
Encapsulation and Accessors ........................................................................................ 76
Using a Property ........................................................................................................... 77
Indexers......................................................................................................................... 78
Bank Example............................................................................................................... 79
Account Class ............................................................................................................... 80
Namespace .................................................................................................................... 82
Constructors .................................................................................................................. 83
Static Members ............................................................................................................. 84
Static in Main................................................................................................................ 85
Inheritance in C#........................................................................................................... 86
New Version of Base Class........................................................................................... 87
Features of the New Base Class.................................................................................... 88
Derived Class ................................................................................................................ 89
Overriding a Virtual Function....................................................................................... 90
Fragile Base Class Problem .......................................................................................... 91
Access Control and Assemblies.................................................................................... 92
Assembly Example ....................................................................................................... 93
Internal Accessibility .................................................................................................... 94
Building Modules and Assemblies ............................................................................... 95
Lab 3 ............................................................................................................................. 96
Summary....................................................................................................................... 97
Chapter 4 C# and the .NET Framework .............................................................. 101
Components and OO in C# ......................................................................................... 103
Copy Semantics in C#................................................................................................. 104
Arrays.......................................................................................................................... 105
Embedded Objects ...................................................................................................... 106
Copying a Class Instance ............................................................................................ 107
class vs. struct ............................................................................................................. 108
Copying a Struct Instance ........................................................................................... 109
Shallow Copy.............................................................................................................. 110
Shallow Copy Output.................................................................................................. 111
Deep Copy and ICloneable ......................................................................................... 112
Interfaces..................................................................................................................... 113
Generic Interfaces ....................................................................................................... 114
Implementing ICloneable............................................................................................ 115
Using an Interface ....................................................................................................... 116
is Operator................................................................................................................... 117
as Operator .................................................................................................................. 118
.NET and COM........................................................................................................... 119
Arrays and Collections................................................................................................ 120
Lab 4A ........................................................................................................................ 121
Sorting an Array.......................................................................................................... 122
Anatomy of Array.Sort ............................................................................................... 123
Using the is Operator .................................................................................................. 124
The Use of Dynamic Type Checking.......................................................................... 125
Implementing IComparable ........................................................................................ 126
An Incomplete Solution .............................................................................................. 127
Overriding ToString.................................................................................................... 128
Understanding Frameworks ........................................................................................ 129
Delegates..................................................................................................................... 130
Stock Market Simulation ............................................................................................ 131
Running the Simulation .............................................................................................. 132
Delegate Code............................................................................................................. 133
Random Number Generation ...................................................................................... 134
Using the Delegates .................................................................................................... 135
Events.......................................................................................................................... 136
Events in C# and .NET ............................................................................................... 137
Client Side Event Code............................................................................................... 139
Chat Room Example ................................................................................................... 140
Lab 4B......................................................................................................................... 141
Summary..................................................................................................................... 142
Chapter 5 Introduction to Windows Forms ......................................................... 149
Creating a Windows Forms App................................................................................. 151
Windows Forms Event Handling................................................................................ 155
Add Events for a Control ............................................................................................ 156
Chapter 3
Object-Oriented Programming
in C#
Object-Oriented Programming in C#
Objectives
C# Object-Oriented Features
Using a Property
Indexers
Bank Example
Account Class
namespace OI.NetCs.Examples
{
using OI.NetCs.Examples;
Namespace
Constructors
Static Members
− All instances of the class share the same static field (for all
accounts the bank name is the same).
Static in Main
Inheritance in C#
Derived Class
public class CheckingAccount : Account
{
private int minBalance;
public CheckingAccount(int bal)
: base(bal)
{
minBalance = balance;
}
public CheckingAccount(int bal, string owner)
: base(bal, owner)
{
minBalance = bal;
}
public int Fee
{
get
{
if (minBalance < 100)
return numXact;
else
return 0;
}
}
...
− The base keyword is used to call the base class, as was done
in the constructors.
Assembly Example
Account.DLL Bank.DLL
Account CheckingAccount
SimpleMath Statement
TestBank.EXE
Internal Accessibility
using OI.NetDev.Examples;
Lab 3
Account
CheckingAccount SavingsAccount
Summary
Lab 3
Introduction
In this lab you will practice working with inheritance in C# by adding a savings
account class to a simple bank account hierarchy. You will also observe the use of
virtual and override in C#.
Account
CheckingAccount SavingsAccount
NOTE: This version of the bank example is a little different than the one used in the
lecture.
Our example will consists of four classes, each in its own file.
Files: Account.cs
SavingsAccount.cs
CheckingAccount.cs
TestAccount.cs.cs
Instructions
1. Build the starter program and study the code. There is only a CheckingAccount class
at this point.
3. Modify the test program to create a SavingsAccount object and perform a few
transactions, and show the account before and after. Notice that at this point you have
three overloaded ShowAccount helper methods, one for each kind of account. You are
now at Step 2.
4. Modify the program to use virtual methods, and collapse the ShowAccount methods
to a single method that applies polymorphically to all the different kinds of accounts.