Phemmy Google Search

Monday, August 4, 2008

C# Variable

All Data in a computer is effectively the same thing(a series of zeros & one). Variables are of different types. Using a box analogy to explain variables; one can assume variables in computer memory as boxes sitting on a shelf. One can imagine that your boxes come in different shapes & sizes and some things will only fit in certain boxes. According to this analogy different types of data may require different methods of manipulation.


To use variables you have to declare them. This means that you have to assign them a name and a type. If this is done the variable can be used as storage units for the type of data that you declare them to hold. C# syntax for declaration of variable and type is:
(type) (name);
If any variable is not declared your code will not compile. There are certain types of data that just about everyone will want to use at one time or the other such as a variable that stores a number. There are a number of simple, predefined types that one should be aware of:


Type


sbyte
byte
short
Ushort
int
Uint
long
Ulong
float
double
decimal
char
bool
string


It should be noted that string has no upper limit on the amount of characters because it can use varying amount of memory; while boolean type bool is one of the most commonly used variable type in C#.


Example of variable declaration


1. Create a new console Application


2. Add the following code to program.cs


Static void Main(string [] args)
{
int myInteger;
string myString;
myInteger=17;
myString= "\"my Integer\" is ";
Console. writeline ("{0} {1}.", mySrting, myInteger);
console.Readkey();
}


3. Execute the code




Variable Naming
The basic variable naming rules are:
  • The first character of a variable name must be either a letter or underscore(_), or the at(@) symbol.
  • Subsequent characters may be letters, underscore characters or numbers.
e.g. myBigVar
VAR1
_test
@test


While these are wrong
99Bottlesof Beer
namespace
It's-All-Over


These are wrong because of some certain keywords that have a specialized meaning to the C# compiler such as namespace while others are number and case sensitivity.


Furthermore, one can have multiple variables whose names differ only in case, such as the ones below:
myVariable
MyVariable
MYVARIABLE


Naming Conventions
Over the years different systems have come and gone while some developers fought tooth and nail to justify their own system of naming.


Hungarian notation is recently known as the most popular system. This entails placing a lower case prefix on all variable names that identifies the type. For instance, if a variable were of type int then you might place an i (or n) in front of it such as in iAge. Adopting this system, it will be easy to see it at a glance what types different variables are.


Currently, there are two naming conventions in use in the .Net framework namespace; they are:


(a) PascalCase e.g.
Age
LastName
WinterOfDiscontent


(b) camelCase e.g.
age
firstName
timeOfDeath
For simple variable use camelCase but for advanced naming use PascalCase which is the Microsoft recommendation.


Variable Declaration & Assignment
Recall that declaration of variable entails using their type and name e.g.
int Age;
Then assign values to variable using (=) assignment operator
age=25;


Declaring multiple variables of the same type at the same time can be done uing commas to separate the variables e.g.


int xSize, ySize;
i.e. xsize and ysize are both declared as integer types.


The second method you are likely to see in assigning values to variables at the same time as declaring them can be:


int age=25;
You can also use both methods together.


int xSize=4, ySize=5;


Olufemi

No comments: