Anda di halaman 1dari 8

C # Chapter 4 Literals Variables and Data Types

A C# program is a collection of Classes. A class is defined by the


set o declarations statements and methods containing
instructions known as executable statements.

The smallest non-reducible , textual elements in program are


referred to as TOKENS.

Following are the 5 types of tokens ::

1) Keywords
2) Identifiers
3) Literals
4) Operators
5) Punctuators

NOTE : White Spaces and Comments are not tokens , though


they may act as separators for tokens .

KEYWORDS :

They are the reserved words which has specific meaning to it,
that cannot be used as an identifier , except when they are
prefaced by @ character.

Eg. new, finally, private etc.. (is not a valid identifier)

But;; @new, @finally, @private is a valid one.

IDENTIFIERS ::

They are programmer designed tokens used for naming classes,


methods , variables, labels, interfaces etc..
Following are the rules for defining an identifier ::

They can have alphabets digits and an underscore charater.


They must nit begin with a digit
It is case sensitive.
Keywords cannot be used as an identifier.

PUNCTUATORS ::(also called as Separators)

They are the symbols used for grouping and separating code.

It includes ::

Parantheses()
Braces {}
Brackets []
Semicolon ;
Colon :
Comma ,
Period .

STATEMENTS :

They are like sentences in natural languages.

A statement is an executable combination of tokens ending with


semicolon.

It includes :

Empty statements
Labeled statements
Declaration statements
Expression statements
Selection statements
Interaction statements
Jump statements
Try statements
Checked statements
Unchecked statements
Lock statements
Using statements

LITERALS ::

They are values assigned to a variable whose value remains


constant throughout the program.

They can be divided as :

1) Numeric Literals.

Integer Literals

Real Literals

2) Character Literals

Single character Literals( )

String Literals ().

3) Boolean Literals.
Some pre-defined Backslash Literals:

Constants Meaning
\a Alert
\b Back space
\n New line
\r Carriage return
\t Horizontal tab
\v Vertical tab
\ Single Quote
\ Double Quote
\\ Back slash
\o null

VARIABLES :

A variable is an identifier that denotes a storage location used to


store a data value.

Data Types :

Data types specify the types and size of the values that can be
stored in an variable. C# is language rich in its datatypes.

Following are the types of data types used in C# :


1) Value Types :

Pre-defined types :

a. Integers.

b. Real nos

c. Booleans

d. Characters

User Defined Types :

a. Enumerations

b. Structures

2) Reference Types :

Predefined Types :

a. Objects

b. Strings

User defined Types :

a. Classes

b. Arrays

c. Interfaces

d. Delegates

3) Pointers.
Default Values :

Type Default Value


All integer types 0
Char types \x000
float 0.0f
double 0.0d
Decimal 0.0m
Bool False
enum 0
All reference Types Null

Scope of the Variables :

The scope of the variable is the region of the code within which
the variable can be accessed.

Consider the code below :

Class ABC

Static int m;

Int n;

Void fun(int x, ref int y, out int z, int [] a)


{

Int j=0;

The above code contains the foll variables :

M as static variable

N is instance variable

X is a value parameter

Y is reference parameter

Z is output parameter

A is an array element

J is a local variable

Static and instance variables are declared at the class level and
are known as fields. Scope of these variables begins at their
declaration and ends when Main Terminates.

Variables x, y, z are the variables of the method fun() so the


scope will be limited till that point.

BOXING and UNBOXING ::


In OOP language methods are invoked using Objects.

Since int and long are not objects they cannot be used to call
methods.

But this can be achieved in C # using the technique called as


Boxing.

Boxing means conversion of a value type on the stack to a object


type on the heap.

Conversion of Object type back to value type is known as


Unboxing

Eg :

int m=10;

Object om =m; //creates a box to hold m

M is stored in a stack and om is stored on a heap.

i.e both m and om are independent of each other.

int m=10;

object om= m ;

int n = (int)om; // unbox om back to int

Anda mungkin juga menyukai