Professional Documents
Culture Documents
Chapter (1)
Basic Concepts
1
Contents
Data Types
Default Values
Variable
Naming Variable
Variable Declaration
Constant Variable
Keywords
Literals
Escape Character
Comments
2
Contents
Data Type Conversion
Implicit Conversion
Explicit Conversion
Scope
Nullable Type
Enumeration
Array
One-Dimensional Array
Jagged Array
3
Data Types
All types ultimately inherit from object.
The smallest piece of data that a computer can handle is a bit, a single value that can be either 0 or 1.
Eight bits are grouped in a byte.
4
Cont’d
Name Type Size
5
Cont’d
Decimal decimal 16 bytes
6
Cont’d
C# Data Types
bool
byte
ubyte
char
short Object
ushort enum class
int string interfaces
struct
uint delegates
long
ulong
decimal
float
double
DateTime 7
Cont’d
8
Stack and Heap
Stack is used for static memory allocation.
Heap is used for dynamic memory allocation.
It is stored in the computer's RAM.
It is stored in the computer's RAM.
Use the stack if you know exactly how much data
Use heap if you don't know exactly how much data
you need to allocate before compile time and it is not
too big. you will need at runtime or if you need to allocate a
It is LIFO data structure. lot of data.
It is stored Directly. Chunks are allocated to store certain kinds of data
Variables can’t be resized. objects.
Its access fast. Data can be stored and removed in any order.
It contains values for Integral Types, Primitive Types It is stored indirectly.
and References to the Objects
Variables can be resized.
Its access is slow.
9
Stack and Heap
10
Data Types: Value Type
A value type is a simple data type such as int or float that represents the data directly.
Fixed length
Predefined value types : Numeric types, Boolean types and Character types
11
Data Types: Value Type
Example: int num = 100;
int x=10;
int y = x;
num
x 10
value copied
RAM
y 10
12
Data Types: Reference Type
A reference type contains a reference to another piece of memory that actually contains the variable’s data.
Variable length
13
Data Types: Reference Type
User-defined type ( or complex type)
Classes,
Interface,
Delegates
and
Arrays
Predefined ( or simple ) types
Object type and String type
Circle c = new Circle( )
object
c
Stack Heap
14
Data Types: Reference Type
When an assignment between two reference variables occurs, only the reference is copied
The actual value remains in the same memory location (i.e. there are two references to a single value)
p1 0x332255
Person p1 = new Person( ); 0x332255
p1.name = “Su Su”;
Person
p2
=
p1;
Reference
copied Su Su
p2 0x332255
Stack Heap
15
Default Value
Type Default Value
enum type 0
17
Naming Variable
It should begin a letter (A-Z, a-z), underscore, or @ symbol.
After that it can include letters (A-Z, a-z), numbers (0-9), or underscore.
If the name begins with @, it must include at least one other character.
It cannot contain special characters such as &, %, #, and $.
It cannot be the same as C# keywords such as if, for, and public, etc.
num1 - valid
Num1 - valid
student_Name - valid
class - invalid
student Name - invalid
#studentName - invalid
return - invalid
1num - invalid
18
Variable Declaration
int amount = 30000; // declare int type variable named “amount” with initialization
const double pi = 3.14 // declare constant variable pi with double type. Its value cannot be changed later.
19
Constant Variable
A variable whose value will not change during the lifetime of the program.
20
Nullable Type
A null value indicates that a field does not contain any data.
A value type cannot be assigned a null value.
For example, int i = null will give you a compile time error.
C# introduced nullable types that allow you to assign null to value type variables.
Nullable types can only be used with value types.
Example
int? i = null;
double? num = null;
num = 4.5;
21
Keywords
abstract as base bool break
byte case catch char checked
class const continue decimal default
delegate do double else enum
event explicit extern false finally
fixed float for foreach get
goto if implicit in int
interface internal is lock long
namespace new null object operator
out override params private protected
public readonly ref return sbyte
sealed set short sizeof stackable
static string struct switch this
throw true try typeof unit
ulong unchecked unsafe unshort using
virtual void while
22
Literals
23
Literal: Integer Literal
A literal of integer type is know as the integer literal.
A suffix can also be used with the integer literals like U or u are used for unsigned numbers while l or L are used for long
numbers.
Example
20 // int
30u // unsigned int
30l // long
0x4b // hexadecimal 24
Literal: Literal Type Character
U uint
L long
F float
D double
M decimal
25
Literal: Real Literal
Real literals are used to write values of types float, double, and decimal.
one or more digits containing a decimal point (the decimal point can appear before, within, or after the digits),
Example
float ff = -12.55f;
double dd = 2.5e-5D;
decimal mm = 1234e5M;
26
Literal: Boolean Literal
Example:
27
Literal: Single Character Literals &
String Literals
‘2’ ( Single character literals )
Example:
char grade=‘A’;
string result=“Credit”;
28
Escape Character
Escape Character Meaning
\a alert
\b backspace
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
\’ single quote
\” double quote
\\ backslash
\0 null
29
Comments
30
Data Type Conversion
It happens when the value of one data type is assigned to another data type.
If the data types are compatible, C# does automatic Type Conversion (Implicit
Conversion).
Type conversion.
31
Implicit Conversion
narrowing conversion: change a value to a data type that might not be able to hold this value
Example: long salary = 300000;
int empSalary = salary;
widening conversion: change a value to a data type that can hold this value.
Example: float m = 80.5;
double mark = m;
32
Explicit Conversion
These are done explicitly by users using the pre-defined function.
It requires a cast operator.
Casting
A cast operator explicitly tells a program to convert a value from one type to another type.
Example: long salary = 300000;
int empSalary = (int) salary;
Casting Array
double[] num = new double[10];
float[] mark =(float[]) num;
Parsing
To convert a string into other data types
Example:
string num= “1234567”;
int value = int.Parse ( num );
Using System.Convert
Convert class has a variety of methods that values from one type to another.
34
Explicit Conversion
35
Explicit Conversion
36
Scope
A variable’s scope determines which other pieces of code can access it.
If a variable is declared inside a method, only code within that method can access the variable.
Block Level Scope
Block is a series of statements enclosed in braces.
If a variable is declared within a block of code, the variable has block
scope, and only other code within that block can access this variable.
37
Scope
Example:
int i=0; else
{ int product = i * j;
for (i = 0; i < 10; i++) Console.WriteLine("Product = " + product);
{ }
int j = 5;
int k = 100;
if (i == j) Console.WriteLine("K = " + k);
{
}
int sum = i + j;
Console.WriteLine("Sum = " + sum);
}
38
Scope
Method Scope
If a variable is declared inside a method but not within a block, the variable is visible to any code inside the method that follows
the declaration.
Example:
int Sum (int num1,int num2)
{ int sum = num1 + num2;
return sum;
}
Class Scope
A variable with class or structure scope is available to all code in this class or structure even if the code appears before this
variable’s declaration.
39
Scope
Example:
class Circle
{
public double CalArea( )
{
area = Math.PI * 4.5 * 4.5;
return area;
}
double area;
}
40
Enumeration
By default, the first member of an enum has the value 0 and the value of each successive enum member is increased by 1.
41
Enumeration
42
Enumeration
Example
An array is a special data type which can store fixed number of values sequentially in the memory.
Data type of all values in array must be the same.
Array in C# is a reference type which is derived from System.Array class.
Copying an array variable copies the reference only
0 1 2 3 4 5 6 7
Example: Array Declaration and Initialization
45
One-Dimensional Array
string[] name = new string[3]{“Su Su”, “Yu Yu”, “Aung Aung”};
long[] salary = {400000L, 5000000L,200000L};
string[] color;
color = new string[3] {“White”, “Orange”, “Red”};
Example: Assigning Array
string[] color = new string[3];
color[0] = “White”;
color[1] = “Orange”;
color[2] = “Red”;
46
One-Dimensional Array
Example: Displaying Array Element
string[] name = new string[3]{“Su Su”, “Yu Yu”, “Aung Aung”};
for(int i=0; i< name.Length; i++)
{ Console.WriteLine(name[i]); }
(or)
foreach( string st in name)
{ Console.WriteLine(st)); }
47
Array Properties and Methods
48
Multi-Dimensional Array
A multi-dimensional array is a two dimensional series like rows and columns.
Example:
int[,] num = new int[ 3 , 3 ]
double[,] salary;
int[,] num = new int[3,2] { { 10,11}, { 20,3}, {2, 55}};
49
Multi-Dimensional Array
string[ , ] subject = {
{ “C#”, “JAVA”, “C++”},
{ “C”, “VB.NET”, “C#.NET”},
{ “XML”, “HTML”, “SQL”},
};
50
Jagged Array
A jagged array is an array of an array.
It stores arrays instead of any other data type value directly.
A jagged array is initialized with two square brackets [][].
The first bracket specifies the size of an array and the second bracket specifies the dimension of the array which is
going to store as values.
A
Example
int[][] A = new int[2][ ];
1 2 3
A[0] = new int[3]{1, 2, 3}; 0
int[][] A= new int[3][]
{
new int[3]{15, 7, 22},
new int[2]{67, 81},
new int[4]{12,91,1,17}
};
Console.WriteLine(A[0][0]);
Console.WriteLine(A[1][1]);
Console.WriteLine(A[2][3]);
52
Jagged Array
1 2
int[][,] B = new int[3][,];
B 3 4
B[0] = new int[3, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 } }; 0 5 6
0 0
0 0
53
Thank You
54