C# - Lesson 2 - Variables & Strings

View previous topic View next topic Go down

C# - Lesson 2 - Variables & Strings

Post  Guest on Sat Nov 07, 2009 5:08 pm

Variables:

A variable is a kind of data memory that allows you to store a value for a certain period.

Before you can use a variable, you first need to declare it. This means "saying that it is a variable and saying what kind of variable".

> example: declarating an "int" variable

Code:
int myVariable;

This line of code means that you declare a variable of the "int" type. (int = integer; integers are numbers)
So this mean you can use the variable "myVariable" to store a number.

Now we gonna give the variable a value.

> example: declaration and giving value

Code:
int myVariable;
myVariable = 3;

Now the variable has the value of 3.

We can also write this in one line of code:

Code:
int myVariable = 3;

Ok, now you are able to use this number.

Now, what is a string?

A string is actually a text variable.
We declare it with the word "string" instead of "int" used above.

Since a string is not only one part, but can exist out of more parts we need to put " " around it.
You can either use two " or two ' , that doesn't matter in C#.

> example: declaration of a string

Code:
string myString;
myString = "This is my first string.";

or

Code:
string myString = "This is my first string.";

So, as you see this is how to "save" variables over a certain part of code.
The variable will only work in the class/namespace where it is declared in.
That means it saves the variable as long as the class/namespace is running.
When they are over the variable will not be kept anymore.

This was it for lesson 2.

_________

Next lesson: Conditions


Last edited by UF Xorax on Sat Nov 14, 2009 3:31 am; edited 1 time in total

Guest
Guest


Back to top Go down

Re: C# - Lesson 2 - Variables & Strings

Post  UF Faltzer on Sun Nov 08, 2009 6:13 pm

so that means i can do it in both ways and it will still work the same way?
avatar
UF Faltzer
UF Member
UF Member

Posts : 183
Join date : 2009-09-24

Back to top Go down

Re: C# - Lesson 2 - Variables & Strings

Post  Guest on Mon Nov 09, 2009 12:46 pm

do what in both ways? the declaration of a variable?

Guest
Guest


Back to top Go down

Re: C# - Lesson 2 - Variables & Strings

Post  UF Faltzer on Mon Nov 09, 2009 5:07 pm

UF Xorax wrote:do what in both ways? the declaration of a variable?

yea
avatar
UF Faltzer
UF Member
UF Member

Posts : 183
Join date : 2009-09-24

Back to top Go down

Re: C# - Lesson 2 - Variables & Strings

Post  Guest on Tue Nov 10, 2009 11:51 am

Well yeah, it will work both ways and still act the same.

Guest
Guest


Back to top Go down

Re: C# - Lesson 2 - Variables & Strings

Post  Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum