Showing posts with label Learn C sharp Gaming Chapter 3. Show all posts
Showing posts with label Learn C sharp Gaming Chapter 3. Show all posts

Tuesday, March 22, 2011

Learn C sharp Gaming Chapter 3


Your First C# Program
There is an ancient tradition (okay it’s not that old) in computer programming that says
that your first program in any language should be a “Hello World” program, a program
that simply prints out a welcome message on your computer.
On the CD for this book you will find a demo entitled “HelloCSharp.”You can find it in the
/Demos/Chapter02/01-HelloCSharp/ directory. The HelloCSharp.cs file in that directory
contains the code for the program; you can open it up in any text editor or Visual Studio
and view it. The code should look like this:
class HelloCSharp
{
static void Main( string[] args )
{
System.Console.WriteLine( “Hello, C#!!” );
}
}
At first glance, you can see that this is about four or five lines longer than you could write
it in C or C++; that’s because C# is a more complicated language.

Classes
C# is an object-oriented programming language, which may not mean anything to you at
this point. “A Brief
Introduction to Classes,” but for now, all you need to know is that C# represents its programs
as objects.
The idea is to separate your programs into nouns and verbs, where every noun can be represented
as an object. For example, if you make a game that has spaceships flying around,
you can think of the spaceships as objects.
A class in a C# program describes a noun; it tells the computer what kind of data your
objects will have and what kind of actions can be done on them. A spaceship class might
tell the computer about how many people are in it, how much fuel it has left, and how fast
it is going.
The Entry Point
Every program has an entry point, the place in the code where the computer will start execution.
In older languages like C and C++, the entry point was typically a global function
called main, but in C# it’s a little different. C# doesn’t allow you to have global functions,
but rather it forces you to put your functions into classes, so you obviously cannot use the
same method for a C# entry point. C# is like Java in this respect; the entry point for every
C# program is a static function called Main inside a class.Every C# program must have a class that has a static Main function; if it doesn’t, then the
computer won’t know where to start running the program. Furthermore, you can only
have one Main function defined in your program; if you have more than one, then the computer
won’t know which one to start with.
note
Technically, you can have more than one Main function in your program, but that just makes things
messy. If you include more than one Main, then you need to tell your C# compiler which class contains
the entry point—that’s really a lot of trouble you can live without.
Hello, C#!!
The part of the program that performs the printing is this line:
System.Console.WriteLine( “Hello, C#!!” );
This line gets the System.Console class—which is built into the .NET framework—and tells
it to print out “Hello, C#!!” using its WriteLine function.
Compiling and Running
There are a few ways you can compile this program and run it. The easiest way would be
to open up a console window, find your way to the demo directory, and use the commandline
C# compiler to compile the file, like this:
csc HelloCSharp.cs

AngularJS Basics - Part 1

                                                                  AngularJS What is AngularJS ·          Framework by googl...