2124C 03id 29202 ppt

background image

Module 3: Using

Value-Type

Variables

background image

Overview

Common Type System

Naming Variables

Using Built-in Data Types

Creating User-Defined Data Types

Converting Data Types

background image

Common Type System

Overview of CTS

Comparing Value and Reference Types

Comparing Built-in and User-Defined

Value Types

Simple Types

background image

Overview of CTS

CTS supports both value and reference

types

 

Reference Type

Reference Type

Type

Type

Value Type

Value Type

background image

Comparing Value and Reference

Types

Value types:

Directly contain

their data

Each has its

own copy of

data

Operations on

one cannot

affect another

Reference types:

Store references to

their data (known

as objects)

Two reference

variables can

reference same

object

Operations on one

can affect another

background image

Comparing Built-in and User-Defined

Value Types

Examples of

built-in value

types:

int

float

Examples of user-

defined value

types:

enum

struct

User-Defined

User-Defined

Value Types

Value Types

Built-in Type

Built-in Type

background image

Simple Types

Identified through reserved keywords

int // Reserved keyword

- or -

System.Int32

background image

Naming Variables

Rules and Recommendations for Naming

Variables

C# Keywords

Quiz: Can You Spot Disallowed Variable

Names?

background image

Rules and Recommendations for

Naming Variables

Rules

Use letters, the

underscore, and digits

Recommendations

Avoid using all

uppercase letters

Avoid starting with

an underscore

Avoid using

abbreviations

Use PascalCasing

naming in multiple-

word names

different
Different

different
Different

Answer42
42Answer

Answer42
42Answer

BADSTYLE
_poorstyle
BestStyle

BADSTYLE
_poorstyle
BestStyle

Msg
Message

Msg
Message

background image

C# Keywords

Keywords are reserved identifiers

Do not use keywords as variable

names

Results in a compile-time error

Avoid using keywords by changing

their case sensitivity

abstract, base, bool, default, if, finally

abstract, base, bool, default, if, finally

int INT; // Poor style

int INT; // Poor style

background image

Quiz: Can You Spot the Disallowed

Variable Names?

char $diskPrice;

char $diskPrice;

char middleInitial;

char middleInitial;

int 12count;

int 12count;

float this;

float this;

2

2

2

2

3

3

3

3

4

4

4

4

1

1

1

1

int __identifier;

int __identifier;

5

5

5

5

background image

Using Built-in Data Types

Declaring Local Variables

Assigning Values to Variables

Compound Assignment

Common Operators

Increment and Decrement

Operator Precedence

background image

Declaring Local Variables

Usually declared by data type and

variable name:

Possible to declare multiple variables in

one declaration:

--or--

int itemCount;

int itemCount;

int itemCount, employeeNumber;

int itemCount, employeeNumber;

int itemCount,

employeeNumber;

int itemCount,

employeeNumber;

background image

Assigning Values to Variables

Assign values to variables that are

already declared:

Initialize a variable when you declare it:

You can also initialize character values:

int employeeNumber;

employeeNumber = 23;

int employeeNumber;

employeeNumber = 23;

int employeeNumber = 23;

int employeeNumber = 23;

char middleInitial = 'J';

char middleInitial = 'J';

background image

Compound Assignment

Adding a value to a variable is very

common

There is a convenient shorthand

This shorthand works for all arithmetic

operators

itemCount = itemCount + 40;

itemCount = itemCount + 40;

itemCount += 40;

itemCount += 40;

itemCount -= 24;

itemCount -= 24;

background image

Common Operators

Common Operators

Common Operators

Common Operators

Common Operators

Equality operators

Relational operators

Conditional

operators

Increment operator

Decrement operator

Arithmetic

operators

Assignment

operators

Example

Example

== !=

< > <= >= is

&& || ?:

++

- -

+ - * / %

= *= /= %= +=

-= <<= >>=

&= ^= |=

background image

Increment and Decrement

Changing a value by one is very

common

There is a convenient shorthand

This shorthand exists in two forms

itemCount += 1;
itemCount -= 1;

itemCount += 1;
itemCount -= 1;

itemCount++;
itemCount--;

itemCount++;
itemCount--;

++itemCount;
--itemCount;

++itemCount;
--itemCount;

background image

Operator Precedence

Operator Precedence and

Associativity

Except for assignment operators, all

binary operators are left-associative

Assignment operators and conditional

operators are right-associative

background image

Creating User-Defined Data Types

Enumeration Types

Structure Types

background image

Enumeration Types

Defining an Enumeration Type

Using an Enumeration Type

Displaying an Enumeration Variable

enum Color { Red, Green, Blue }

enum Color { Red, Green, Blue }

Color colorPalette = Color.Red;

Color colorPalette = Color.Red;

Console.WriteLine(“{0}”, colorPalette); // Displays Red

Console.WriteLine(“{0}”, colorPalette); // Displays Red

background image

Structure Types

Defining a Structure Type

Using a Structure Type

Employee companyEmployee;
companyEmployee.firstName = "Joe";
companyEmployee.age = 23;

Employee companyEmployee;
companyEmployee.firstName = "Joe";
companyEmployee.age = 23;

public struct Employee
{
public string firstName;
public int age;
}

public struct Employee
{
public string firstName;
public int age;
}

background image

Converting Data Types

Implicit Data Type Conversion

Explicit Data Type Conversion

background image

Implicit Data Type Conversion

To Convert int to long:

Implicit conversions cannot fail

May lose precision, but not magnitude

using System;
class Test
{

static void Main( )

{

int intValue = 123;
long longValue = intValue;
Console.WriteLine("(long) {0} = {1}", intValue,

longValue);

}

}

using System;
class Test
{

static void Main( )

{

int intValue = 123;
long longValue = intValue;
Console.WriteLine("(long) {0} = {1}", intValue,

longValue);

}

}

background image

Explicit Data Type Conversion

To do explicit conversions, use a cast

expression:

using System;
class Test
{

static void Main( )

{

long longValue = Int64.MaxValue;
int intValue = (int) longValue;
Console.WriteLine("(int) {0} = {1}", longValue,

intValue);

}

}

using System;
class Test
{

static void Main( )

{

long longValue = Int64.MaxValue;
int intValue = (int) longValue;
Console.WriteLine("(int) {0} = {1}", longValue,

intValue);

}

}

background image

Lab 3.1: Creating and Using Types

background image

Review

Common Type System

Naming Variables

Using Built-in Data Types

Creating User-Defined Data Types

Converting Data Types


Document Outline


Wyszukiwarka

Podobne podstrony:
2009 03 18 POZ 03id 26788 ppt
03id 4102 ppt
2124C 04id 29203 ppt
2124C 00id 29199 ppt
2124C 01id 29200 ppt
2009 03 18 POZ 03id 26788 ppt
03 Sejsmika04 plytkieid 4624 ppt
Choroby układu nerwowego ppt
10 Metody otrzymywania zwierzat transgenicznychid 10950 ppt
10 dźwigniaid 10541 ppt
03 Odświeżanie pamięci DRAMid 4244 ppt

więcej podobnych podstron