Syntax, Data Types, and conversion - C# FOR BEGINNERS CRASH COURSE (2014)

C# FOR BEGINNERS CRASH COURSE (2014)

Chapter 3 Syntax, Data Types, and conversion

3.1 Different keywords in C#

Keywords are special predefined reserved words and are each assigned with a unique meaning. These keywords can be organized in to categories useful for better understanding. Below is a list of keywords categorized into different types.

1) Keywords for class, method, field and property

· abstract

· extern

· internal

· new

· const

· override

· protected

· private

· public

· sealed

· readonly

· static

· virtual

· void

2) Keywords for type conversions

· explicit

· implicit

· as

· is

· operator

· sizeof

· typeof

3) Keywords useful for program flow control

· if

· else

· for

· foreach

· in

· case

· break

· continue

· return

· while

· goto

· default

· do

· switch

4) Keywords used for built in types and enumerations

· bool

· char

· class

· byte

· decimal

· enum

· double

· float

· interface

· long

· int

· object

· sbyte

· short

· string

· uint

· struct

· ulong

· ushort

5) Keywords used for exception handling

· try

· catch

· throw

· finally

· checked

· unchecked

6) Keywords used as literals, method passing parameters

· true

· false

· null

· this

· value

· out

· params

· ref

7) Keywords useful in function pointers, object allocation, unmanaged code

· delegate

· event

· new

· stackalloc

· unsafe

3.2 Data Types in C#

Data types are used to store the data in a specific type. There are several built in data types that are used by the programmers for declaring data.

Every data type has a limited set of options it can be, these limited number of options are called the data range for the data type. Listed below are the different data types present and the data range that they can be:

· bool: Used to represent the Boolean value. The values that can be assigned are true or false.

· byte: 8 – bit unsigned integer. The range of value for a byte data type is from 0 to 255

· char: 16 – bit Unicode character. The range of values is from U +0000 to U + ffff

· double: 64 – bit double precision floating point type. The range of values is from (+/-) 5.0 x 10-324 to (+/-) 1.7 x 10308

· decimal: 128 bit precise decimal values with significant digits. The range of values is from ( -7.9 x 1028 to 7.9 x 1028 ) / 100 to 28

· float: 32 bit single precision floating point. The range of values is from -3.4 x 1038 to + 3.4 x 1038

· int: 32 – bit signed integer type. It has range of values from -2,147,483,648 to 2,147,483,647

· long: 64 – bit signed integer type. It has range of values from -923,372,036,854,775,808 to 9,223,372,036,854,775,807

· sbyte: 8 – bit signed integer type. It has range of values from -128 to 127

· short: 16 – bit signed integer type. It has range of values from -32,768 to 32,767

· unit: 32 – bit unsigned integer type. It has range of values from 0 to 4,294,967,295

· ulong: 64 – bit unsigned integer type. It has range of values from 0 to 18,446,744,073,709,551,615

· ushort: 16 – bit unsigned integer type. It has range of values from 0 to 65,535

3.3 Type conversion in C#

Type conversion is useful when the programmer needs to convert from one data type to another. The type conversion is also known as type casting. There are two types of type casting in C#. They are implicit type conversion and explicit type conversion.

Implicit type conversion

The implicit keyword is used for implicit conversions. They do not need any casting operator. These conversions include small to large integral type, from derived class to base class.

Explicit type conversion

The explicit conversions are done explicitly by users through the use of the pre-defined functions. In these conversions, a cast operator is needed.

Example 3:

using System;

namespace TypeConversion

{

class Conversion

{

static void Main(string[ ] args)

{

double d=10.243;

int i;

i = int (d);

Console.WriteLine(i);

Console.Read();

}

}

}

C# consists of type conversion methods that are useful for users.

The following list shows the type conversion methods.

· ToByte: Used for converting a type to byte

· ToBoolean: Used for converting Boolean value

· ToDateTime: Used for converting a type to the date time type

· ToDouble: Converts a type to double

· ToDecimal: Converts the floating point or integer to decimal type

· ToInt64: Converts the a type to 64 bit integer

· ToSingle: Converts the type to floating point number

· ToString: Converts the type to string type

· ToUInt64: Converts a type to an unsigned big integer

The following code snippet shows the conversion of value type to Double type.

Example 4:

using System;

namespace type

{

class DoubleConversion

{

int a=10;

float f=30.05f;

bool b=false;

Console.WriteLine(a.ToDouble());

Console.WriteLine(f.ToDouble());

Console.WriteLine(b.ToDouble());

Console.Read();

}

}