C# Tutorial, starting, learning the basics, and keeping...
C# Tutorial by: Brinuz @ MPGH.
First of all, this is not C++!!! and this is not exactly the best language to mak hacks, etc.
You need a compiler, i sugest:
Visual C# 2008 Express Edition.
You'll easly find it free to download and install.
*Open it and let's create a project:
-File -> New Project
-Select Console Application
-Call it w/e you want
-Click ok
Ok, before starting to post Codes of lines and lines, you should know a few basic things to code:
***************************
This:
// when you see something like this in the code, it means that the program will ignore it, its just a comment in the lines.
OR
/*when you see something like this in the code, it means that the program will ignore it, its just a comment in the lines. */
***************************
*Every normal line of coding that contains an instruction should end with ";".
e.g:
Code:
int var;
string text;
text = "www.mpgh.net";
var = 1337;
***************************
*Getting the past e.g, you should know what are the most basic data type in this language: String, Int.
lets set it easy:
-An
Int is a "bit" of memory that is created, and used by the program that will let you save a number. (ATTENTION: NOT NUMBERS LIKE 1.3, or 1323.1256788, for that you shuld use the type: Double or Float).
-A
String is a "bit" of memory that is created, and used by the program that will let you save text, basiclly anything you write or set in it (this includes numbers, letters, symobols, etc).
*Also when you create one:
you can initialize it as a value, eg:
Code:
string text = "www.mpgh.net";
but, lets say that you will only later what it will have, you can always do:
Code:
string text;
//code
//code
//code
//code
text = "www.mpgh.net";
*You can also "say" that a string is equal to another one:
Code:
string Text = "www.mpgh.net";
string Link = Text;
OR
Code:
string Text = "www.mpgh.net";
string Link;
Link = Text;
*You can add one string to another:
Code:
string Site = "mpgh.net";
string web = "www.";
web = web + site; // this will add "www."+"mpgh.net", which will result in "www.mpgh.net"
*You can do maths with data of the type int:
Code:
int x = 2;
int y = 1;
int result;
result = x + y; //the value of result will be 3. which is 2 plus 1.
***************************
Bla, bla bla... im here writing stuff and you will probabily don't understand without using it. (which may be normal, mainly because i can't explain myself that well, even if i know xD).
Let's get into action and code something!
Code:
/* * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * *
* C# Starting Tutorial and basics for MPGH by Brinuz. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * */
// Basiclly this contais objects that you might use later on your code like Console. the one beeing used in Main() (which makes you able to write on console)
using System;
// You will learn a bit more about this, but as you may realise this has the same name as you console application.
namespace ConsoleApplication1
{
public class Program //your main Class, the body of you code. I will talk later about what is a class and how to use.
{
static void Main() //your main, your program will start #
{
// # HERE and will finish always (or most of the times.... you may be able to close it, quit it at the middle of it, etc...) *
string textWorld = " Hello World!"; //Explained it Before at the start of the post.
Console.Write("The mighty message that everyone writes:");
// This is the basic mode of writing in Console: Console.Write(""); this will write a line with the text that you may insert in ("").
Console.WriteLine(textWorld);
// This one does the same as the one before, but will add a line at the end of the line, so the next Console.Write(""); or w/e will be used in the next line.
// AND
// Instead of the need to write " Hello World!" thing, since you already have it saved in memory, you can use it. (textWorld)
// * FINISH HERE
}
}
}
Ok, i guess you can see one or other thing that i tried to explain at the start, beeing used in there... You shouldnt really pay that much attention now to Class's, namespace, etc...
*************************
*Lets say you want to compare two or more things in your program, AGE, for example...
you use;
Code:
if(condition)
{
}
else
{
}
condition Operators:
> or < : Obvious?
== : equals.
!= : different
*you should do somethings like this:
Code:
using System;
namespace ConsoleApplication1
{
public class Program
{
static void Main()
{
int a = 18;
int b = 29;
if (a > b) //this will compare 2 or more things (lets just stay with 2 for now...) it will check if a is bigger then b, and if it is, it will do:
{
Console.WriteLine("var a is Older then var b"); //since a were older (or bigger) then b, it will execute every instruction inside this { ... }
}
else //if a isnt bigger then b, then he will do this (just like our language: If i'm a boy, i will like girls, Else i will like boys.
{
Console.WriteLine("var a i Younger then var b");
}
}
}
}
Of course this one wouldnt be that logic because you always know the values you insert at a and b, so you actually know which one is the older. Let's try another new thing:
*You can read something that you type on console:
Code:
string Name = "";
Console.WriteLine("Insert your name:");
Name = Console.ReadLine(); //it will save what he read from your input in the var Name
Console.WriteLine("Your name is " + Name);
as simple as that. the string Name will get the value (or text) that you typed in the console and when Enter key is pressed.
*Now getting this into a Program (using the old eg.)
Code:
using System;
namespace ConsoleApplication1
{
public class Program
{
static void Main()
{
int a = 0;
int b = 0;
Console.WriteLine("Insert the age of the first person:");
a = Convert.ToInt32(Console.ReadLine()); //i do have here to use Convert.ToInt32() in here because he will need to convert the type string (text, etc.) into a INT, so it has a better way to compare, or do mats, w/e.
Console.WriteLine("Insert the age of the secound person:");
b = Convert.ToInt32(Console.ReadLine());
if (a > b) //this will compare 2 or more things (lets just stay with 2 for now...) it will check if a is bigger then b, and if it is, it will do:
{
Console.WriteLine("var a is Older then var b"); //since a were older (or bigger) then b, it will execute every instruction inside this { ... }
}
else //if a isnt bigger then b, then he will do this (just like our language: If i'm a boy, i will like girls, Else i will like boys.
{
Console.WriteLine("var a i Younger then var b");
}
}
}
}
*Now that you know something already let's have something for you to do by urself or check:
TO DO:
Make a program that follow this:
-Will ask a user to input 2 numbers
-these numbers can't be the same
-numbers must to be between 0 and 20.
-at the end it will write in the console which number is the bigger and tell the difference between them.
eg: if you insert 5 and 15... the difference would be 10... from 5 to 15.
IMPORTANT!!! IF YOU REALLY WANT TO LEARN TO THIS BY URSELF WITHOUT LOOKING TO THE SOLUTION WITHOUT BEEING FINISHED!!
-You can check what you have read already.
-You Shouldnt check the solution without finishing.
-Try not to Copy&Paste.
*i'm advicing you this, because it is the best way to learn something
*
One of the many possible Solutions:
i will make this as simpler as possible for you to understand, but there is a thing that you need to be "aware", IT IS possible to make this in a simpler, and way better mode. I made it like this for a better understanding from others and you. Keep that in mind.
Before posting the solution. I will talk to you about more operators:
and
|| means OR
&& means AND
How can this be usefull? Here's a e.g:
Code:
if(a == b && a == c) //means: if a is equal to b and also equal to c. if you use || in here it would be: if a is equal to b OR a is equal to c.
{
//CODE
}
*Here's one solution:
Code:
using System;
namespace MpghTUTSolution1
{
class Program
{
static void Main()
{
Console.WriteLine("You will need to Insert 2 Numbers:");
Console.WriteLine("First Number:");
int aNum = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Secound Number:");
int bNum = Convert.ToInt32(Console.ReadLine());
if (aNum != bNum) //means they are different
{
if ((aNum < 20 && aNum > 0) || (bNum < 20 && bNum > 0))
{
int result = 0;
if (aNum > bNum)
{
result = aNum - bNum;
Console.WriteLine("The biggest number is: " + aNum + " and the difference is " + result);
}
else
{
result = bNum - aNum;
Console.WriteLine("The biggest number is: " + bNum + " and the difference is " + result);
}
}
else
{
Console.WriteLine("Invalid Size Numbers, the Program will end");
}
}
else
{
Console.WriteLine("Numbers are equal, the Program will end");
}
Console.ReadKey(); //to stop the program so you can see the text / result.
}
}
}
*Sorry for the sucky english*
*STILL WORKING ON IT (TUT): Adding Screens later, and how to create a project.*