Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: C# Tutorial - Hello, World (part of a series of tutorials I'm working on)  (Read 1200 times)

0 Members and 1 Guest are viewing this topic.
C# Tutorial - Hello, World (part of a series of ...
« on: February 27, 2007, 01:35:19 pm »
  • Minalien
  • *
  • Reputation: +10/-1
  • Offline Offline
  • Gender: Female
  • Posts: 2119
http://roi.dayjo.org/e107_plugins/content/content.php?content.6

This 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.

Quote from: C# Tutorial - Hello, World!, by Madilim Kagalakan
Step I: The Code
Code: C
  1. using System;
  2.  
  3. namespace CSTutorial
  4. {
  5.      class Program
  6.      {
  7.           static void Main( string[] args )
  8.           {
  9.                Console.WriteLine( "Hello, World!" );
  10.  
  11.                Console.ReadKey();
  12.           }
  13.      }
  14. }

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
Code: C#
  1. using System;
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)

Code: C
  1. namespace Tutorial1
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.

Code: C
  1. class Program
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.
Code: C
  1. string potion_name = "Magic Potion";
  2. int potion_health_inc = 25;
Or, you could (and should) do it by defining a class, as in the following example:
Code: C
  1. class Potion
  2. {
  3.      public string Name;
  4.      public int HealthInc;
  5. }
Then define your potion by using
Code: C#
  1. Potion MagicPotion = new Potion();
  2. MagicPotion.Name = "Magic Potion";
  3. 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.

Code: C
  1. 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.

Code: C
  1. 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" ); )

Code: C
  1. Console.ReadKey();
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 :D
(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
Quote
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.
  • Development Blog
Re: C# Tutorial - Hello, World (part of a series...
« Reply #1 on: March 05, 2007, 04:48:03 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 413
Oh cool. Need a good C# tutorial. It seems every time I learn, I can make a program say 'Hello World' then I get bored.
This had better be interesting :P
Logged
Re: C# Tutorial - Hello, World (part of a series...
« Reply #2 on: March 05, 2007, 08:06:07 pm »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6629
Hehe, you've even done one of FusionScript, kick ass.

http://roi.dayjo.org/e107_plugins/content/content.php?content.7

although this is wrong;

Code: [Select]
[NativeFunctionInfo("ReadLine", "string", "")]
private string ReadLine( ScriptThread fsThread )
{
     // Return the line
     return( Console.ReadLine() );
}

It should be.


Code: [Select]
[NativeFunctionInfo("ReadLine", "string", "")]
private void ReadLine( ScriptThread fsThread )
{
     // Return the line
     thread.SetReturnValue( Console.ReadLine() );
}
« Last Edit: March 05, 2007, 08:09:00 pm by Infinitus »
Logged
Re: C# Tutorial - Hello, World (part of a series...
« Reply #3 on: March 06, 2007, 05:34:02 pm »
  • Minalien
  • *
  • Reputation: +10/-1
  • Offline Offline
  • Gender: Female
  • Posts: 2119
Sorry about that, Infinitus, I was at school and couldn't test it, but anyways <_<;
Fixing now <_<;
Logged
Quote
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.
  • Development Blog
Re: C# Tutorial - Hello, World (part of a series...
« Reply #4 on: March 13, 2007, 03:04:13 pm »
  • Txet Lanosrep
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 700
Great, I'm just starting to learn C# and I knew most of that already, but it's good to review.  What I need now is a game that I could look through and just learn how to basically program a game.  Do you think you next tutorial could be just that?  Like a simple tic tak toe game, or the like?  That would help me and probably anybody else new to C#.
Logged

Retro
  • TITANIC
Re: C# Tutorial - Hello, World (part of a series...
« Reply #5 on: March 13, 2007, 03:58:46 pm »
  • Minalien
  • *
  • Reputation: +10/-1
  • Offline Offline
  • Gender: Female
  • Posts: 2119
I could make a step-by-step tutorial on programming a PONG or BREAKOUT styled game, if you'd like, but it'll have to wait until I have some more free time
Logged
Quote
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.
  • Development Blog
Re: C# Tutorial - Hello, World (part of a series...
« Reply #6 on: March 13, 2007, 04:02:07 pm »
  • Txet Lanosrep
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 700
Sounds great, anything thats simple.  Yeah don't feel like you have to just because I asked, I just thought if you needed an idea for your next tutorial that could be one.
Logged

Retro
  • TITANIC
Re: C# Tutorial - Hello, World (part of a series...
« Reply #7 on: March 13, 2007, 04:06:05 pm »
  • Minalien
  • *
  • Reputation: +10/-1
  • Offline Offline
  • Gender: Female
  • Posts: 2119
Believe me, I like helping people get into game design, particularly if they want to do something more than Game Maker or some crap like that
Logged
Quote
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.
  • Development Blog
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.03 seconds with 68 queries.

anything