|
« on: February 27, 2007, 01:35:19 pm »
|
|
http://roi.dayjo.org/e107_plugins/content/content.php?content.6This is the first part of a series of C# tutorials I'm working on. At the moment, I'm just going over some of the basics, but I dont plan on continuing my basics tutorials, as I want to start teaching using C# for game development. Step I: The Code
using System; namespace CSTutorial { class Program { static void Main( string[] args ) { Console.WriteLine( "Hello, World!" ); Console.ReadKey(); } } }
Step II: Enter & Compile Enter the preceding code into your IDE (Integrated Development Environment; I recommend Microsoft Visual C# 2005 Express Edition (It's Free)), then Debug the program (Press F5 in MSVC#).
Press any key to exit out of the program.
Step III: Code Disection This bit of code makes a large difference in how your code looks. Using statements let you use contents of a namespace without having to declare the namespace first (ie: instead of System.Console.WriteLine, you can use Console.WriteLine because we're using the System namespace)
Here, we're defining a namespace of our own. Think of namespaces like closets. You have to go into the closet to get the board game you want.
Namespace declarations, such as the one above, allow us to create our own closets in which to store things.
Classes are a big thing in C#, because C# uses Object-Oriented Programming (aka OOP). Classes allow us to create objects.
Think of obect-oriented programming's hierarchy like this: Classes are contained in Namespaces. Where namespaces are containers, Classes are templates for your objects. Let's use game development as an example.
Say you wanted to create a magic potion. You COULD create a bunch of variables with all of the potion's information.
string potion_name = "Magic Potion"; int potion_health_inc = 25;
Or, you could (and should) do it by defining a class, as in the following example:
class Potion { public string Name; public int HealthInc; }
Then define your potion by using
Potion MagicPotion = new Potion ();MagicPotion.Name = "Magic Potion"; MagicPotion.HealthInc = 25;
For a single item, it seems like it's easier to use the first method. However, when you get into having a lot more potions, it becomes far easier and far more organized to use a class.
static void Main( string[] args )
This bit of code creates a function. In this case, a static function (I'll explain what that means in a later tutorial). This function MUST be defined as static void Main() and MUST be present in your application for it to run. This is the first function called when the program starts up.
Console.WriteLine( "Hello, World!" );
This accesses the Console function WriteLine, which writes a line of text to the console (confusing, I know, but you'll understand after you've run the program ;P
All your text that is to be output MUST be inside the quotation marks, and know that the quotation marks will not show up in the outputted text. Do not put a double-quote (") inside message. If you want to have a double-quote, you need to do a character escape (in other words, it doesn't treat it as a double-quote would normally be treated, it instead outputs a double-quote) by putting a backslash (\) in front of it (ie: Console.WriteLine( "The new angel said \"Hello, World!\" as she popped into existance" ); )
This command is simple; it waits for the user to press a key before continuing on (which, in this case, ends the program).
Step IV: Experiment The next step is EXTREMELY IMPORTANT to your success in programming; experiment. Do trial-and-error, see if you cant solve some problems you may (or may not) run into. If you want any help, feel free to email me (minalien@gmail.com) and I'd be glad to help.
Keep your eyes out for my next tutorial, where we'll go a little bit more in-depth with user input and basic console programming (please keep in mind, when I say console programming, I dont mean game console, I mean the little black window that pops up when you run your program. Dont get your hopes up just yet <_<) [MiNEdit] Hmm... I think it looks nicer on RoI <_<
« Last Edit: February 27, 2007, 08:07:25 pm by 4Sword »
Logged
|
There's such a double standard about religion in the modern world. Catholics can gather, wear white robes, and say "In nomine Patris, et Filii, et Spiritus Sancti" and be considered normal.
But if my friends and I gather, wear black robes, and say "Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn", we're considered cultists.
|