PostC# Tutorial, starting, learning the basics, and keeping...

Posts 1–15 of 21 · Page 1 of 2
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:

Code:
string text;
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:

Code:
||
and
Code:
&&
|| 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.*
Honestly, props to you. I might not be learning C# yet, but so far from what I've read, I've understood every word. Good job, keep it up!


EDIT:
Ooh, just a question. :P
Assuming C# is similar, so I'm assuming unlike C++, you don't need to have.
Code:
return 0;
at the end? :P And also, just wondering. If I were to compile that, would that just quickly flash on my screen and flash off? So instant open/close?

Lol, this is easier to understand since I have some knowledge of C++.
[php]
Console.WriteLine is like cout. Displays output.
Console.ReadLine is cin. Whee, this seems fun. o_o;
[/php]
Quote Originally Posted by crushed View Post
Honestly, props to you. I might not be learning C# yet, but so far from what I've read, I've understood every word. Good job, keep it up!


EDIT:
Ooh, just a question. :P
Assuming C# is similar, so I'm assuming unlike C++, you don't need to have.
Code:
return 0;
at the end? :P And also, just wondering. If I were to compile that, would that just quickly flash on my screen and flash off? So instant open/close?

Lol, this is easier to understand since I have some knowledge of C++.
[php]
Console.WriteLine is like cout. Displays output.
Console.ReadLine is cin. Whee, this seems fun. o_o;
[/php]
He's using void main, meaning either no return at the end, or a return with no value(return
Hey thx a lot for this. Sorry I'm slow getting to this I'll add your tut to the list, hell even though its C#. Goodjob.
very good tutorial! but im already a c# programmer... and a c# pro! lol
Quote Originally Posted by why06 View Post
Hey thx a lot for this. Sorry I'm slow getting to this I'll add your tut to the list, hell even though its C#. Goodjob.
Thanks for that

Quote Originally Posted by Void View Post
Well done. I've read on C# once already since I wanted to start, and I saw something like

Code:
string text = "World!";
console.WriteLine("Hello {0}!",text);
The way you did it puts the string at the end of the sentence, I just wanted to add that. I could be wrong though.
yeap thats correct, but you also also do:

Code:
string text = "MPGH";
console.WriteLine("Hello " + text + " World!");
So the output would be "Hello MPGH World".

And thanks for adding that (forgot).

Quote Originally Posted by crushed View Post
Honestly, props to you. I might not be learning C# yet, but so far from what I've read, I've understood every word. Good job, keep it up!


EDIT:
Ooh, just a question. :P
Assuming C# is similar, so I'm assuming unlike C++, you don't need to have.
Code:
return 0;
at the end? :P And also, just wondering. If I were to compile that, would that just quickly flash on my screen and flash off? So instant open/close?

Lol, this is easier to understand since I have some knowledge of C++.
[php]
Console.WriteLine is like cout. Displays output.
Console.ReadLine is cin. Whee, this seems fun. o_o;
[/php]
like Hell_demon said it is a void main, so there i did not return on it. (void)
It wouldnt flash on the screen if you run it in "Without debug".
In Debug mod, it would flash on the screen, a easy way too stop that is to make for ex: "Console.Readkey();"
oh, and you are correct about comparing it to C++...
Readline & Writeline

PS: i will later today continue the tutorial, and maybe also add Windows APP later.
Quote Originally Posted by kilert View Post
very good tutorial! but im already a c# programmer... and a c# pro! lol
You just said you blow balls at programming, copy+paste/leech code from others and claim it as your own(and asking credit for it).
You also go back 2-3 pages, copy random code to a new project and claim it as your own(forgetting to remove credits if there were any).
You should jump infront of a train, grab a gun and shoot yourself, stab yourself in the eye with the biggest knife you can find, blow men's penisses(not mine please)
And last but not least, GTFO AND LEARN VB, SUITS YOU BETTER GAY COPY PASTER
Well done. I've read on C# once already since I wanted to start, and I saw something like

Code:
string text = "World!";
console.WriteLine("Hello {0}!",text);
The way you did it puts the string at the end of the sentence, I just wanted to add that. I could be wrong though.
HD FTW!

o waiit
namespaces are important
class under the same namespaces are only able to interchange data with each other (same variable, calling of function etc) unless you put external classes under "using" or references
Quote Originally Posted by falzarex View Post
HD FTW!

o waiit
namespaces are important
class under the same namespaces are only able to interchange data with each other (same variable, calling of function etc) unless you put external classes under "using" or references
i know all that stuff and what they do ^^ but saying a bunch of things at the start is not good, people will get confused... mainly if they don't even know what is a class/method/function. That's why i said it wasnt important for now.

btw yeap you are right, and Thanks ^^
sorry double post mpgh lagged
--anywayz
to access objects of a class you use period
e.g HD.Rage(VBfag);
where Rage is an object of HD, in this case a function lol
Yeah anyway XNA tutorials are a good reference in the sense that they explain each line of code in their tutorials that's how I learnt all these stuff anyway just not clear with arrayz
Quote Originally Posted by falzarex View Post
Yeah anyway XNA tutorials are a good reference in the sense that they explain each line of code in their tutorials that's how I learnt all these stuff anyway just not clear with arrayz
i will talk about arrays later too... (i expect to..) and yeap i seen lots of XNA tuts (learning it, or trying to xDD)
i meant the ones on xna creator club site
looking forward to your extended tutorials :P
Updated with:
-Solution (and && , || operators)
Posts 1–15 of 21 · Page 1 of 2
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Talk with us