In this chapter we'll be looking at data-types in C# as well variables and how to declare them.
Now let's take a look at the different datatypes and a first look at variables. We'll look more into this later.
Data-type chart:
Source: http://www.dotnet-guide.com/images/unifi...-sharp.jpg
You may notice that string is not on the list, but maybe I know why that is. A string is not an actual data type (It is one, but not a simple datatype), but an array or sequence of unicode characters. Which the char data-type is equal to.
A char is actually equal to a byte as well, but more about that later when we get to some of the advanced things.
If you look at the example that's how you declare the different variables.
So let's try to declare a string.
Let's break this line into pieces.
The first thing we see is:
That is the datatype of our variable.
Then we have:
That is the name of our variable.
Then we have:
That is an operator used to make it equal to the value we're giving it. It can also be used to assign the left handed variable with a (data)type or pointer.
Then we have:
That is the value of our string. A string has to be declared in apostrophes ("), but an integer ex. int or byte does not need them, but they're also only numerics as they're integers.
At last we have:
If you cannot remember what it does then read back in the previous tutorial.
Or mark below:
<from>A semi-colon (;) is used as an end statement in C#. For a clear explanation look back in the previous tutorial.<to>
What I will suggest now is to try to declare different types of variables, look at the chart for examples.
Now try to see if you can write them out to the console as well.
Example:
Now let's look into some mathematical operations.
You can add/remove values etc. to a variable, some variables support other operators than others, but let's just take a look at int as of now. More about type-conversion later.)
Here are some examples of mathematical operations:
You can also do this if you wish to make X equal to the result then instead doing:
You could just do this from the start.
These are just examples and there is a lot more operators etc. and things you can do.
Now let's get more specific what a variable is. A variable is a place in your computers memory that you can use to hold some data. See your memory as a city. A city has a lot of different house. The different house has differerent peoples living/objects stored in them.
If the memory is the city, the variable is the house and the data is the people and objects in the house.
Example of memory.
That was a bit about data-types and variables.
Downloads/Links:
MSDN (Data-types)
MSDN (Operators)
Datatypes & Variables Download (Source)
Original
Now let's take a look at the different datatypes and a first look at variables. We'll look more into this later.
Data-type chart:
Source: http://www.dotnet-guide.com/images/unifi...-sharp.jpg
You may notice that string is not on the list, but maybe I know why that is. A string is not an actual data type (It is one, but not a simple datatype), but an array or sequence of unicode characters. Which the char data-type is equal to.
A char is actually equal to a byte as well, but more about that later when we get to some of the advanced things.
If you look at the example that's how you declare the different variables.
So let's try to declare a string.
Code:
string myString = "Hello Hack Forums!";
Let's break this line into pieces.
The first thing we see is:
Code:
string
That is the datatype of our variable.
Then we have:
Code:
myString
That is the name of our variable.
Then we have:
Code:
=
That is an operator used to make it equal to the value we're giving it. It can also be used to assign the left handed variable with a (data)type or pointer.
Then we have:
Code:
"Hello Hack Forums!"
That is the value of our string. A string has to be declared in apostrophes ("), but an integer ex. int or byte does not need them, but they're also only numerics as they're integers.
At last we have:
Code:
;
If you cannot remember what it does then read back in the previous tutorial.
Or mark below:
<from>A semi-colon (;) is used as an end statement in C#. For a clear explanation look back in the previous tutorial.<to>
What I will suggest now is to try to declare different types of variables, look at the chart for examples.
Now try to see if you can write them out to the console as well.
Example:
Code:
static void Main(string[] args)
{
string myString = "Hello Hack Forums!";
int myInt = 105000;
short myShort = 5252;
byte myByte = 200;
Console.WriteLine(myString);
Console.WriteLine(myInt);
Console.WriteLine(myShort);
Console.WriteLine(myByte);
Console.ReadLine();
}
Now let's look into some mathematical operations.
You can add/remove values etc. to a variable, some variables support other operators than others, but let's just take a look at int as of now. More about type-conversion later.)
Here are some examples of mathematical operations:
Code:
int X = 10;
int Y = 15;
int Res = X + Y;
Code:
int X = 10;
int Y = 15;
int Res = X - Y;
Code:
int X = 10;
int Y = 15;
int Res = X * Y;
Code:
int X = 10;
int Y = 15;
int Res = X / Y;
Code:
int X = 10;
int Y = 15;
int Res = X ^ Y;
You can also do this if you wish to make X equal to the result then instead doing:
Code:
X = Res;
You could just do this from the start.
Code:
int X = 10;
int Y = 15;
X += Y;
Code:
int X = 10;
int Y = 15;
X -= Y;
Code:
int X = 10;
int Y = 15;
X *= Y;
Code:
int X = 10;
int Y = 15;
X /= Y;
Code:
int X = 10;
int Y = 15;
X ^= Y;
These are just examples and there is a lot more operators etc. and things you can do.
Now let's get more specific what a variable is. A variable is a place in your computers memory that you can use to hold some data. See your memory as a city. A city has a lot of different house. The different house has differerent peoples living/objects stored in them.
If the memory is the city, the variable is the house and the data is the people and objects in the house.
Example of memory.
That was a bit about data-types and variables.
Downloads/Links:
MSDN (Data-types)
MSDN (Operators)
Datatypes & Variables Download (Source)
Original
No comments:
Post a Comment