Introduction to Programming with C#
Curtis Bennett xnagamemaking.com
Objectives
Learn the basic concepts of programming Set up Visual C# Express and XNA Create a simple C# program
What is Programming?
Tell the computer what to do. Give instructions and manage data. Write code – series of instructions.
Computer Languages
Computers only understand binary (zeroes and ones) but we write in higher level languages that are translated into binary (eventually). Syntax – specific way to write in a language C, C++, Java, Visual Basic, C#
Compiler
Translates code into a program. Takes computer code in a language (C#) and changes it into code closer to what the machine can understand (byte code). to build or compile a program – change computer code to byte code. Visual C# is our compiler
Types of Languages
Structured – Procedural Object-Oriented (OO) List based, relational. (Much rarer than Structured and OO)
Structured – Procedural
All computer code runs in a very linear fashion. Programs run just first statement after the other. C, Basic
Object-Oriented
Arranges programs in a different way. Much closer to real life. We cover what objects are later. C++, Smalltalk, Java, C#
C# Procedural Object-Oriented
C# is our language. C# is OO but inside Objects code is put in little procedural sections (blocks). At first we won’t worry about Objects.
What is XNA?
XNA is some additional libraries Microsoft made for developing games. Not part of C#, but an addition. Won’t cover it until Part II.
Getting Visual C# and XNA
Goto:
http://creators.xna.com/Education/newtoxna.a spx
Download and install Visual C# Download and install XNA
First C# Program
Will print out “Hello World!” on the screen. Console Program – just text.
First C# Program
Start XNA Game Studio Express File->New Project Select “Console Application” icon. In name type “HelloWorld” and click OK. File->Save All – Can choose a place to put it.
First C# Program
Add the following highlighted text into the program:
using System; using System.Collections.Generic; using System.Text; namespace HelloWorld { class Program { static void Main(string[] args) {
Console.WriteLine("Hello World!");
} } }
First C# Program – Running
After the change to the program, goto Debug->Start Debugging or hit F5. Displayed is a console. Press any key for the program to exit. If it didn’t run correctly make sure the changes in the previous slide were typed in exactly as shown.
First C# Program – Line by Line
C# gives a lot of different instructions we can use, but we want to tell C# which of the instructions we'll be using. We don't want C# to give us every available instruction, since we won't use most of them and it adds overhead to the program to say we'll use more instructions then we need. The first part of the program tells C# what instructions we'll use:
using System; using System.Collections.Generic; using System.Text;
First C# Program – Line by Line
Note that each of the commands ends with a semicolon. The semicolon is a way of telling C# that an instruction is complete. The amount of whitespace (spaces, tabs, returns) after an instruction doesn't matter, that the semicolon is there is all that counts.
First C# Program – Line by Line
C# defines blocks of code by brackets {}. The previous code put everything into three blocks. First block defines a namespace block, second block class Program defines an object. The final block static void Main(string[] args) defines a function (or method), a group of statements that together form a common task. Main is the entry point for the program. When we start the program it starts running instructions from Main.
First C# Program – Line by Line
These two lines do the work:
Console.WriteLine("Hello World!"); Console.ReadKey();
They each tell the console something to do (print and pause until a key is pressed). The “Hello World!” Is in quotations because it is some string – written text that’s not computer code.
Comments
Statements that are ignored by the compiler. Single line comment //
// Print out Hello World! Console.WriteLine("Hello World!");
Multiline comment /* … */
/* Print out Hello World! */ Console.WriteLine("Hello World!");
Documentation comment ///
Next Time
Look at storing and changing data. More complex C# program.