1 08 00407 000 04 18 19 Sample
1 08 00407 000 04 18 19 Sample
AL
ut
ho
riz
U
ed
R
AT
ep
ro
d
uc
IO
tio
n
or
N
D
is
tri
C
bu
tio
O
n
Object Oriented
Pr
PY
oh
Programming in C#
bii
te
Student Workbook
d
Object-Oriented Programming in C#
Rev. 4.8.5
EV
Student Guide
U
na
AL
Information in this document is subject to change without notice. Companies, names and data used
ut
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,
ho
U
Product and company names mentioned herein are the trademarks or registered trademarks of their
ed
respective owners.
R
AT
ep
ro
d uc
IO
tio
Object Innovations
O
877-558-7246
n
www.objectinnovations.com
Pr
PY
oh
i bi
te
d
AL
Chapter 6 Object-Oriented Programming
ut
ho
Chapter 7 Classes
riz
AT
Chapter 10 Characters and Strings
ep
Chapter 12 Inheritance
uc
IO
Chapter 13 Virtual Methods and Polymorphism
tio
Chapter 15 Exceptions
N
D
Chapter 16 Interfaces
is
tri
PY
oh
AL
on.
ut
ho
U
named after the lab number. Starter code is frequently
ed
AT
The CaseStudy directory contains a case study in multiple
ep
ro
steps.
d uc
N
Supplements directory. Code examples for the supplements
D
is
C
bu
tio
O
n
Pr
PY
oh
i bi
te
d
IntelliSense ....................................................................................................................... 15
Build the Project ............................................................................................................... 16
R
AT
Run the Project.................................................................................................................. 17
ep
Summary ........................................................................................................................... 21
IO
tio
Exercise............................................................................................................................. 30
is
Answer .............................................................................................................................. 31
tri
Variables ........................................................................................................................... 32
C
bu
Expressions ....................................................................................................................... 33
tio
Assignment ....................................................................................................................... 34
O
Calculations Using C# ...................................................................................................... 35
n
Sample Program................................................................................................................ 36
Pr
Input in C# ........................................................................................................................ 38
More about Classes........................................................................................................... 39
i bi
AL
Decimal Type.................................................................................................................... 62
ut
Decimal Literals................................................................................................................ 63
Character Type.................................................................................................................. 64
ho
Character Literals.............................................................................................................. 65
riz
string ................................................................................................................................. 66
U
ed
AT
Implicit Conversions......................................................................................................... 69
ep
Explicit Conversions......................................................................................................... 70
Conversions Example ....................................................................................................... 71
ro
Nullable Types.................................................................................................................. 72
d
Lab 3 ................................................................................................................................. 73
uc
IO
Summary ........................................................................................................................... 74
tio
N
Arithmetic Operators ........................................................................................................ 80
Multiplication ................................................................................................................... 81
D
is
Division............................................................................................................................. 82
tri
Additive Operators............................................................................................................ 83
C
bu
Short-Circuit Evaluation................................................................................................... 88
Ternary Conditional Operator........................................................................................... 90
PY
oh
Assignment Operators....................................................................................................... 94
d
Expressions ....................................................................................................................... 95
Precedence ........................................................................................................................ 96
Associativity ..................................................................................................................... 97
Checking ........................................................................................................................... 98
Lab 4 ................................................................................................................................. 99
Summary ......................................................................................................................... 100
Arrays.............................................................................................................................. 113
na
AL
Fibonacci Example ......................................................................................................... 114
ut
continue........................................................................................................................... 117
riz
AT
Multiple Methods............................................................................................................ 121
ep
IO
tio
N
Object Models................................................................................................................. 133
Reusable Software Components ..................................................................................... 134
D
is
Abstraction...................................................................................................................... 137
Encapsulation.................................................................................................................. 138
tio
Polymorphism................................................................................................................. 143
i
Initialization.................................................................................................................... 164
na
AL
Initialization with Constructors ...................................................................................... 165
ut
AT
Static Constructor ........................................................................................................... 174
ep
N
Test Program................................................................................................................... 186
Copying a Structure ........................................................................................................ 187
D
is
HotelCopy.cs................................................................................................................... 189
C
Results of Hotel Copy..................................................................................................... 190
bu
object............................................................................................................................... 196
i
Arrays.............................................................................................................................. 198
te
AL
Structure Parameters ....................................................................................................... 220
ut
AT
Properties Examples ....................................................................................................... 228
ep
IO
Sample Program.............................................................................................................. 237
tio
N
Chapter 10 Characters and Strings .............................................................................. 243
Characters ....................................................................................................................... 245
D
is
AL
Random Number Generation .......................................................................................... 281
ut
U
Arrays as Collections ...................................................................................................... 285
ed
AT
Bank Class ...................................................................................................................... 292
ep
Indexers........................................................................................................................... 301
IO
tio
N
Summary ......................................................................................................................... 306
D
O
Access Control................................................................................................................ 315
n
AL
Running the Case Study.................................................................................................. 340
ut
U
Introduction to Polymorphism ........................................................................................ 347
ed
AT
Type Conversions in Inheritance .................................................................................... 350
ep
N
override Keyword ........................................................................................................... 358
Polymorphism................................................................................................................. 359
D
is
Account........................................................................................................................... 375
d
Currency.......................................................................................................................... 394
na
AL
Currency Format Example.............................................................................................. 395
ut
AT
User Defined Conversions: Example.............................................................................. 404
ep
N
Context and Stack Unwinding ........................................................................................ 415
Exception Example ......................................................................................................... 416
D
is
AL
IStatement Methods ........................................................................................................ 454
ut
IChecking........................................................................................................................ 455
ISavings .......................................................................................................................... 456
ho
AT
Access Modifier.............................................................................................................. 462
ep
Overview......................................................................................................................... 467
IO
tio
N
foreach Loop ................................................................................................................... 471
Array Notation ................................................................................................................ 472
D
is
AL
Writing Generic Code..................................................................................................... 501
ut
AT
Lab 17B........................................................................................................................... 508
ep
IO
tio
N
Usage of Delegates ......................................................................................................... 521
Declaring a Delegate....................................................................................................... 522
D
is
Account.cs....................................................................................................................... 529
DelegateAccount.cs ........................................................................................................ 530
PY
oh
Events.............................................................................................................................. 535
Events in C# and .NET ................................................................................................... 536
Client Side Event Code................................................................................................... 538
Chat Room Example ....................................................................................................... 539
Lab 18 ............................................................................................................................. 540
Summary ......................................................................................................................... 541
AL
Summary ......................................................................................................................... 558
ut
U
dynamic versus object..................................................................................................... 567
ed
AT
Named Arguments .......................................................................................................... 570
ep
N
Contravariance Example................................................................................................. 578
Asynchronous Programs in C# 5.0 ................................................................................. 579
D
is
Tuples.............................................................................................................................. 591
d
AL
ut
ho
riz
U
ed
R
AT
ep
ro
d uc
IO
tio
n
or
N
D
is
tri
C
bu
tio
O
n
Pr
PY
oh
i bi
te
d
AL
ut
ho
Introduction to .NET
riz
U
ed
R
AT
ep
ro
d uc
IO
tio
n
or
N
D
is
tri
C
bu
tio
O
n
Pr
PY
oh
i bi
te
d
Introduction to .NET
Objectives
EV
After completing this unit you will be able to:
U
AL
ut
AT
ep
IO
for creating C# programs.
tio
n
N
D
is
tri
C
bu
tio
O
n
Pr
PY
oh
i bi
te
d
What Is .NET?
AL
ut
languages.
riz
U
ed
AT
F# is a functional language which also supports object-
ep
ro
.NET platforms.
n
or
runs.
tio
O
.NET Framework is the original implementation of .NET
n
Pr
AL
NuGet is a package manager specifically designed for .NET.
ut
ho
extend .NET.
U
ed
AT
ep
IO
tio
N
It can also be used for ASP.NET Core apps and services.
D
is
tri
O
It comes with built in support for JavaScript and extensions
n
Pr
and others.
i bi
te
d
Application Models
AL
macOS and Linux
ut
ho
Mobile applications
riz
U
Cross-platform targeting iOS, Android and Windows
ed
R
Desktop
AT
ep
ro
IO
tio
deployable units.
tio
O
n
Pr
PY
oh
i bi
te
d
Managed Code
AT
ep
Core.
ro
IO
desktop.
tio
N
A runtime provides various services such as
D
is
C
on.
bu
tio
O
n
Pr
PY
oh
i bi
te
d
U
ed
Programs will compile into .EXE files that will run in the
R
AT
CLR.
ep
IO
tio
// SimpleCalc.cs
n
//
or
N
// This program does a simple calculation:
D
using System;
C
bu
tio
{
PY
oh
int height = 5;
te
int area;
d
AL
ut
easier.
ho
U
highlighting editor.
ed
AT
ep
IO
course to create and compile your C# programs.
tio
n
N
Appendix A.
D
is
C
bu
O
Although it is easy to create and run .NET Core
n
Pr
AL
ut
ho
riz
U
ed
R
AT
ep
ro
d uc
IO
tio
n
or
N
D
is
tri
C
bu
tio
O
n
Pr
PY
oh
i bi
te
d
earlier.
na
AL
ut
project". This will bring up the New Project dialog. (You can
riz
also use the menu File | New | Project when the main Visual
U
ed
AT
ep
ro
d uc
IO
tio
n
or
N
D
is
tri
C
bu
tio
O
n
Pr
PY
oh
i bi
te
d
AL
ut
ho
riz
U
ed
R
AT
ep
ro
d uc
IO
tio
n
or
N
D
is
tri
C
bu
tio
O
3. Click Next.
n
Pr
PY
oh
i bi
te
d
4.7.2.
na
AL
ut
ho
riz
U
ed
R
AT
ep
ro
d uc
IO
tio
n
or
N
D
is
tri
C
bu
tio
O
n
Pr
5. Click Create.
PY
oh
i bi
te
d
1
Examples in later chapters may place project and solution in the same directory.
AL
ut
ho
riz
U
ed
R
AT
ep
ro
d uc
IO
tio
n
or
N
D
is
tri
C
bu
tio
O
n
Pr
PY
oh
i bi
te
d
using System;
na
AL
using System.Collections.Generic;
ut
using System.Linq;
ho
using System.Text;
riz
using System.Threading.Tasks;
U
ed
AT
{
ep
{
d
IO
int height = 5;
tio
int area;
n
}
is
}
tri
C
bu
namespace.
Pr
PY
Unneeded ones can be removed by right-clicking over the
oh
i
using area and selecting Remove ... from the context menu.
bi
te
d
IntelliSense
AL
ut
ho
riz
U
ed
R
AT
ep
ro
d uc
IO
tio
n
or
N
D
O
n
Pr
PY
oh
i bi
te
d
AL
ut
U
ed
R
AT
ep
ro
d
IO
following:
tio
n
N
keyboard shortcut Ctrl+Shift+B.
D
is
C
just builds the project SimpleCalcVs)2.
bu
tio
O
n
Pr
PY
oh
i bi
te
d
2
The two are the same in this case, because the solution has only one project, but some solutions have
multiple projects, and then there is a difference.
AL
Toolbar button (This button is not provided by default;
ut
AT
ep
IO
Toolbar button
tio
n
N
D
is
tri
C
bu
tio
O
n
Pr
PY
oh
i bi
te
d
AL
ut
U
{
ed
{
AT
ep
int area;
d uc
Console.ReadLine();
or
N
}
D
}
is
tri
C
This program is saved as a Visual Studio solution in
bu
tio
Chap01\SimpleCalcVs.
O
n
PY
oh
AL
not .NET Core), it is easy to create Windows GUI programs
ut
ho
in C#.
riz
U
Example: See Chap01\SimpleCalcGui
ed
R
AT
ep
ro
d uc
IO
tio
n
or
N
D
is
C
bu
Chapter 19.
tio
O
n
Pr
PY
oh
i bi
te
d
.NET Documentation
AL
https://docs.microsoft.com
ut
ho
Select .NET.
riz
U
ed
R
AT
ep
ro
d uc
IO
tio
n
or
N
D
is
tri
C
bu
tio
O
n
Pr
PY
oh
i bi
te
d
Summary
.NET.
riz
U
ed
AT
.NET platforms.
ep
ro
IO
tio
N
Windows for building all types of .NET applications.
D
is
tri
O
n
documentation online.
PY
oh
i bi
te
d
AL
ut
ho
riz
U
ed
R
AT
ep
ro
d uc
IO
tio
n
or
N
D
is
tri
C
bu
tio
O
n
Pr
PY
oh
i bi
te
d
AL
ut
ho
First C# Programs
riz
U
ed
R
AT
ep
ro
d uc
IO
tio
n
or
N
D
is
tri
C
bu
tio
O
n
Pr
PY
oh
i bi
te
d
First C# Programs
Objectives
EV
After completing this unit you will be able to:
U
AL
ut
development environment.
riz
U
ed
AT
ep
namespaces.
d uc
IO
Use variables and simple expressions in C# programs.
tio
n
N
calculations.
D
is
C
bu
O
n
PY
oh
i bi
te
d
Hello, World
AL
language, including output.
ut
ho
U
program.
ed
AT
ep
// Hello.cs
IO
tio
class Hello
n
{
or
N
public static int Main(string[] args)
D
{
is
System.Console.WriteLine(
tri
C
"Hello, World");
bu
return 0;
tio
}
O
n
}
Pr
PY
oh
i bi
te
d
Program Structure
// Hello.cs
EV
class Hello
{
U
...
na
}
AL
ut
programming.
R
AT
ep
All of the code defining the class (its variables and methods)
IO
tio
C
bu
PY
oh
i bi
te
d
{
AL
...
ut
return 0;
ho
}
riz
U
}
ed
AT
ep
IO
tio
N
void if you do not return an exit code.
D
is
tri
O
n
PY
oh
The runtime will call this Main method – it is the entry point
te
d
{
AL
System.Console.WriteLine(
ut
"Hello, World");
ho
return 0;
riz
U
}
ed
}
R
AT
Every method in C# has one or more statements.
ep
ro
IO
A statement may be spread out over several lines.
tio
n
N
D
line.
tio
O
n
Pr
PY
oh
i bi
te
d
Namespaces
AL
The fully-qualified name of a class is specified by the
ut
ho
name.
U
ed
System.Console
R
AT
ep
IO
// Hello2.cs
tio
n
using System;
or
N
D
class Hello
is
{
tri
{
tio
Console.WriteLine("Hello, World");
O
}
n
}
Pr
PY
oh
Main method.
This version of the program also illustrates a Main()
method with no command-line arguments and void
return type.
Exercise
name).
na
AL
ut
AT
ep
Hello, World
ro
My name is Bob
d
Goodbye
uc
IO
tio
n
or
N
D
is
tri
C
bu
tio
O
n
Pr
PY
oh
i bi
te
d
Answer
// Hello3.cs
EV
using System;
U
class Hello
na
{
AL
public static void Main()
ut
{
ho
Console.WriteLine("Hello, World");
riz
U
Console.WriteLine("My name is Bob");
ed
Console.WriteLine("Goodbye");
}
R
AT
ep
}
ro
d uc
IO
tio
n
or
N
D
is
tri
C
bu
tio
O
n
Pr
PY
oh
i bi
te
d
Variables
AL
ut
Some common types are int for integers and double for
ho
U
You must declare variables before you can use them.
ed
R
AT
A variable declaration reserves memory space for the
ep
// not initialize
or
N
D
C
bu
int kilo;
tio
kilo = 1024;
O
n
PY
oh
i bi
te
d
Expressions
arithmetic operators:
AL
ut
+ addition
ho
- subtraction
riz
* multiplication
U
ed
/ division
R
AT
Here are some examples of expressions:
ep
ro
kilo * 1024
d uc
(fahrenheit – 32) * 5 / 9
IO
3.1416 * radius * radius
tio
n
or
N
D
is
tri
C
bu
tio
O
n
Pr
PY
oh
i bi
te
d
Assignment
AL
On the right hand side is an expression.
ut
ho
AT
semicolon.
ep
ro
IO
area = 3.1416 * radius * radius;
tio
n
N
of an assignment statement.
D
is
int item = 5;
tri
C
int total = 30;
bu
O
n
new value.
i bi
te
d
Calculations Using C#
AL
Create expressions and assign values to your variables.
ut
ho
U
You can easily perform labeled output, relying on two
ed
features of C#:
R
AT
ep
IO
converts numeric data to string data when required.
tio
n
N
D
C
bu
O
n
The total is 35
Pr
PY
oh
i bi
te
d
Sample Program
AL
// Convert.cs - Step 1
ut
//
ho
// Fahrenheit to Celsius
U
ed
using System;
R
AT
ep
class Convert
ro
{
d
IO
{
tio
}
is
tri
}
C
bu
tio
O
n
Pr
PY
oh
i bi
te
d
AL
Write() writes out just the string without the new line.
ut
ho
int x = 24;
riz
int y = 5;
U
int z = x * y;
ed
AT
Console.WriteLine(" is " + z);
ep
IO
tio
N
D
just discussed.
i bi
te
Input in C#
AL
ut
U
Fahrenheit temperature.
ed
AT
ep
programming.
d uc
IO
We can encapsulate or “wrap” the details of input in a class.
tio
n
N
D
is
tri
C
bu
tio
O
n
Pr
PY
oh
i bi
te
d
objects.
AL
ut
AT
ep
keyword.
uc
IO
tio
N
D
InputWrapper class.
is
tri
C
bu
tio
O
n
Pr
PY
oh
i bi
te
d
InputWrapper Class
string.
na
AL
ut
provided.
riz
U
A prompt string is passed as an input parameter.
ed
R
AT
See files InputWrapper.cs in directory TestInputWrapper,
ep
IO
Although the code is quite short, it is a little complex,
tio
n
N
different .NET Framework classes.
D
is
C
implementation of InputWrapper in order to use it.
bu
tio
PY
oh
i bi
te
d
Echo Program
a personalized greeting.
na
AL
ut
See Echo.
ho
riz
AT
ep
IO
// EchoName.cs
tio
//
n
N
// prints out greeting using name
D
is
using System;
tri
C
bu
class Echo
tio
{
O
public static void Main(string[] args)
n
Pr
{
PY
InputWrapper iw = new InputWrapper();
oh
}
d
Using InputWrapper
AL
Prompt to obtain input data by calling the appropriate
ut
getXXX method.
ho
riz
U
ed
R
AT
ep
ro
d uc
IO
tio
n
or
N
D
is
tri
C
bu
tio
O
n
Pr
PY
oh
i bi
te
d
AT
ep
Solution Explorer.
ro
d uc
IO
tio
n
or
N
D
is
tri
C
bu
tio
O
n
You can also build just the current project via the toolbar
bi
te
button .
d
AT
streams for I/O.
ep
ro
N
released version of the .NET Framework at the time
D
C
bu
tio
O
n
Pr
PY
oh
i bi
te
d
AL
ut
XML.
riz
U
System.Drawing contains classes providing GDI+ graphics
ed
functionality.
R
AT
ep
IO
System.Web provides support for browser/server
tio
communication.
n
or
N
System.IO provides support for reading and writing with
D
are supported.
C
bu
O
protocols.
n
Pr
PY
oh
i bi
te
d
Lab 2
AT
of the chapter.
ep
ro
IO
tio
n
or
N
D
is
tri
C
bu
tio
O
n
Pr
PY
oh
i bi
te
d
Summary
and operators.
U
ed
AT
value computed by an expression to a variable.
ep
ro
programming interface.
or
N
The .NET Framework has a large class library that is
D
is
C
bu
tio
O
n
Pr
PY
oh
i bi
te
d
Lab 2
Introduction
EV
In this lab, you modify or implement several C# programs to perform
calculations. You need to perform input (through a wrapper class), perform a calculation,
U
and output the result. Do as many of these exercises as time permits. If you have extra
na
AL
time, do some of the optional experiments suggested in some of the exercises, or make up
some experiments on your own.
ut
ho
U
Root Directory: OIC\CSharp
ed
AT
ep
IO
Chap02\Circle (Exercise 2 answer)
tio
N
Examine the code of the starter program. Build and run. Notice that the
D
user for a Fahrenheit temperature, read in the value entered by the user, and print out the
tri
result. Make use of the wrapper class InputWrapper that was discussed in this chapter.
C
bu
The starter program uses int as the data type for temperatures. An optional
tio
experiment is to use double as the data type. Could you input the Fahrenheit temperature
O
n
PY
Exercise 2. Calculate the Area of a Circle
oh
i
Use Visual Studio to create an empty C# project Circle in the Lab2 folder. This
bi
will create the folder Circle. Add a new file Circle.cs to your project, where you will
te
Write a C# program to calculate the area of a circle. Prompt the user for the
radius, read in the value entered by the user, calculate the area of the circle, and print out
the result. For pi, use the approximation 3.1416. What is an appropriate data type to use
for radius and area?
As an optional experiment, use the class Math (in the namespace System) for a
more accurate value of pi.
Another optional experiment is to capture the output data in a file. This is easy to
do at the command line, by using the > to “redirect” output to a file.
EV
Circle > output.txt
When using this technique, the prompts are written to the output file and not displayed on
the screen. Hence you need to know what to type! If you type “10” for the radius, your
U
na
U
ed
R
AT
ep
ro
d uc
IO
tio
n
or
N
D
is
tri
C
bu
tio
O
n
Pr
PY
oh
i bi
te
d
AL
ut
ho
riz
U
ed
R
AT
ep
ro
d uc
IO
tio
n
or
N
D
is
tri
C
bu
tio
O
n
Pr
PY
oh
i bi
te
d
AL
ut
ho
riz
U
ed
R
AT
ep
ro
ucd
IO
7400 E. Orchard Road, Suite 1450 N
tio
www.ITCourseware.com
or
N
D
is
tri
C
bu
tio
O
n
Pr
PY
oh
bii
te
d
9-08-00407-000-04-18-19