Friday 31 July 2009

Beginning C# Lesson 3 - Variables

A variable is something that stores data.
It could be a character, number, image or many other things,
the thing you have to understand is how to declare and user
them, and what datatypes store which type fo data.

Variables are given a unique name inside the "scope" of the
program, i will talk about scope soon for now just understand that
we cant have two variables the same name in a method unless
the scope of the method changes.

Software developers can have long discussions on what datatypes
to use in a program, as using the wrong type could have negative
impact on your applications performance or maintability.

Data Type

Range

byte

0 .. 255

sbyte

-128 .. 127

short

-32,768 .. 32,767

ushort

0 .. 65,535

int

-2,147,483,648 .. 2,147,483,647

uint

0 .. 4,294,967,295

long

-9,223,372,036,854,775,808 .. 9,223,372,036,854,775,807

ulong

0 .. 18,446,744,073,709,551,615

float

-3.402823e38 .. 3.402823e38

double

-1.79769313486232e308 .. 1.79769313486232e308

decimal

-79228162514264337593543950335 .. 79228162514264337593543950335

char

A Unicode character.

string

A string of Unicode characters.

bool

True or False.

object

An object.


You dont neeed to know all of them at the moment but there are a few i will introduce you too:
  • char - stores a single character
  • string - stores a group of characters
  • int - stores hole numbers (check the above table for its range)
Using 'char'

Characters are two bytes in memory space, and are are assigned using 'charvalue' instead of the string "stringvalue", characters can hold a single character mfor example :

char myCharacter = 'M';

Example:



You can "Concatenate"(join) values inside a string. The Console.WriteLine(string) stores only a string, when you input arguments it trys to convert the data (if its not a string datatype) to string, using the "+" operator we can concatenate the string "My character is " with the variable myCharacter to form one string inside the writeline, when the WriteLine() has converted the values it will write it on the console.

Using "string"

string is a group or collection of characters concatenated to form a variable and its file size is two bytes per character.

We assign a value to a string datatype using quotations "stringvalue" for example:
string myString = "Strings are awesome";

string program example:



As you can see, we can join strings together too.

Using an Integer

Integer's are whole numbers (see the range from table) and is 3 bytes in memory.
They do not require quotations or apostrophe's and are assigned (as most numerical datatypes are) purely for example:

int myInteger = 100;

Because int only accepts whole numbers, we can not use decimal points.

Integer example:



Integers can also perform mathmatical operations known as "operators", operators work with raw data so when you see 2 + 2 your actually seeing:

0000 0010
0000 0010 +
Total = 0000 0100

Just like the concatenateoperator that joins the binary format of strings.
This is a big reason why we use datatypes, to tell the compiler how to computate
the binary representation. So "+" two strings or characters joins them and "+" two
integers adds them, there is some exceptions how ever, ie how would you join
the values of a string with an int?

The following code shows how:



Rerember a WriteLine(string) method accepts only string variables, and as previously
mentioned it attempts to convert its argument values to the string datatype.
The datatype int is one of those argument values that can be converted to a string with
little hassle, so when its used inside the WriteLine() method it is automaticly converted to a string, then the WriteLine() checks for any operators and finds "+", so its treats both variables as string and joins them together.

Also take note of the space i entered after "My int is", space is a character just like
all other characters on your keyboard, and is treated the same.

An example of math operators using ints:



Notice how I put the addition inside brackets?

Its because of the WriteLine() "Order of precedence", what operators are
checked and computated in order.

Brackets are higher order of precedence than joining the string, so it first calculates the
total of (a + b) then converts the result to a string and finally joins it to the string.

+ is addition.
* is multiplication.
/ is devide
% is modulas (remainder of a number)

Some other numerical datatypes such as "Decimal" (decimal) have the same operators and
work the same way.

Console input/output

Applications can take input and output, a Console application inherits those rules.
So far we have only outputted values using the Console.WriteLine() method from the Console class, how ever we can also input data into a variable!

An example, you need to store a persons first name , you could add this code inside your
Main method:

string name ;
Console.Write("Enter your first name : ");
name = Console.ReadLine();
Console.WriteLine("Your name is " + name);
Console.ReadKey();

The variable name is declared but not assigned a value,
then the console uses the "Write()" method.

The Write() method is similar to WriteLine() but instead of writing some text and then setting the cursor on a new line it just writes it on where ever the cursor is, please compile that code to understand visually!

Once the message is displayed, we use the Console.ReadLine() to read data from where ever the cursor was to the last character entered in the console.



You cannot explicity ReadLine() a datatype other than a string, to if you were to add data to
an int variable you would have to convert the ReadLine() data to an int which will be explained in another lesson.

That basicly sums up the introduction to variables lesson, hope it was fun :P

No comments:

Post a Comment