50
1 1 st Semester 2005 Module2 Module2 Basic C# Concept Basic C# Concept ออออออออ อออออออออออ Aphirak Jansang [email protected] http://www.cpe.ku.ac.th/~aphirak Computer Engineering Department Kasetsart University, Bangkok THAILAND

1 st Semester 2005 1 Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang [email protected] aphirak Computer Engineering

Embed Size (px)

Citation preview

11st Semester 2005

Module2 Module2 Basic C# ConceptBasic C# Concept

อภิ�รั�กษ์� จั�นทรั�สรั�างAphirak Jansang

[email protected]://www.cpe.ku.ac.th/~aphirak

Computer Engineering DepartmentKasetsart University, Bangkok THAILAND

21st Semester 2005

Outline C# Overview Variable and Constant Expression Statement

31st Semester 2005

C# Program Overview

C# is an object-oriented programming language.

Everything must be in some classclass..

A program is a set of class declarations.

แดง

ขาวด�าง

โฮ่�ง

ส�น�ข

class

C# OverviewC# Overview

41st Semester 2005

Very simple C# program example

Starting PointStarting Point

C# OverviewC# Overview

51st Semester 2005

Simple C# program structure

Your Class NameYour Class Name

Your declaration part Your declaration part

Your statementsYour statements

Your Namespace NameYour Namespace Name

C# OverviewC# Overview

61st Semester 2005

C# Keywords Example

11

22

33

Any keywords?

C# OverviewC# Overview

71st Semester 2005

More C# Keywords Example

C# OverviewC# Overview

81st Semester 2005

What is Objects?

In computer languagesIn computer languages“an object is called class”

Object = Properties + MethodsObject = Properties + Methods

C# OverviewC# Overview

91st Semester 2005

Example: Class declaration

Class Carclass car {

} Class Human

class human { {

static void main()

}}

C# OverviewC# Overview

101st Semester 2005

What is Namespace?

“Section of codeSection of code that is identified by a specific namespecific name”

Namespace example1Namespace example1

class Human

class Car

C# OverviewC# Overview

Namespace example2Namespace example2

class Car

111st Semester 2005

Example: namespace declaration

Namespace example1namespace example1 {

} Namespace example2 + Class car

namespace example2 {class car {}

}

C# OverviewC# Overview

121st Semester 2005

Naming Guidelines Letters, digits, underscores(_) First character letter Can be long (63 char) Reserved words (keywords) are not

allowed

*** Case Sensitive ****** Case Sensitive ***ExampleExample

KU56 ≠ kU56 ≠KU56 ≠ kU56 ≠ ku56ku56

C# OverviewC# Overview

131st Semester 2005

Built-in Namespace: System

ConsoleConsole class can be used for DisplayingDisplaying information on the screen

System.Console.Write(”HelloWorld!”);System.Console.Write(”HelloWorld!”);

System.Console.WriteLine(”HelloWorld!”);System.Console.WriteLine(”HelloWorld!”);

Retrieving Retrieving information from the user

String ch;String ch;

ch = System.Console.Readline();ch = System.Console.Readline();

C# OverviewC# Overview

141st Semester 2005

Example: Console class

C# OverviewC# Overview

151st Semester 2005

Summary C# Program Overview Namespace

Class Main()

C# OverviewC# Overview

161st Semester 2005

Practice: Try it by yourself Declare the following class/namespace

in C# syntax Class Movie Class HelloWorld Namespace KU65

Class Man Class Woman

C# OverviewC# Overview

171st Semester 2005

Reference Problem Calculate AREA of Rectangle!!!

Width

High

AREA = Width x HighAREA = Width x High

C# OverviewC# Overview

181st Semester 2005

Level1: Reference Problem

C# OverviewC# Overview

191st Semester 2005

Short break – 8 Minutes

201st Semester 2005

Outline C# Overview Variable and Constant Expression Statement

211st Semester 2005

Basic uses Declaration Part

Variable Constant

Variable & ConstantVariable & Constant

221st Semester 2005

What is Variable?

Variables are used to store “data.”

““They must be They must be declareddeclared before used” before used”

Variable & ConstantVariable & Constant

231st Semester 2005

C# Declaration Location

Declaration PartDeclaration PartHere Here

Variable & ConstantVariable & Constant

241st Semester 2005

C# Variable Declaration Syntax:

<data type> <name>;

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 & ConstantVariable & Constant

251st Semester 2005

C# Basic Data Type int

Integer (-2147483648 to 2147483647)

