1 chapter one a first program using c#. 2 objectives learn about programming tasks learn...

39
1 Chapter One A First Program Using C#

Upload: marion-foster

Post on 25-Dec-2015

218 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming

1

Chapter One

A First Program Using C#

Page 2: 1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming

2

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

Page 3: 1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming

3

Objective

• Learn how to compile and execute a C# program from the command line

• Learn how to add comments to a C# program• Learn how to compile and execute a program

using Visual Studio IDE• Learn how to eliminate the reference to Out by

using the System namespace

Page 4: 1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming

4

Programming

• A computer program is a set of instructions that you write to tell a computer what to do

• Programmers do not use machine language when creating computer programs. Instead, programmers tend to use high-level programming languages

• Each high-level language has its own syntax and limited set of vocabulary that is translated into machine code by a compiler

• In addition to understanding syntax, a programmer must also understand programming logic

Page 5: 1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming

5

Object-Oriented Programming

• Variables are named computer memory locations used to hold values that may vary

• Operations are usually called or invoked to manipulate variables

• A procedural program defines the variable memory locations, then calls a series of procedures to input, manipulate, and output the value stored in those locations

• A single procedural program often contains hundreds of variables and thousands of procedure calls

Page 6: 1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming

6

Object-Oriented Programming

• Object-oriented programming is an extension of procedural programming, which in addition to variables and procedures contains: objects, classes, encapsulation, interfaces, polymorphism, and inheritance

• Objects are object-oriented components• Attributes of an object represent its characteristics• A class is a category of objects or a type of object• An instance refers to an object based on a class

Page 7: 1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming

7

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()

Page 8: 1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming

8

Object-Oriented Programming

• Methods and variables in object-oriented programming are encapsulated, that is, users are only required to understand the interface and not the internal workings of the class

• Polymorphism and Inheritance are two distinguishing features in the object-oriented programming approach

• Polymorphism describes the ability to create methods that act appropriately depending on the context

• Inheritance provides the ability to extend a class so as to create a more specific class

Page 9: 1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming

9

The C# Programming Language

• C# was developed as an object-oriented and component-oriented language

• It exists as part of the Visual Studio .NET package• C# (like Java) is modeled after the C++ programming

language• Pointers are not used in C#• C# does NOT require the use of object destructors,

forward declarations, or #include files• It has the ability to pass by reference• Multiple inheritance is not allowed in C#

Page 10: 1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming

10

Writing a C# Program that Produces Output

• “This is my first C# program” is a literal string of characters

• The string appears in parenthesis because it is a parameter or an argument

• The WriteLine() method prints a line of output on the screen

Page 11: 1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming

11

Writing a C# Program that Produces Output

• Out is an object that represents the screen• The Out object was created and endowed with the

method WriteLine()• Not all objects have the WriteLine() method

Page 12: 1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming

12

Writing a C# Program that Produces Output

• Console is a class• Console defines the attributes of a collection of similar

“Console” objects

Page 13: 1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming

13

Writing a C# Program that Produces Output

• System is a namespace, which is a scheme that provides a way to group similar classes

• Namespaces are used to organize classes

Page 14: 1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming

14

Writing a C# Program that Produces Output

• The difference between the above code and the previous code is the amount of whitespace

• Both versions of code share the same method header (including access modifiers and other keywords)

Page 15: 1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming

15

Selecting Identifiers

• Every method used in C# must be part of a class• A C# class name or identifier must meet the basic

following requirements:– An identifier must begin with an underscore or a letter– An identifier can contain only letters or digits, not special

characters such as #,$, or &– An identifier cannot be a C# reserved keyword

Page 16: 1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming

16

Selecting Identifiers

• The reserved public keyword is an access modifier that defines the circumstance under which a class can be accessed

Page 17: 1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming

17

Writing a C# Program that Produces Output

• Code written for a C# program using a text editor

Page 18: 1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming

18

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

Page 19: 1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming

19

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

Page 20: 1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming

20

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

Page 21: 1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming

21

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

Page 22: 1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming

22

Compiling and Executing a Program from the Command Line

• Output of Hello Program

Page 23: 1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming

23

Adding Comments to a Program

• In large programs it becomes difficult to remember why certain steps were included and the role of certain variables and methods

• Program comments are nonexecuting statements that you add to document a program

• You can also comment out various statements in a program to debug and observe the effects of the program with the statement or statements commented out

Page 24: 1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming

24

Adding Comments to a Program

• There are three types of comments in C#:– Line comments– Block comments– XML-documentation format

Page 25: 1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming

25

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

Page 26: 1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming

26

Compiling and Executing a Program Using the Visual Studio IDE

• Navigating to Visual Studio .NET

Page 27: 1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming

27

Compiling and Executing a Program Using the Visual Studio IDE

• Creating a project

Page 28: 1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming

28

Compiling and Executing a Program Using the Visual Studio IDE

• Selecting project options

Page 29: 1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming

29

Compiling and Executing a Program Using the Visual Studio IDE

• The console application template

Page 30: 1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming

30

Compiling and Executing a Program Using the Visual Studio IDE

• Output screen after compiling the Hello program

Page 31: 1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming

31

Compiling and Executing a Program Using the Visual Studio IDE

• Output of the Hello program as run from the Visual Studio IDE

Page 32: 1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming

32

Compiling and Executing a Program Using the Visual Studio IDE

• List of Hello Program Files

Page 33: 1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming

33

Eliminating the Reference to Out by Using the System Namespace

• A program may contain an unlimited number of statements, as long as they are each terminated by a semicolon

Page 34: 1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming

34

Eliminating the Reference to Out by Using the System Namespace

• The Output of ThreeLines program

Page 35: 1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming

35

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

Page 36: 1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming

36

Eliminating the Reference to Out by Using the System Namespace

• An alias is an alternative name for a class• An alias can be used to shorten a long class name (as in

the above example)

Page 37: 1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming

37

Chapter Summary

• A computer program is a set of instructions that you write to tell a computer what to do

• Procedural Programming involves creating computer memory locations, called variables, and a set of operations, called procedures. In object-oriented programming, you envision program components as objects that are similar to concrete objects in the real world

• The C# language was developed as an object-oriented and component-oriented language

Page 38: 1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming

38

Chapter Summary

• To write a C# program that produces a line of console output, you must pass a literal string as a parameter to the System.Console.Out.WriteLine() method

• You can define a C# class or variable by using any name or identifier that begins with an underscore or a letter, that contains only letters or digits, and that is not a C# reserved keyword

• To create a C# program, you can use the Microsoft Visual Studio environment or any text editor

Page 39: 1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming

39

Chapter Summary

• After you write and save a program, you must compile the source code

• Program comments are nonexecuting statements that add to document a program or to disable statements

• As an alternative to using the command line, you can compile and write your program within the Visual Studio IDE

• When you need to repeatedly use a class from the same namespace, you can shorten the statements you type by using a clause that indicates a namespace where the class can be found