Tuesday, March 15, 2011

Learn gaming in c sharp chapter 2

Learning the basics of a program
using System;
using System.Collections.Generic;
using System.Text;

namespace Tutorial1
{
    class Program
    {       
        static void Main(string[] args)
        {
           
        }
    }
}
The first thing you might notice is the different colors.  Dark blue signifies a code/command that C# recognizes.  Light blue indicates a class, while the regular text is for user defined names.

Skipping those "using" statements for a moment you'll see:

namespace"namespace" is an equivalent to a folder in windows.  Everything in that folder is accessible if that "folder" is open.  Just like in windows a folder can folders inside of it; Namespaces can have other namespaces and classes inside of it as well.  While we'll cover this more later on, it just good to remember that comparison.

classClass is a dual purpose object.  It too acts somewhat like a "folder", however instead of just holding more "folders" it holds methods, variables.... pretty much everything belongs inside of a class with only a few exceptions that we'll cover much later.  Classes too will be covered in depth later.  Just remember that while the class is accessible from the namespace, the things inside of the class are only accessible when the Class is "opened".  Don't worry if this is over your head right now.  It will be made clear in lessons to come.


Methods and MainThe "static void Main()" is a method.  A method can be the work horse of your program.  I say "can" because you can use additional classes as your workhorse if you so choose.  This is a design question that you will decide later. 

Now the "Main" method is a special method.  It is the entry point for your program in C#.  When your program runs the first thing it does is checks Main to see where it should go next.  Right now it goes nowhere.

Programming is a lot like writing a "Choose your own adventure book".  Start here, then to here, then to there......etc, no matter where it's written or what order.  Now if you've never read a choose your own adventure book, all I can do is compare it to a road map or making your computer do a scavenger hunt following your list of instructions.... or your code.

Now's also a good time to mention that C# is case sensitive.  Main is different from main.  “tutorial1” will produce an error while “Tutorial1” will signify our namespace.  Also now is a good time to introduce "Intellisense".  Intellisense will provide a reasonable option for what you have begun typing.  When writing your own code it's a good way to remind you if you've declared a variable or if something is accessible from where you are.  Right now it might not seem like it right now but in the future it will help with spelling and coding a lot.

ScopeMouthwash is a good thing but that's not what we're talking about here.  The {} brackets signify the "scope" of that method, class namespace etc.  Everything within the curly brackets belongs to that item.  A lot of things can have them, but right now we're looking at Namespaces Classes and Methods.  Look at how they line up.  Visual C# Express will try to do this automatically for you if it can but try your best to keep it straight.  When you write something that needs them, put them in first, then code.  Getting your {} mixed up in long code will throw you off tremendously.  We've all done it, you will too, so keep a look out for it.

The Using StatementsOkay now back to the using statements.  They are calling up other namespaces.  C# comes with built in Namespaces and classes inside of them.  System includes a lot and later on Microsoft.Xna.Framework will include more.  How do you know what's what and what to use?  Practice and using your MSDN help files.  Go to [Help->How do I-> then search the index for namespaces] for a list of the available namespaces and the classes inside of them.  For now our basic ones will do but you'll want to become familiar with all of them eventually.  Yes there's a lot there, yes the functionality of it boggled me too once but you'll get it with time.

using statements simply mean "Hey, I want to use the classes in this namespace(folder)".  That's it.  You can even write your own specialized classes and use your own namespaces eventually.

What about Programming?Ah yes I did say that, so type this inside of the Main() scope

Console.WriteLine("Hello World!");
Console.ReadLine();

Your program should look like this:

namespace Tutorial1
{
    class Program
    {       
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Console.ReadLine();
        }
    }
}

You probably got a shot of Intellisense working there.  You could have hit tab, or ".", or a "(" when it gave you the right option to make it quicker but you don't have to.  You also can hit the up and down arrows to scroll through all the options.

Now hit F5.

You should have seen:

Hello World!

No comments:

Post a Comment

Your comment is pending for approval

AngularJS Basics - Part 1

                                                                  AngularJS What is AngularJS ·          Framework by googl...