Post on 31-Dec-2015
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
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
3
Methods
Building blocks of C# programs Every program is a class! The Main method
Each console or windows application must have exactly one
4
Simple Program: Printing a Line of Text
Visual Studio .NET-generated console application.
Rename
Complete the code
6
Simple Program: Output
Execution of the Welcome1 program.
Welcome2.cs
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.
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
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.
11
Adding a reference to an assembly in Visual Studio .NET
Add Reference dialogue
http://www.codeproject.com/KB/cs/DisplayConfirmation_.aspx
12
Adding a reference to an assembly in Visual Studio .NET
References folder
Solution Explorer
System.Windows.Forms reference
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
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
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();
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
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() );
18
Should know!
Memory Concepts Arithmetic Decision Making: Equality and
Relational Operators