Presentation is loading. Please wait.

Presentation is loading. Please wait.

C# — Console Application

Similar presentations


Presentation on theme: "C# — Console Application"— Presentation transcript:

1 C# — Console Application
1. Create a Console Project 2. The “Hello World” program 3. Standard Input & Output 4. Arrays 5. Call By Value & Call By Reference 6. The foreach statement

2 1. Create a Console Project
Open MS Visual Studio .NET 2003 Then click on the New Project button

3 Do the following to create the project

4 The following program has been created
using System; namespace ConsoleApplication { /// <summary> /// Summary description for Class1. /// </summary> class Class1 /// The main entry point for /// the application. [STAThread]

5 static void Main(string[] args)
{ // // TODO: Add code to start // application here } Build Solution: to compile the program. Start or Start without Debugging: to run the program. There is no output if we run the program now.

6 We should replace Class1 with a user-friendly name

7 2. The Hello World Program
We add the following code for our first program: static void Main(string[] args) { // // TODO: Add code to start // application here Console.WriteLine( "Hello World"); } Run Hello World Press any key to continue

8 3. Standard Input & Output
Output: We have used the WriteLine() method to output “Hello World” in our first program. We can use the Write() method as follows Console.Write("Hello "); Console.WriteLine("World"); or Console.WriteLine("Hello World"); Console.Write("\n");

9 More examples: Example 1: Console.WriteLine("PGUI " ); Example 2: int year = 2004; Console.WriteLine("PGUI " + year); PGUI 2004 Example 3: double x = 55.77; Console.WriteLine("x = " + x); x = 55.77

10 Example 4: char c = 'x'; double d = 0.5; Console.WriteLine(c + " = " + d); x = 0.5 Example 5: string s = "Hello World"; Console.WriteLine(s); Hello World

11 Example 6: int x = 2004; double y = 0.5; Console.WriteLine("{0} {1}", x, y); Example 7: double y = 7.5; Console.WriteLine("{0, 10:C5}", y); currency 10 places 5 decimal places $

12 Input: the Read() method
Example: int i; Console.WriteLine("Enter string:"); while ((i = Console.Read()) != -1) Console.WriteLine( (char)i + ": " + i); The Read() method reads the next character from the standard input stream and returns its American Standard Code

13 Enter string: az a: 97 z: 122 : 13 :10 ^Z Press any key to continue a: 97, ..., z: 122 carriage return: 13 newline: 10

14 The ReadLine() method Example 1: double price; string inputString; Console.Write("Enter price: "); inputString = Console.ReadLine(); price = double.Parse(inputString ); Console.Write("price = "); Console.WriteLine("{0:C}", price); Enter price: 55 price = $55.00 Press any key to continue

15 Example 2: int ID; string inputString; Console.Write("Enter your ID: "); inputString = Console.ReadLine(); ID = int.Parse(inputString ); Console.Write("Your ID is: "); Console.WriteLine(ID); Enter your ID: Your ID is: Press any key to continue

16 Simple native types: bool, int, double, char, byte Integral simple types: short, int, long, ushort, uint, ulong, Floating-point simple types: float, double, decimal 16 bytes Fixed precision up to 28 or 29 digits

17 4. Arrays Example 1: one-dimensional array int[] day = new int[2];
We can declare and initialise as follows int[] day = new int[2] {23, 24}; or int[] day = {23, 24};

18 Example 2: use array as an argument of a user-defined method
using System; namespace ConsoleApplication { class Welcome static void Main(string[] args) int[] a = {3, 5}; int sum = sumArray(a); Console.WriteLine(sum); }

19 // Implementation of the
// sumArray() method static int sumArray(int[] arr) { int sum = 0; int i; for (i=0; i<arr.Length; i++) sum += a[i]; return sum; } } // end class Welcome } // end namespace 8 Press any key to continue

20 Example 3: multi-dimensional array
int[] day; // 1D array double[,] matrix; // 2D array double[,,] plane; // 3D array int[,] data = new int[3,5]; int[,] a = {{1,2}, {3,4}, {5,6}}; int[,] b = {{1,2,3}, {4,5,6}}; int[,] c = {{1,2,3,4,5,6}};

21 Example 4: class Welcome { public static void Main() int[,] data = new int[3,5]; // 3 rows 5 columns Console.WriteLine( "Number of elements = " + data.Length); for (int i = 0; i < data.GetLength(0); i++)

22 { Console.WriteLine( "Row " + i + ": "); for (int j = 0; j < data.GetLength(1); j++) data[i,j] = i * j; Console.Write( data[i,j] + ", "); } Console.WriteLine();

23 Number of Elements = 15 Row 0: 0, 0, 0, 0, 0, Row 1: 0, 1, 2, 3, 4, Row 2: 0, 2, 4, 6, 8, Press any key to continue

24 5. Call By Value & Call By Reference
Example 1: Call By Value class FailedSwap { public static void Main() int numOne = 1, numTwo = 2; Swap(numOne, numTwo); Console.WriteLine( "numOne = " + numOne); "numTwo = " + numTwo); }

25 public static void Swap(int x, int y) { int temp; temp = x; x = y; y = temp; } numOne = 1 numTwo = 2 Press any key to continue

26 Example 2: Call By Reference
class SwapTwo { public static void Main() int numOne = 1, numTwo = 2; Swap(ref numOne, ref numTwo); Console.WriteLine( "numOne = " + numOne); "numTwo = " + numTwo); }

27 public static void Swap(ref int x, ref int y) { int temp; temp = x; x = y; y = temp; } numOne = 2 numTwo = 1 Press any key to continue

28 6. The foreach statement foreach(type variableName in arrayName)
do_statement Example 1: static void WriteArray(int[] a) { foreach(int element in a) Console.Write(element + " "); }


Download ppt "C# — Console Application"

Similar presentations


Ads by Google