1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering Department Kasetsart University, Bangkok THAILAND
1 st Semester Outline C# Overview Variable and Constant Expression Statement
1 st Semester Simple C# program structure Your Class Name Your declaration part Your statements Your Namespace Name C# Overview
1 st Semester Naming Guidelines Letters, digits, underscores(_) First character letter Can be long (63 char) Reserved words (keywords) are not allowed *** Case Sensitive *** Example KU65 ≠ kU65 ≠ku65 KU65 ≠ kU65 ≠ ku65 C# Overview
1 st Semester Built-in Namespace: System Console Console class can be used for Displaying Displaying information on the screenSystem.Console.Write(”HelloWorld!”);System.Console.WriteLine(”HelloWorld!”); Retrieving Retrieving information from the user String ch; ch = System.Console.Readline(); C# Overview
1 st Semester How to Comment Code in C# 1 2
1 st Semester Summary C# Program Overview Namespace Class Main() C# Overview
1 st Semester Outline C# Overview Variable and Constant Expression Statement
1 st Semester Basic uses Declaration Part Variable Constant Variable & Constant
1 st Semester What is Variable? Variables are used to store “data.” “They must be declared before used” Variable & Constant
1 st Semester C# Declaration Location Declaration Part Here Variable & Constant
1 st Semester C# Variable Declaration Syntax: ; Example: We can also assign its initial value. E.g., int radius; double area; int a,b,c; bool isokay; int k = 200; bool done = false; Variable & Constant
1 st Semester C# Basic Data Type int Integer ( to ) double Floating-point ( ) bool Boolean values, true and false char a Unicode character (’A’ ’B’) string string of Unicode characters (”StarsIII”) Variable & Constant
1 st Semester Example: Variable Declaration Variable & Constant
1 st Semester Basic uses Declaration Part Variable Constant Variable & Constant
1 st Semester C# Constant Declaration Syntax: const = ; Example: const int radius = 15; const double area=1.5; const bool isokay=true; const string movie=”StarWarIII”; Variable & Constant
1 st Semester Outline C# Overview Variable and Constant Expression Statement
1 st Semester Arithmetic Expression in C# Operators + - * / % (remainder after division) Example / 2 % 2 % 2.2 0.6Expression
1 st Semester Precedence rules for Arithmetic Operators 1.( ) parentheses 2.*, /, % 3.+ – 4.If equal precedence, left to right Example int Width,High; Width=10*5+(16 * 12)/5; High= (16+5)+20%2;
1 st Semester Outline C# Overview Variable and Constant Expression Statement
1 st Semester C# Statement LocationStatementsHereStatement
1 st Semester C# Statement Types Assignment Statement Input Statement Output StatementStatement
1 st Semester Assignment Statement "put" To "put" a value in the memory space allocated to a variable equal sign (=) Use the equal sign (=) when making assignments. Syntax: = ; = ; int Width,High; Width=10;High=5; int Width = 10; int High = 5; Statement
1 st Semester Example: Assignment StatementStatement
1 st Semester C# Statement Types Assignment Statement Input Statement Output StatementStatement
1 st Semester Input Statement Console.ReadLine() Return string Use to get the input from user Convert string to other data type int.Parse() Convert string to integer double.Parse() Convert string to double Example string yourname; yourname = Console.ReadLine(); Statement
1 st Semester Example: Input Statement Ex1: string myname; myname = Console.ReadLine(); Ex2: int Width,High; string temp1; temp1 = Console.ReadLine(); Width = int.Parse(temp1); temp1 = Console.ReadLine(); Width = int.Parse(temp1); Statement
1 st Semester C# Statement Types Assignment Statement Input Statement Output StatementStatement
1 st Semester Output Statement Console.WriteLine(VariableName) Display variable value in some position of string Output Formatting Example1 Console.WriteLine(”Hello {0}, {1}”, ps0, ps1); Example2 double salary=12000; Console.WriteLine("My salary is {0:f}.", salary); More information about formatting * Statement
1 st Semester Summary C# Overview Variable and Constant Expression Statement