Constant Variables or Symbols
Constants are variables whose values, once defined, can not be changed by the program. Constant
variables are declare using the const keyword, like:
Constant variables must be initialized as they are declared. It is a syntax error to write:
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 name following Camel notation
could be:
Some typical names of method following Pascal Notation are
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#.
The program blow uses these operators.
using System;
using System.Collections.Generic;
using System.Text;
namespace CSharpProgramming
{
class ArithmeticOperators
{
// The program shows the use of arithmetic operators
// + - * / % ++ --
static void Main(string [] args)
{
// 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 = 20, num2 = 4; // 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,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.
Here, {0}, {1} and {2} will be replaced by the values of the num1, num2, and sum variables. In {i}, i specifier 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:
Or: