chapter 1 - a first program
TRANSCRIPT
-
8/7/2019 Chapter 1 - A First Program
1/45
Microsoft Visual C# 2008
Comprehensive
Chapter 1A First Program Using C#
-
8/7/2019 Chapter 1 - A First Program
2/45
Microsoft Visual C# 2008 Comprehensive 2
Objectives
Learn about programming
Explore object-oriented programming concepts
Learn about the C# programming language
Write a C# program that produces output
Learn how to select identifiers to use within yourprograms
-
8/7/2019 Chapter 1 - A First Program
3/45
Microsoft Visual C# 2008 Comprehensive 3
Objectives (continued)
Add comments to a C# program
Eliminate the reference to Out by using the System
namespace
Write and compile a C# program using the commandprompt and using Visual Studio
Learn alternate ways to write the Main() method
-
8/7/2019 Chapter 1 - A First Program
4/45
Microsoft Visual C# 2008 Comprehensive 4
Programming
Computerprogram Set of instructions that tells a computer what to do
Machine language
Expressed as a series of 1s and 0s 1s represent switches that are on, and 0s represent
switches that are off
High-level programming languages
Use reasonable terms such as read, write, or add Instead of the sequence of on/off switches that perform
these tasks
-
8/7/2019 Chapter 1 - A First Program
5/45
Microsoft Visual C# 2008 Comprehensive 5
Programming (continued)
High-level programming language (continued)
Allows you to assign reasonable names to areas ofcomputer memory
Has its own syntax (rules of the language) Compiler
Translates high-level language statements intomachine code
Programming logic Involves executing the various statements and
procedures in the correct order
To produce the desired results
-
8/7/2019 Chapter 1 - A First Program
6/45
Microsoft Visual C# 2008 Comprehensive 6
Programming (continued)
Debugging
Process of removing all syntax and logical errors fromthe program
-
8/7/2019 Chapter 1 - A First Program
7/45
Microsoft Visual C# 2008 Comprehensive 7
Object-Oriented Programming
Procedural program Create and name computer memory locations that
can hold values (variables)
Write a series of steps or operations to manipulatethose values
Identifier A one-word name used to reference a variable
P
rocedures ormethods Logical units that group individual operations used ina computer program
Called orinvoked by other procedures or methods
-
8/7/2019 Chapter 1 - A First Program
8/45
Microsoft Visual C# 2008 Comprehensive 8
Object-Oriented Programming(continued)
Object-oriented programming An extension of procedural programming
New features include: Objects Similar to concrete objects in the real world
Contain their own variables and methods
Attributes of an object represent its characteristics
State of an object is the collective value of all itsattributes at any point in time
-
8/7/2019 Chapter 1 - A First Program
9/45
Microsoft Visual C# 2008 Comprehensive 9
Object-Oriented Programming(continued)
New features include (continued): Class
A category of objects or a type of object
Describes the attributes and methods of every objectthat is an instance, or example, of that class
Encapsulation
Technique of packaging an objects attributes and
methods into a cohesive unit That can be used as an undivided entity
Interface Interaction between a method and an object
-
8/7/2019 Chapter 1 - A First Program
10/45
Microsoft Visual C# 2008 Comprehensive 10
Object-Oriented Programming(continued)
New features include (continued): Polymorphism
Describes the ability to create methods that actappropriately depending on the context
Inheritance
Provides the ability to extend a class to create a morespecific class
-
8/7/2019 Chapter 1 - A First Program
11/45
Microsoft Visual C# 2008 Comprehensive 11
The C# Programming Language
C# programming language
Developed as an object-oriented and component-
oriented language Part of Microsoft Visual Studio 2008
Allows every piece of data to be treated as an objectand to employ the principles of object-orientedprogramming
Contains a GUI interface that makes it similar toVisual Basic
-
8/7/2019 Chapter 1 - A First Program
12/45
Microsoft Visual C# 2008 Comprehensive 12
The C# Programming Language(continued)
C# programming language (continued)
Modeled after the C++ programming language
However, eliminates some of the most difficult featuresto understand in C++
Very similar to Java
In C# simple data types are objects
-
8/7/2019 Chapter 1 - A First Program
13/45
Microsoft Visual C# 2008 Comprehensive 13
Writing a C# Program that ProducesOutput
literal string
argumentsmethodobjectclassnamespace
-
8/7/2019 Chapter 1 - A First Program
14/45
Microsoft Visual C# 2008 Comprehensive 14
Writing a C# Program that ProducesOutput (continued)
Namespace
Provides a way to group similar classes
C# method parts Method header
Includes the method name and information about whatwill pass into and be returned from a method
Method body Contained within a pair of curly braces and includes all
the instructions executed by the method
-
8/7/2019 Chapter 1 - A First Program
15/45
Microsoft Visual C# 2008 Comprehensive 15
Writing a C# Program that ProducesOutput (continued)
Whitespace
Any combination of spaces, tabs, and carriage returns(blank lines)
Organizes your code and makes it easier to read
Keywords
Predefined and reserved identifiers that have specialmeaning to the compiler
Access modifier
Defines the circumstances under which the methodcan be accessed
-
8/7/2019 Chapter 1 - A First Program
16/45
Microsoft Visual C# 2008 Comprehensive 16
Selecting Identifiers
Requirements
Must begin with an underscore, at sign (@), or letter
Letters include foreign-alphabet letters Can contain only letters or digits
Not special characters such as #, $, or &
Cannot be a C# reserved keyword
-
8/7/2019 Chapter 1 - A First Program
17/45
Microsoft Visual C# 2008 Comprehensive 17
Selecting Identifiers (continued)
-
8/7/2019 Chapter 1 - A First Program
18/45
Microsoft Visual C# 2008 Comprehensive 18
Selecting Identifiers (continued)
-
8/7/2019 Chapter 1 - A First Program
19/45
Microsoft Visual C# 2008 Comprehensive 19
Selecting Identifiers (continued)
-
8/7/2019 Chapter 1 - A First Program
20/45
Microsoft Visual C# 2008 Comprehensive 20
Selecting Identifiers (continued)
-
8/7/2019 Chapter 1 - A First Program
21/45
Microsoft Visual C# 2008 Comprehensive 21
Adding Comments to a Program
Program comments
Nonexecuting statements that document a program
Comment out
Turn a statement into a comment
Types of comments in C#
Line comments
Block comments XML-documentation format comments
-
8/7/2019 Chapter 1 - A First Program
22/45
Microsoft Visual C# 2008 Comprehensive 22
Adding Comments to a Program(continued)
-
8/7/2019 Chapter 1 - A First Program
23/45
Microsoft Visual C# 2008 Comprehensive 23
Eliminating the Reference to Out byUsing the System Namespace
-
8/7/2019 Chapter 1 - A First Program
24/45
Microsoft Visual C# 2008 Comprehensive 24
Eliminating the Reference to Out byUsing the System Namespace
(continued)
-
8/7/2019 Chapter 1 - A First Program
25/45
Microsoft Visual C# 2008 Comprehensive 25
Not Recommended: Using an Alias
using cannot be used with a class name
Alias
An alternative name for a class
Avoid using aliases if your intention is simply toreduce typing
-
8/7/2019 Chapter 1 - A First Program
26/45
Microsoft Visual C# 2008 Comprehensive 26
Not Recommended: Using an Alias(continued)
-
8/7/2019 Chapter 1 - A First Program
27/45
Microsoft Visual C# 2008 Comprehensive 27
Writing and Compiling a C# Program
Steps for viewing a program output
Compile source code into intermediate language(IL)
C#just in time (JIT) compiler translates theintermediate code into executable statements
-
8/7/2019 Chapter 1 - A First Program
28/45
Microsoft Visual C# 2008 Comprehensive 28
Compiling Code from the CommandPrompt
Command csc
Stands for C Sharp compiler
-
8/7/2019 Chapter 1 - A First Program
29/45
Microsoft Visual C# 2008 Comprehensive 29
Compiling Code from within the VisualStudio IDE
Advantages of using the Visual Studio IDE
Some of the code you need is already created for you
The code is displayed in color
You can double-click an error message and the cursorwill move to the line of code that contains the error
Other debugging tools are available
-
8/7/2019 Chapter 1 - A First Program
30/45
Microsoft Visual C# 2008 Comprehensive 30
Compiling Code from within the VisualStudio IDE (continued)
-
8/7/2019 Chapter 1 - A First Program
31/45
Microsoft Visual C# 2008 Comprehensive 31
Alternate Ways to Write a Main()
Method
Main() method with parameters
-
8/7/2019 Chapter 1 - A First Program
32/45
Microsoft Visual C# 2008 Comprehensive 32
Alternate Ways to Write a Main()
Method (continued)
Main() method with a return type
-
8/7/2019 Chapter 1 - A First Program
33/45
Microsoft Visual C# 2008 Comprehensive 33
You Do It
Enter your first C# program into a text editor so youcan execute it
-
8/7/2019 Chapter 1 - A First Program
34/45
Microsoft Visual C# 2008 Comprehensive 34
Entering a Program into an Editor
Use any text editor to write the following code andsave it as Hello.cs
-
8/7/2019 Chapter 1 - A First Program
35/45
Microsoft Visual C# 2008 Comprehensive 35
Compiling and Executing a Programfrom the Command Line
Type the command that compiles your program:
csc Hello.cs
-
8/7/2019 Chapter 1 - A First Program
36/45
Microsoft Visual C# 2008 Comprehensive 36
Compiling and Executing a Programfrom the Command Line (continued)
Execute the program Hello.exe
-
8/7/2019 Chapter 1 - A First Program
37/45
Microsoft Visual C# 2008 Comprehensive 37
Compiling and Executing a ProgramUsing the Visual Studio IDE
Steps
Create a new project (console application)
Enter the project name
Write your program using the editor
To compile the program, click Build on the menu bar,and then click Build Solution
As an alternative, you can press F6
Click Debug on the menu bar and then click StartWithout Debugging
-
8/7/2019 Chapter 1 - A First Program
38/45
Microsoft Visual C# 2008 Comprehensive 38
Compiling and Executing a ProgramUsing the Visual Studio IDE (continued)
-
8/7/2019 Chapter 1 - A First Program
39/45
Microsoft Visual C# 2008 Comprehensive 39
Compiling and Executing a ProgramUsing the Visual Studio IDE (continued)
-
8/7/2019 Chapter 1 - A First Program
40/45
Microsoft Visual C# 2008 Comprehensive 40
Compiling and Executing a ProgramUsing the Visual Studio IDE (continued)
-
8/7/2019 Chapter 1 - A First Program
41/45
Microsoft Visual C# 2008 Comprehensive 41
Compiling and Executing a ProgramUsing the Visual Studio IDE
(continued)
-
8/7/2019 Chapter 1 - A First Program
42/45
Microsoft Visual C# 2008 Comprehensive 42
Deciding which Method to Use
Advantage of using the command line
Saves disk space
Advantages of using the Visual Studio IDE
Automatic sentence completion
Words are displayed using different colors based ontheir category
Code automatically generated by the IDE is very
helpful when writing a GUI
-
8/7/2019 Chapter 1 - A First Program
43/45
Microsoft Visual C# 2008 Comprehensive 43
Adding Comments to a Program
Line comment example// Filename Hello.cs
// Written by
// Writtenon
Block comment example/* This programdemonstratestheuseof
the WriteLine()methodto printthe
message Hello, world!*/
-
8/7/2019 Chapter 1 - A First Program
44/45
Microsoft Visual C# 2008 Comprehensive 44
Summary
A computer program is a set of instructions that tella computer what to do
Procedural programming versus object-oriented
programming The C# programming language is an object-
oriented and component-oriented language
System.Console.Out.WriteLine() method
Produces a line of console output You can define a C# class or variable by using any
name or identifier
-
8/7/2019 Chapter 1 - A First Program
45/45
Microsoft Visual C# 2008 Comprehensive 45
Summary (continued)
Comments are nonexecuting statements that youadd to document a program
Or to disable statements when you test a program
Use of namespaces To create a C# program, you can use the Microsoft
Visual Studio environment
You have multiple options for writing the Main()
method header