double Floating-point ( )

boolBoolean values, true and false

char a Unicode character (’A’ ’B’)

string string of Unicode characters (”StarsIII”)

Variable & ConstantVariable & Constant

261st Semester 2005

Basic uses Declaration Part

Variable Constant

Variable & ConstantVariable & Constant

271st Semester 2005

C# Constant Declaration Syntax:

const <data type> <name> = <value>;

Example:

const int radius = 15;const double area=1.5;const bool isokay=true;const string movie=”StarWarIII”;

Variable & ConstantVariable & Constant

281st Semester 2005

Example: Variable Declaration

Variable & ConstantVariable & Constant

291st Semester 2005

Level2: Reference Problem

Variable & ConstantVariable & Constant

301st Semester 2005

Short break – 5 Minutes

311st Semester 2005

Outline C# Overview Variable and Constant Expression Statement

321st Semester 2005

Expression in C# Arithmetic Expression Boolean Expression

ExpressionExpression

331st Semester 2005

Arithmetic Expression Operators

+ - * /% (remainder after division)

Example 11 + 5 16 11 / 2 5.5 11 % 2 1 5.0 % 2.2 0.6

ExpressionExpression

341st Semester 2005

Precedence rules for Arithmetic Operators

1. ( ) parentheses2. *, / , % 3. + –4. If equal precedence, left to right

ExampleExampleint Width,High;int Width,High;Width=10*5+(16 * 12)/5;Width=10*5+(16 * 12)/5;High= (16+5)+20%2;High= (16+5)+20%2;

351st Semester 2005

Expression in C# Arithmetic Expression Boolean Expression

ExpressionExpression

361st Semester 2005

Boolean Expression Operators

Comparison Equal == Not equal !=!= Less << Greater >> Less than or equal to <=<= Greater than or equal to >=>=

Boolean And &&&& Or ||||

ExpressionExpression

371st Semester 2005

Short break – 5 Minutes

381st Semester 2005

Outline C# Overview Variable and Constant Expression Statement

391st Semester 2005

C# Statement Location

StatementsStatementsHere Here

StatementStatement

401st Semester 2005

C# Statement Types Assignment Statement Input Statement Output Statement

StatementStatement

411st Semester 2005

Assignment Statement To "put""put" a value in the memory

space allocated to a variable Use the equal sign (=) equal sign (=) when makin

g assignments. Syntax:

<<variablevariable> = <> = <valuevalue>;>;

int Width,High;int Width,High;Width=10;Width=10;High=5;High=5;

int Width = 10;int Width = 10;int High = 5;int High = 5;

StatementStatement

421st Semester 2005

Example: Assignment Statement

StatementStatement

431st Semester 2005

C# Statement Types Assignment Statement Input Statement Output Statement

StatementStatement

441st Semester 2005

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

ExampleExamplestring yourname;string yourname;yourname = Console.ReadLine();yourname = Console.ReadLine();

StatementStatement

451st Semester 2005

Example: Input Statement

Ex1:Ex1:string myname;

myname = Console.ReadLine();

Ex2:Ex2:int Width,High;int Width,High;

string temp1;string temp1;

temp1 = Console.ReadLine();temp1 = Console.ReadLine();

Width = int.Parse(temp1);Width = int.Parse(temp1);

temp1 = Console.ReadLine();temp1 = Console.ReadLine();

Width = int.Parse(temp1);Width = int.Parse(temp1);

StatementStatement

461st Semester 2005

Level3: Reference Problem

StatementStatement

471st Semester 2005

C# Statement Types Assignment Statement Input Statement Output Statement

StatementStatement

481st Semester 2005

Output Statement Console.WriteLine(VariableName) Display variable value in some position

of string

Output Formatting

Example1Example1Console.WriteLine(”Hello {0}, {1}”, ps0, ps1);Console.WriteLine(”Hello {0}, {1}”, ps0, ps1);

Example2Example2double salary=12000;double salary=12000;Console.WriteLine("My salary is {0:f}.", salary);Console.WriteLine("My salary is {0:f}.", salary);

More information about formattingMore information about formatting*http*http://://msdnmsdn..microsoftmicrosoft..comcom//librarylibrary//enen--usus//csrefcsref//htmlhtml//vclrfFormattingNumericResultsTablevclrfFormattingNumericResultsTable..aspasp

StatementStatement

491st Semester 2005

Level4: Reference Problem

StatementStatement

501st Semester 2005

Summary C# Overview Variable and Constant Expression Statement

SummarySummary