Chapter 01: A First Program in C#
Chapter 01: A First Program in C#
Chapter 01: A First Program in C#
1
Objectives
• Learn about programming tasks
• Learn object-oriented programming concepts
• Learn about the C# programming language
• Learn how to write a C# program that
produces output
• Learn how to select identifiers to use within
your programs
2
Objective
3
Programming
4
Object-Oriented Programming
5
Object-Oriented Programming
6
Object-Oriented Programming
• For example:
– An Automobile is a class whose objects have the following
attributes: year, make, model, color, and current running status
– Your 1997 red Chevrolet is an instance of the class that is made
up of all Automobiles
• Methods of classes are used to change attributes and
discover values of attributes
– The Automobile class may have the following methods:
getGas(), accelerate(), applyBreaks()
7
Object-Oriented Programming
8
The C# Programming Language
9
Writing a C# Program that Produces Output
11
Writing a C# Program that Produces Output
• Console is a class
• Console defines the attributes of a collection of similar
“Console” objects
12
Writing a C# Program that Produces Output
13
Writing a C# Program that Produces Output
14
Selecting Identifiers
15
Selecting Identifiers
16
Writing a C# Program that Produces Output
17
Compiling and Executing a Program from
the Command Line
• After creating source code, you must do the following
before you can view the program output:
– Compile the source code into intermediate language
(IL)
– Use the C# just in time (JIT) compiler to translate the
intermediate code into executable statements
18
Compiling and Executing a Program from
the Command Line
• After compiling your source code (typing csc followed by
the filename), you will have three possible outcomes:
– You receive an operating system error message
– You receive one or more program language error
messages
– You receive no error messages, indicating that the
program has compiled successfully
19
Compiling and Executing a Program from
the Command Line
• If you receive an operating system message it may
mean that:
– You misspelled the command csc
– You misspelled the filename
– You forgot to include the extensions .cs with the
filename
– You didn’t use the correct case
– You are not within the correct subdirectory or folder
on your command line
– The C# compiler was not installed properly
– You need to set a path command
20
Compiling and Executing a Program from
the Command Line
• A syntax error occurs when you introduce typing errors
into your program
• The C# compiler issues warnings as wells as errors
• If a syntax error occurs, you must reopen the source
code and make the necessary corrections
• If you compile the program with no errors (using csc
file.cs) you can run the program from the command
prompt by typing the name of the .exe file created
21
Compiling and Executing a Program from
the Command Line
22
Adding Comments to a Program
23
Adding Comments to a Program
24
Compiling and Executing a Program Using
the Visual Studio IDE
• C# programs can also be written using the Visual Studio
IDE (instead of a text editor). This approach offers many
advantages including:
– Some of the code you need is already created for you
– The code is displayed in color, so you can more easily identify
parts of your program
– If error messages appear when you compile your program, you
can double-click on an error message and the cursor will move
to the line of code that contains the error
– Other debugging tools are available
25
Compiling and Executing a Program Using
the Visual Studio IDE
• Creating a project
27
Compiling and Executing a Program Using
the Visual Studio IDE
31
Compiling and Executing a Program Using
the Visual Studio IDE
34
Eliminating the Reference to Out by Using
the System Namespace
• When you need to repeatedly use a class from the same
namespace, you can shorten statements by using the
“using” keyword
• Output is identical as the previous version of ThreeLines
35
Eliminating the Reference to Out by Using
the System Namespace
36
Chapter Summary
37
Chapter Summary
38
Chapter Summary
39