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 C# Program Overview C# is an object-oriented programming language. class. Everything must be in some class. A program is a set of class declarations. แดง ขาว ด่าง โฮ่ง สุนัข class C# Overview
1 st Semester Very simple C# program example Starting Point C# Overview
1 st Semester Simple C# program structure Your Class Name Your declaration part Your statements Your Namespace Name C# Overview
1 st Semester C# Keywords Example Any keywords? C# Overview
1 st Semester More C# Keywords Example C# Overview
1 st Semester What is Objects? In computer languages “an object is called class” Object = Properties + Methods C# Overview
1 st Semester Example: Class declaration Class Car class car { } Class Human class human { { static void main() } } C# Overview
1 st Semester What is Namespace? Section of code specific name “Section of code that is identified by a specific name” Namespace example1 class Human class Car C# Overview Namespace example2 class Car
1 st Semester Example: namespace declaration Namespace example1 namespace example1 { } Namespace example2 + Class car namespace example2 { class car { } } 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 KU56 ≠ kU56 ≠ku56 KU56 ≠ kU56 ≠ ku56 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 Example: Console class C# Overview
1 st Semester Summary C# Program Overview Namespace Class Main() C# Overview
1 st Semester Practice: Try it by yourself Declare the following class/namespace in C# syntax Class Movie Class HelloWorld Namespace KU65 Class Man Class Woman C# Overview
1 st Semester Reference Problem Calculate AREA of Rectangle!!! Width High AREA = Width x High C# Overview
1 st Semester Level1: Reference Problem C# Overview
1 st Semester Short break – 8 Minutes
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 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 Example: Variable Declaration Variable & Constant
1 st Semester Level2: Reference Problem Variable & Constant
1 st Semester Short break – 5 Minutes
1 st Semester Outline C# Overview Variable and Constant Expression Statement
1 st Semester Expression in C# Arithmetic Expression Boolean ExpressionExpression
1 st Semester Arithmetic Expression 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 Expression in C# Arithmetic Expression Boolean ExpressionExpression
1 st Semester Boolean Expression Operators Comparison = Equal = != Not equal != < Less < > Greater > <= Less than or equal to <= >= Greater than or equal to >= Boolean && And && || Or ||Expression
1 st Semester Short break – 5 Minutes
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 Level3: Reference Problem 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 Level4: Reference Problem Statement
1 st Semester Summary C# Overview Variable and Constant Expression StatementSummary