Sei sulla pagina 1di 9

Variables & Data Types

Variables
Variable is a named memory location which stores the value and
the value can be changed through the program
Constraints on Variable Names
The Variable name should contain characters (a-z ,A-Z), numbers
(0-9) and underscore (_)
The Variable name should start with either character or
underscore (_)
Capitalization Styles Defined
We define three types of capitalization styles
Pascal Case: The first letter in the identifier and the first letter of
each subsequent concatenated word are capitalized.
E.g. BackColor, DataSet
Camel Case: The first letter of an identifier is lowercase and the first
letter of each subsequent concatenated word is capitalized.
E.g. numberOfDay, isValid

UpperCase: All letters in the identifier are capitalized.


E.g. ID, PI
Variable has two parts
1) Declaration
2) Definition
Declaration: Specifying the type of the variable. Syntax: DataType
identifier;
E.g. int a;
Definition: Assigning the value to the variable
E.g. a = 5;
There are two ways in assigning values to the variables
1) Static Assignment
2) Dynamic Assignment
Static Assignment: Assigning Hard - Coded Values in the program
E.g. a = 5;
Dynamic Assignment: Reading the values to the variable during
the run time
E.g. a = Console.ReadLine();

Data Reading and Writing


Console.Read() Method:
Read() method to get a value from the user. To use it, the name of
a variable can be assigned to it. The syntax used is:
VariableName = Console.Read();
Console.ReadLine() Method:
After performing its assignment, the ReadLine() method sends the
caret to the next line.
Console.Write() Method (or) Console.WriteLine() method:
Both the methods are used to display the data to the screen
WriteLine() method sends the caret to the next line.
Console.WriteLine(Welcome to c# programming);

Data Types

Data Type specifies what kind of values the variable can store
How many bytes of memory should be allocated for the variable
Data Types are classified into two categories
1) Value Types
2) Reference Types
Values Types:
stores its value directly
Value types are stored in memory area known as Stack
Predefined Value Types
1) Integer 2) Float 3) Decimal
4) Boolean
5) Char
Reference Types
Reference Type stores a reference to the value
Reference Types are stored in memory area known as Heap
Predefined Reference Types
1)Object
2) String

Boxing and Unboxing


Boxing:
Converting the value type to reference type is known as boxing.
In Conversion process, variable memory location is also changed
from stack to the heap
Boxing is a implicit conversion process
E.g. int i = 5;
object o;
o = i;
Unboxing:
Converting the reference type to value type is known as unboxing.
In Conversion process, variable memory location is also changed
from heap to the stack
Unboxing is an explicit conversion process
E.g. Object o = 5;
int i = (int) o;

Scope of Variable
The scope of a variable is the region of code from which the variable can
be accessed. In general, the scope is determined by the following rules:
A field (also known as a member variable) of a class is in scope for as
long as its containing class is in scope (this is the same as for C++, Java,
and VB).
A local variable is in scope until a closing brace indicates the end of the
block statement or method in which it was declared.
A local variable that is declared in a for, while, or similar statement is in
scope in the body of that loop.
Is Scope clashes for Fields and Local Variables? NO
class progam {
int j = 10;
public static void Main ( String[] args) {
int j = 20;
Console.WriteLine (j);
}
}

Constants
Prefixing a variable with the const keyword when it is declared and
initialized designates that variable as a constant. As the name
implies, a constant is a variable whose value cannot be changed
throughout its lifetime:
const int A = 100; // This value cannot be changed.
Characteristics of Constants
They must be initialized when they are declared, and once a value
has been assigned, it can never be overwritten.
The value of a constant must be computable at compile time.
Constants are always implicitly static.
Advantages of Constants
Constants make your programs easier to read by replacing magic
numbers and strings with readable names whose values are easy to
understand.
Constants make your programs easier to modify
Constants make it easier to avoid mistakes in your programs

THANK YOU

Potrebbero piacerti anche