Previous Page Next Page
Variables
During the execution of a program, data is temporarily stored in memory. A variable is the name given to a memory location holding a particular type of data. So, each variable has associated with it a data type and a value. In C#, variables are declared as:
<data type> <variable>;
e.g.,
int i;
The above line will reserve an area of 4 bytes in memory to store an integer type values, which will be referred to in the rest of program by the identifier 'i'. You can initialize the variable as you declare it (on the fly) and can also declare/initialize multiple variables of the same type in a single statement, e.g.,
bool isReady = true;
float percentage = 87.88, average = 43.9;
char digit = '7';
In C# (like other modern languages), you must declare variables before using them. Also, there is the concept of "Definite Assignment" in C# which says "local variables (variables defined in a method) must be initialized before being used". The following program won't compile:
static void Main()
{
int age;
// age = 18;
Console.WriteLine(age); // error
}
But, if you un-comment the 2nd line, the program will compile. C# does not assign default values to local variables. C# is also a type safe language, i.e., values of particular data type can only be stored in their respective (or compatible) data type. You can't store integer values in Boolean data types like we used to do in C/C++.
Constant Variables or Symbols
Constants are variables whose values, once defined, can not be changed by the program. Constant variables are declared using the
const keyword, like:
const double PI = 3.142;
Constant variables must be initialized as they are declared. It is a syntax error to write:
const int MARKS;
It is conventional to use capital letters when naming constant variables.
Naming Conventions for variables and methods
Microsoft suggests using
Camel Notation (first letter in lowercase) for variables and
Pascal Notation (first letter in uppercase) for methods. Each word after the first word in the name of both variables and methods should start with a capital letter. For example, variable names following Camel notation could be:
salary totalSalary
myMathsMarks isPaid
Some typical names of method following Pacal Notation are
GetTotal() Start()
WriteLine() LastIndexOf()
Although it is not mandatory to follow this convention, it is highly recommended that you strictly follow the convention. Microsoft no longer supports Hungarian notation, like using iMarks for integer variable. Also, using the underscore _ in identifiers is not encouraged.
Operators in C#
Arithmetic Operators
Several common arithmetic operators are allowed in C#.
Operand Description
+ (add)
- (subtract)
* (multiply)
/ (divide)
% (remainder or modulo)
++ (increment by 1)
-- (decrement by 1)
The program below uses these operators.
using System;
namespace CSharpSchool
{
class ArithmeticOperators
{
// The program shows the use of arithmetic operators
// + - * / % ++ --
static void Main()
{
// result of addition, subtraction,
// multiplication and modulus operator
int sum=0, difference=0, product=0, modulo=0;
float quotient=0; // result of division
int num1 = 10, num2 = 2; // operand variables
sum = num1 + num2;
difference = num1 - num2;
product = num1 * num2;
quotient = num1 / num2;
// remainder of 3/2
modulo = 3 % num2;
Console.WriteLine
("num1 = {0}, num2 = {1}", num1, num2);
Console.WriteLine();
Console.WriteLine
("Sum of {0} and {1} is {2}",
num1, num2, sum);
Console.WriteLine
("Difference of {0} and {1} is {2}",
num1, num2, difference);
Console.WriteLine
("Product of {0} and {1} is {2}",
num1, num2, product);
Console.WriteLine
("Quotient of {0} and {1} is {2}",
num1, num2, quotient);
Console.WriteLine();
Console.WriteLine
("Remainder when 3 is divided by
{0} is {1}", num2, modulo);
num1++; // increment num1 by 1
num2--; // decrement num2 by 1
Console.WriteLine
("num1 = {0}, num2 = {1}", num1, num2);
}
}
}
Although the program above is quite simple, I would like to discuss some concepts here. In the
Console.WriteLine() method, we have used format-specifiers
{int} to indicate the position of variables in the string.
Console.WriteLine
("Sum of {0} and {1} is {2}",
num1, num2, sum);
Here,
{0},
{1} and
{2} will be replaced by the values of the
num1,
num2 and
sum variables. In
{i},
i specifies that (i+1)th variable after double quotes will replace it when printed to the Console. Hence,
{0} will be replaced by the first one,
{1} will be replaced by the second variable and so on...
Another point to note is that num1++ has the same meaning as:
num1 = num1 + 1;
or,
num1 += 1;
(We will see the description of second statement shortly)
Previous Page Next Page