Professional Documents
Culture Documents
1
Introduction to C# Applications
Take some more tea, the March Hare said to Alice, very earnestly. Ive had nothing yet, Alice replied in an offended tone, so I cant take more. You mean you cant take less, said the Hatter: its very easy to take more than nothing.
Lewis Carroll
Whats in a name? That which we call a rose by any other name would smell as sweet.
William Shakespeare
OBJECTIVES
In this chapter you will learn: To write simple C# applications using code rather than visual programming. To write statements that input and output data to the screen. To declare and use data of various types. To store and retrieve data from memory. To use arithmetic operators. To determine the order in which operators are applied. To write decision-making statements. To use relational and equality operators. To use message dialogs to display messages.
Lecturer: Snr.Lec.Eren KUREN
When faced with a decision, I always ask, What would be the most fun?
Peggy Walker
Outline
Introduction A Simple C# Application: Displaying a Line of Text Creating a Simple Application in Visual C# Express Modifying Your Simple C# Application Formatting Text with Console.Write and
Console.WriteLine
1.9 Decision Making: Equality and Relational Operators 1.10 (Optional) Software Engineering Case Study: Examining the ATM Requirements Document
1.1 Introduction
Console applications input and output text in a console window, which in Windows XP and Windows Vista is known as the Command Prompt.
11
10
12
13
15
14
16
Contextual Keywords add equals into partial var alias from join remove where ascending get let select yield by global on set descending group orderby value
The contextual keywords in Fig. 1.2 can be used as identifiers outside the contexts in which they are keywords, but for clarity this is not recommended. Lecturer: Snr.Lec. Eren KUREN
17
19
Identifiers may also be preceded by the @ character. This indicates that a word should be interpreted as an identifier, even if it is a keyword (e.g. @int).
Indent the entire body of each class declaration one level of indentation between the left and right braces that delimit the body of the class. This format emphasizes the class declarations structure and makes it easier to read. You can let the IDE format your code by selecting Edit > Advanced > Format Document.
18
20
21
23
22
24
25
27
Fig. 1.3 | Creating a Console Application with the New Project dialog.
Lectuer: Snr.Lec.Eren KUREN Lectuer: Snr.Lec.Eren KUREN
26
28
29
31
30
32
33
35
34
36
Type Welcome1.cs here to rename the file Tool tip describes highlighted member
Highlighted member
Fig. 1.7 | IntelliSense feature of Visual C# Express. Fig. 1.6 | Renaming the program file in the Properties window.
Lectuer: Snr.Lec.Eren KUREN Lectuer: Snr.Lec.Eren KUREN
37
39
Figure 1.10 shows the results of executing this application, displayed in a console (Command Prompt) window.
Up arrow
Up and down arrows allow you to scroll through overloaded versions of the method.
Lectuer: Snr.Lec.Eren KUREN Lectuer: Snr.Lec.Eren KUREN
38
40
Console window
Fig. 1.10 | Executing the application shown in Fig. 1.1. Fig. 1.9 | Save Project dialog.
Lectuer: Snr.Lec.Eren KUREN Lectuer: Snr.Lec.Eren KUREN
10
41
43
Applications output
Fig. 1.12 | Executing the application shown in Fig. 1.1 from a Command Prompt window.
Lectuer: Snr.Lec.Eren KUREN Lectuer: Snr.Lec.Eren KUREN
42
44
11
45
47
Error description(s)
46
48
Outline
Welcome2.cs
The Write method does not move the cursor to a new line after displaying its argument.
12
49
A single statement can display multiple lines by using newline characters. Like space characters and tab characters, newline characters are whitespace characters. The application of Fig. 1.15 outputs four lines of text, using newline characters to indicate when to begin each new line.
1 2 3 4 5 6 // Fig. 1.15: Welcome3.cs // Displaying multiple lines with a single statement. using System; public class Welcome3 {
Outline
51
Welcome1.cs
7 // Main method begins execution of C# application 8 public static void Main( string[] args ) 9 { 10 Console.WriteLine( "Welcome\nto\nC#\nProgramming!" ); 11 } // end Main 12 } // end class Welcome3 Welcome to C# Programming!
\\ \"
Fig. 1.16 | Some common escape sequences. Fig. 1.15 | Displaying multiple lines with a single statement.
Lecturer: Snr.Lec.Eren KUREN Lecturer: Snr.Lec. Eren KUREN
50
52
Outline
Welcome4.cs
6 { 7 // Main method begins execution of C# application 8 public static void Main( string[] args ) 9 { 10 Console.WriteLine( "{0}\n{1}", "Welcome to", "C# Programming!" ); 11 } // end Main 12 } // end class Welcome4 Welcome to C# Programming!
Method WriteLines first argument is a format string that may consist of fixed text and format items.
13
53
55
54
56
Outline
(1 of 2 )
Console.ReadLine() reads the data entered by the user, and Convert.ToInt32 converts the value into an integer.
Fig. 1.18 | Displaying the sum of two numbers input from the keyboard. (Part 1 of 2).
14
57
59
Outline
Addition.cs
18 19 20 21 22 Console.Write( "Enter second integer: " ); // prompt user // read second number from user number2 = Convert.ToInt32( Console.ReadLine() ); sum = number1 + number2; // add numbers
(2 of 2 )
23 24 Console.WriteLine( "Sum is {0}", sum ); // display sum 25 } // end Main 26 } // end class Addition Enter first integer: 45 Enter second integer: 72 Sum is 117
Fig. 1.18 | Displaying the sum of two numbers input from the keyboard. (Part 2 of 2).
58
60
15
61
63
62
64
Fig. 1.19 | Memory location showing the name and value of variable number1.
16
65
67
Fig. 1.20 | Memory locations after storing values for number1 and number2.
66
68
1.8 Arithmetic
The arithmetic operators are summarized in Fig. 1.22.
C# operation
Addition Subtraction Multiplication
C# expression
f + 7 p - c b * m
m
x or x y y
Division Remainder
/ %
x y or
x / y v % u
r mod s
Fig. 1.22 | Arithmetic operators. Fig. 1.21 | Memory locations after calculating and storing the sum of number1 and number2.
17
69
71
The multiplication, remainder and division operations are evaluated first in left-to-right order (i.e., they associate from left to right), because they have higher precedence than addition and subtraction. The addition and subtraction operations are evaluated next. These operations are also applied from left to right.
Lectuer: Snr.Lec.Eren KUREN
70
72
18
73
75
A condition is an expression that can be either true or false. Conditions can be formed using the equality operators (== and !=) and relational operators (>, <, >= and <=) summarized in Fig. 1.25.
Standard algebraic C# equality equality and or relational Sample C# relational operators operator condition
Equality operators = == != x == y x != y x is equal to y x is not equal to y
Meaning of C# condition
74
76
Standard algebraic C# equality equality and or relational Sample C# relational operators operator condition
Relational operators > < > < >= <= x > y x < y x >= y x <= y
Meaning of C# condition
x is greater than y x is less than y x is greater than or equal to y x is less than or equal to y
19
77
79
Outline
Comparison.cs
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 // prompt user and read second number Console.Write( "Enter second integer: " ); number2 = Convert.ToInt32( Console.ReadLine() ); if ( number1 == number2 ) Console.WriteLine( "{0} == {1}", number1, number2 ); if ( number1 != number2 ) Console.WriteLine( "{0} != {1}", number1, number2 ); if ( number1 < number2 ) Console.WriteLine( "{0} < {1}", number1, number2 ); if ( number1 > number2 ) Console.WriteLine( "{0} > {1}", number1, number2 ); if ( number1 <= number2 ) Console.WriteLine( "{0} <= {1}", number1, number2 );
(2 of 3 )
Fig. 1.26 | Comparing integers using if statements, equality operators and relational operators. (Part 2 of 3).
Lecturer: Snr.Lec.Eren KUREN
78
80
Outline
Outline
36 37 38 if ( number1 >= number2 ) Console.WriteLine( "{0} >= {1}", number1, number2 );
Figure 1.26 uses six if statements to compare two integers entered by the user.
Comparison.cs
1 2 3 // Fig. 1.26: Comparison.cs // Comparing integers using if statements, equality operators, // and relational operators.
39 } // end Main 40 } // end class Comparison Enter Enter 42 == 42 <= 42 >= first integer: 42 second integer: 42 42 42 42
Comparison.cs (3 of 3 )
(1 of 3 )
4 using System; 5 6 public class Comparison 7 { 8 // Main method begins execution of C# application 9 public static void Main( string[] args ) 10 { 11 int number1; // declare first number to compare 12 int number2; // declare second number to compare 13 14 15 16 17 // prompt user and read first number Console.Write( "Enter first integer: " ); number1 = Convert.ToInt32( Console.ReadLine() );
Enter first integer: 1000 Enter second integer: 2000 1000 != 2000 1000 < 2000 1000 <= 2000
Fig. 1.26 | Comparing integers using if statements, equality operators and relational operators. (Part 1 of 3).
Enter first integer: 2000 Enter second integer: 1000 2000 != 1000 2000 > 1000 2000 >= 1000
Fig. 1.26 | Comparing integers using if statements, equality operators and relational operators. (Part 3 of 3).
Lecturer: Snr.Lec.Eren KUREN Lecturer: Snr.Lec.Eren KUREN
20
81
83
82
84
21
85
87
86
88
Screen
Cash Dispenser
22
89
91
90
92
23
93
95
Fig. 1.31 | Use case diagram for the ATM system from the users perspective.
94
24