C# Tutorial by: Brinuz @ MPGH.
First of all, this is not C++!!! and this is not exactly the best language to make hacks, etc.
Reposting because the old one (2 weeks older), I am now unable to edit it and add things like i said i would, this is why im Reposting, and Also, i request Why06 if he can change the link of the one in the C++ Tut List.
Thanks everyone.
Old one: https://www.mpgh.net/forum/31-c-c/104...s-keeping.html
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 OR ...
- Select Console Application
- Call it w/e you want (Default: ConsoleApplication1)
- Click Ok
- There you go, a project for you to start with
Ok, before starting to post Codes of lines and lines, you should know a few basic things to code:
---------*** Chapter:Getting Started ***---------
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.
---------*** Chapter: Start ***---------
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...
*************************
---------*** Chapter: IF (Comparation) ***---------
*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:
---------*** Chapter: Something for you 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 THIS BY URSELF, DO IT WITHOUT LOOKING TO THE SOLUTION WITHOUT THE PROGRAM BEEING FINISHED!!
-You can check what you have read already.
-You Shouldnt check the solution without finishing.
-Try not to Copy&Paste old E.g's.[/CENTER]
*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.
}
}
}
---------*** Chapter: Cicles ***---------
*Ok, let's say that you want to do something on your code that will need to repeat, and repeat and repeat until the user stops it, or until the user inserts what you want, or do what you want. There is a Solution!!! Cicles!
We have a thing called:
Do-While, which will DO <something> WHILE <something>
Code:
Do
{
//code
}
While(Condition)
Here's a Simple E.g:
Code:
using System;
namespace Cicles
{
class Program
{
static void Main()
{
Console.WriteLine("Welcome to the little Do-While Cicle try it Program.");
Console.WriteLine("Insert a Fixed Number: (anything)");
int fixedNum = Convert.ToInt32(Console.ReadLine());
int biggerNum = 0;
do //Will do the code inside {...} WHILE the biggerNum is lower then the Fixed one.
{
Console.WriteLine("Insert a Bigger Number then the fixed one:");
biggerNum = Convert.ToInt32(Console.ReadLine());
}
while (biggerNum < fixedNum); //Isn't Do-While obvious? Do <Something> While <Something>
//Lets say that I will: Do <Eat Something> While <I'm Hungry>.
//The Condition should be Inside the ( ... );
Console.WriteLine("Gratz! If you reached this is because you inserted a bigger number (or should)");
}
}
}
*There is another Cicle called: While
Code:
while(Condition)
{
//This will do basiclly the same as the one above, with a a small (or BIG..) difference:
//The above one will Always start the cicle.. whenever what he did before without checking the condition Before, so basiclly, the Above one, does the Comparation at the END, and this one posted here, will do the comparation at the start of the while, which you will understand better at the future when you do lots of things by urself.
}
*And There is even another Cicle called: For.
*This is probabily one of the most important things that you will learn or learned by now...
*A Program that will type every number from 0 to 20:
Code:
using System;
namespace ForAPP
{
class Program
{
static void Main()
{
int x;
Console.WriteLine("-- Numbers from 0 to 20 --");
Console.WriteLine("");
for (x = 0; x <= 20; x = x + 1)
{
Console.WriteLine("Number " + x);
}
}
}
}
I know, you probably look at it and you get like: "Wth is this?". Im going to explain.
For Works like this:
I know it's quite wierd, and hard to understand, but i'm sure in a while you will get used to it, until then, use do-while, or while
****** STILL EDITING******
*Sry for the sucky English*
Reposting because the old one (2 weeks older), I am now unable to edit it and add things like i said i would, this is why im Reposting, and Also, i request Why06 if he can change the link of the one in the C++ Tut List.
Thanks everyone.