Вы находитесь на странице: 1из 25

Software Development Training Program

Zeeshan Hanif

Cloud Computing - 101 Lecture 2


Zeeshan Hanif zee81zee@yahoo.com zee81shan@hotmail.com

First C# Program
using System; public class First { public static void Main(string[] args) { Console.WriteLine(I belong to Operation Badar); } }

Parts of a C# Program

Class Name
public class First

Main method
public static void Main(string[] args)

Statements
Console.WriteLine(I belong to Operation Badar);

Bits and Bytes


Bit is the smallest unit of storage Bit is a digit that can have only two values: 0 or 1 A byte is made up of 8 bits
1 0 0 1 1 1 0 1
8 Bits

1 Byte

Memory allocation
10011001 11111001 01110101 10010101 Main Memory 11000001

11000101
01001110 10111001

Types of Data: Datatypes

Data types are very important in every language for storing and manipulating data Different kinds of data exist All have different features hence difference requirements Nature of data determines its type

Datatypes and size in memory

Integer Types
Size bits/bytes 8 bits / 1 byte Signed/ Unsigned signed Type in System SByte -128 to 127 Range

C# Keyword sbyte

byte short ushort int


uint long ulong

8 bits / 1 byte 16 bits / 2 bytes 16 bits / 2 bytes 32 bits / 4 bytes


32 bits / 4 bytes 64 bits / 8 bytes 64 bits / 8 bytes

unsigned signed unsigned signed


unsigned signed unsigned

Byte Int16 UInt16 Int32


UInt32 Int64 UInt64

0 to 255 -32765 to 32768 0 to 65535 -2147483648 to 2147483647


0 to 4294967195 -9223373036854775808 to 9223373036854775807 0 to 18446744073709551615

Datatypes and size in memory

Floating-Point and Decimal Types


Significant Figures
7

C# Keyword
float

Size bits/bytes
32 bits / 4 bytes

Type in System
Single

Range
-3.40282347E38 to 3.40282347E38 1.5 * 10-45 to 3.4 * 1038

double

64 bits / 8 bytes
96 bits / 12 bytes

15

Double

-1.7976931348623157E308 to 1.7976931348623157E308 5.0 * 10-324 to 1.7 * 10308


-79228162514264337593543950335 to 79228162514264337593543950335 1.0 * 10-28 to 7.9 * 1028

decimal

28

Decimal

Datatypes and size in memory

Character Types
Size bits/bytes 16 bits / 2 bytes Type in System Char Value All Unicode Character

C# Keyword char

Boolean Types
Size bits/bytes 8 bits / 1 byte Type in System Boolean true and false Value

C# Keyword bool

Literals

A value of any kind is called a literal Integer literals


int count = 2250; / long = 7854214;

Floating point literals


double d=2.5 E-5.0; / decimal f = 34.34m;

Character literals
char c=B;

Boolean literals
bool b=false; // also true

Literals

NOTE : All the integer types are considered int or long type by default. All the floating point types are considered double by default.
So if you want to store 4.4 in float datatype you can not write this: - float f = 4.4; / decimal d = 4.4; Instead you write float f = 4.4f; decimal d = 4.4m; This way you explicitly tell the compiler to treat 4.4 as float.

Literals

If you want to add two byte or short like:


byte a = 5; byte b = 5; byte c = a+b;

Instead you write


byte a = 5;
byte b = 5; int c = a+b;

Binary Representation

Decimal Numbers Binary numbers Conversion of decimal numbers into binary Conversion of binary numbers into decimal numbers

What is a Variable?

`vary`-able means change-able


The variable is the basic unit of storage. A variable is defined by the combination of a type, variable name and an optional initializer. In c# all variables must be declared before they can be used. Initial value int a = 4;

Type

Name

Example

Some variable declarations examples: 1. int a = 3; 2. int x,y,z; x = 4; y = 6; z = 3; 3. char c = a; 4. bool b = true; 5. double d = 44.4, v = 43.3;

What is a Identifier?

Identifiers are used to name variables, classes, interfaces, methods, and other elements. An identifier is a sequence of characters that starts with an underscore (_), or a letter. Subsequent characters may contain these characters plus the digits 0 through 9.

Rules for Identifiers

A-Z, a-z, 0-9,_,@ Must not start with digit So followings are valid identifiers: _minute total Sum4 Hello67_hello abc_34 _34343

Invalid Identifiers

Followings are invalid identifiers: &total Minute# 4hour 12hello abc@abc $asdfa

Identifiers

Both uppercase and lowercase letters can be used in an identifier. Because C# is case sensitive, therefore these are treated as different identifiers: Total total TOTAL toTal

What is a Keyword?
abstract as base default delegate do foreach goto if null object operator sealed short sizeof uint ulong unchecked

bool
break byte case

double
else enum event

implicit
in int interface

out
override params private

stackalloc
static string struct

unsafe
ushort using virtual

catch
checked class const

explicit
extern false finally

internal
is lock long

protected
public readonly ref

switch
this throw true

void
while

continue
decimal

fixed
float

namespace
new

return
sbyte

try
typeof

Casting / Conversion

Implicit casting
When one type of data is assigned to the other type of variable, an automatic type conversion occurs if left hand type is larger than the right hand type. This conversion is sometimes called widening conversion since you are converting small value to large value.

Casting / Conversion
byte a = 5; int b = a; long c = b; double d = c; All the above are implicit type casting examples. The right hand side is smaller than the left hand side

Casting / Conversion

Explicit casting
When you want to assign a larger value to a smaller value, you can not do it without explicitly casting it using the cast operator. This conversion is sometimes called narrowing conversion since you are explicitly making the value narrower to fit into the small size.

The syntax for explicit casting is: target variable = (target) value;

Casting / Conversion
NOTE: When you explicitly cast large value to small value some information may be lost. For example, when floating point value is assigned to an integer the fractional part is lost int a = 5; byte b = a; The second line will cause error. You can cast the second line like this using cast operator. byte b = (byte) a;

Вам также может понравиться