C# — Console Application

Slides:



Advertisements
Similar presentations
2. C# Language Fundamentals
Advertisements

C# Language Report By Trevor Adams. Language History Developed by Microsoft Developed by Microsoft Principal Software Architect Principal Software Architect.
Programming Dr. Abraham Most slides are from your textbook.
Getting Started with C# 1 SWE 344 Internet Protocols & Client Server Programming.
C#: CS1 Should We Switch ? WG2.4 AUGUST 7,2003 IRA POHL  ALGOL – PL/1 – Pascal – C / C++ / Java / C#  What have we learned from CS1 choices  Neat Features.
Distributed Systems Tutorial 1 - Getting Started with Visual C#.NET.
C# Programming: From Problem Analysis to Program Design1 2 Your First C# Program C# Programming: From Problem Analysis to Program Design 2 nd Edition.
Introduction to Computing and Programming
C# Programming: From Problem Analysis to Program Design1 2 Your First C# Program.
C# Tutorial From C++ to C#. Some useful links Msdn C# us/library/kx37x362.aspxhttp://msdn.microsoft.com/en- us/library/kx37x362.aspx.
Computer and Programming File I/O File Input/Output Author: Chaiporn Jaikaeo, Jittat Fakcharoenphol Edited by Supaporn Erjongmanee Lecture 13.
Introduction to Classes and Objects (Through Ch 5) Dr. John P. Abraham Professor UTPA.
1. 2 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Decisions { class.
C# B 1 CSC 298 Writing a C# application. C# B 2 A first C# application // Display Hello, world on the screen public class HelloWorld { public static void.
3. Declaring Value-Type Variables
INPUT/OUTPUT STATEMENT ADDITION SLIDES. Console.ReadLine() – Use to get the input (String) from user Convert string to other data type – int.Parse() 
CSCI 3328 Object Oriented Programming in C# Chapter 3: Introduction to Classes and Objects UTPA – Fall
Computer Programming 1.  Editor Console Application Notepad Notepad++ Edit plus etc.  Compiler & Interpreter Microsoft.NET Framework  Microsoft visual.
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.
 Simple C# program  Console applications input and output text in a console window, which in Windows XP and Windows Vista is known as the Command Prompt.
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Iteration (Loop) partI Thanachat Thanomkulabut. Consider the following program! using System; Namespace SimPleExample { class SimPleC { class SimPleC.
Chapter 2: Using Data.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
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.
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Section 3 - Arrays and Methods. Arrays Array: collection of group of data variables of same type, sharing the same name for convenience - Easy to search.
BİL527 – Bilgisayar Programlama I Functions 1. Contents Functions Delegates 2.
CSC 298 Streams and files.
Chapter One Lesson Three DATA TYPES ©
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.
INPUT/OUTPUT STATEMENT ADDITION SLIDES. Console.ReadLine() – Use to get the input (String) from user Convert string to other data type – int.Parse() 
Session 02 Module 3: Statements and Operators Module 4: Programming constructs Module 5: Arrays.
Computer Programs CS 1400 Dennis A. Fairclough Version 1.1 CS 1400 Dennis A. Fairclough Version 1.1.
Lecture 2 Review of 1301 and C# Richard Gesick. Common data types int, long, double, float, char, bool, string.
1 Statements © University of Linz, Institute for System Software, 2004 published under the Microsoft Curriculum License.
C# Part 1 Intro to C#. Background Designed to be simple, modern, general- purpose, OO, programming language Strong type checking, array bounds checking,
C# Programming: From Problem Analysis to Program Design
C# Basic Syntax, Visual Studio, Console Input / Output
C# Basic Syntax, Visual Studio, Console Input / Output
Basic Introduction to C#
Lecture 30a C# as Conventional Prog Lan
Using the Console.
Repeating Code Multiple Times
BİL527 – Bilgisayar Programlama I
C# Arrays.
Computing with C# and the .NET Framework
Case Study 2 – Marking a Multiple-choice Test
Lesson 2: Program design process & Intro to code
Dr Shahriar Bijani Winter 2017
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I UTPA – Fall 2012 This set of slides is revised from lecture slides.
Advanced Programming Lecture 02: Introduction to C# Apps
Variables, Loops, Decision Statements, etc
An Introduction to Java – Part I, language basics
ניתוח מערכות מידע תכנות ב C#
CSCI 3328 Object Oriented Programming in C# Chapter 3: Introduction to Classes and Objects UTPA – Fall 2012 This set of slides is revised from lecture.
Thanachat Thanomkulabut
Chapter 3 – Introduction to C# Programming
Lecture 1 Review of 1301/1321 CSE /26/2018.
Module 2 Variables, Assignment, and Data Types
CSE Module 1 A Programming Primer
C# Programming: From Problem Analysis to Program Design
When an argument to method is an entire array or an individual element of reference type, the called method receives a copy of reference. However an argument.
Introduction to C#.net PROF. S. LAKSHMANAN,
Introduction to java Part I By Shenglan Zhang.
Presentation transcript:

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

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

Do the following to create the project

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]

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.

We should replace Class1 with a user-friendly name

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

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");

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

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

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

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

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

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

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: 0123456789 Your ID is: 0123456789 Press any key to continue

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

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};

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); }

// 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

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}};

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

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

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

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); }

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

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); }

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

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 + " "); }