1 introduction to c# programming console applications no visual components only text output two...

18
1 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt -Used in Windows 95/98/ME Command prompt -Used in later versions of Windows

Upload: carmel-harriet-strickland

Post on 31-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt - Used in Windows 95/98/ME Command

1

Introduction to C# Programming

Console applicationsNo visual componentsOnly text outputTwo types

• MS-DOS prompt -Used in Windows 95/98/ME

• Command prompt -Used in later versions of Windows

Page 2: 1 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt - Used in Windows 95/98/ME Command

2

Namespaces

Group related C# features into a categories

Contain code that can be reused .NET framework library contains many

namespaces Must be referenced in order to be used Example: Console feature is in

namespace System

Page 3: 1 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt - Used in Windows 95/98/ME Command

3

Methods

Building blocks of C# programs Every program is a class! The Main method

Each console or windows application must have exactly one

Page 4: 1 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt - Used in Windows 95/98/ME Command

4

Simple Program: Printing a Line of Text

Visual Studio .NET-generated console application.

Rename

Complete the code

Page 5: 1 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt - Used in Windows 95/98/ME Command
Page 6: 1 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt - Used in Windows 95/98/ME Command

6

Simple Program: Output

Execution of the Welcome1 program.

Page 7: 1 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt - Used in Windows 95/98/ME Command

Welcome2.cs

Page 8: 1 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt - Used in Windows 95/98/ME Command

8

Some common escape sequences

Escape sequence Description \n Newline. Position the screen cursor to the beginning of the

next line. \t Horizontal tab. Move the screen cursor to the next tab stop. \r Carriage return. Position the screen cursor to the beginning

of the current line; do not advance to the next line. Any characters output after the carriage return overwrite the previous characters output on that line.

\\ Backslash. Used to print a backslash character. \" Double quote. Used to print a double quote (") character.

Page 9: 1 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt - Used in Windows 95/98/ME Command

9

Displaying output

With C# Windows applications• Forms with several output types• Contain Graphical User Interfaces (GUIs)

Graphical User Interface• GUIs are used to make it easier to get data from

the user as well as display data to the user

• Message boxes• Within the System.Windows.Forms namespace• Used to prompt or display information to the user

Page 10: 1 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt - Used in Windows 95/98/ME Command

Welcome4.cs

The System.Windows.Forms namespace allows the programmer to use the MessageBox class.

This will display the contents in a message box as opposed to the console.

Page 11: 1 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt - Used in Windows 95/98/ME Command

11

Adding a reference to an assembly in Visual Studio .NET

Add Reference dialogue

http://www.codeproject.com/KB/cs/DisplayConfirmation_.aspx

Page 12: 1 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt - Used in Windows 95/98/ME Command

12

Adding a reference to an assembly in Visual Studio .NET

References folder

Solution Explorer

System.Windows.Forms reference

Page 13: 1 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt - Used in Windows 95/98/ME Command

13

Dialog displayed by calling MessageBox.Show.

OK button allows the user to dismiss the dialog.

Dialog is automatically sized to accommodate its contents.

Close box

Page 14: 1 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt - Used in Windows 95/98/ME Command

14

Getting input

Primitive data types built into C#• string, int, double, char, long …15 types

Console.ReadLine()Used to get a value from the user input

Int32.Parse()Used to convert a string argument to an

integerAllows math to be performed once the

string is converted

Page 15: 1 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt - Used in Windows 95/98/ME Command

1 // Addition.cs

2 // An addition program.

3

4 using System;

5

6 class Addition

7 {

8 static void Main( string[] args )

9 {

10 string firstNumber, // first string entered by user

11 secondNumber; // second string entered by user

12

13 int number1, // first number to add

14 number2, // second number to add

15 sum; // sum of number1 and number2

16

17 // prompt for and read first number from user as string

18 Console.Write( "Please enter the first integer: " );

19 firstNumber = Console.ReadLine();

Page 16: 1 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt - Used in Windows 95/98/ME Command

21 // read second number from user as string

22 Console.Write( "\nPlease enter the second integer: " );

23 secondNumber = Console.ReadLine();

24

25 // convert numbers from type string to type int

26 number1 = Int32.Parse( firstNumber );

27 number2 = Int32.Parse( secondNumber );

28

29 // add numbers

30 sum = number1 + number2;

31 // display results Console.WriteLine( "\nThe sum is {0}.", sum );

34 Console.Write("Press ENTER to continue");

35 Console.ReadLine();

36 } // end method Main

37 } // end class Addition

Page 17: 1 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt - Used in Windows 95/98/ME Command

17

Combining steps

21 // read second number from user as string

22 Console.Write( "\nPlease enter the second integer: " );

23 secondNumer = Int32.Parse( Console.ReadLine() );

Page 18: 1 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt - Used in Windows 95/98/ME Command

18

Should know!

Memory Concepts Arithmetic Decision Making: Equality and

Relational Operators