Está en la página 1de 4

Visual C# .

NET - Your rst line of code

le:///home/moon/.mozilla/refox/xn31e8nu.defaul...

Th e only th ing we'l l do with th e code is to write some text to th e screen. But h ere's th e code th at Visual C# prepares for you wh en you first create a Console Application:

For now, ignore th e 3 lines th at start with us ing (we'l l get to th em later in th e book). But th ey add references to in-built code. Th e name s pace line includes th e name of your application. A namespace is a way to group related code togeth er. Again, don't worry about th e term namespace, as you'l l learn about th ese later. Th e th ing th at's important above is th e word clas s . All your code will be written in classes. Th is one is called Program (you can call th em anyth ing you like, as long as C# h asn't taken th e word for itself). But th ink of a class as a segment of code th at you give a name to. Inside of th e class called Program th ere is th is code: static void Main(string[] args) { } Th is piece of code is someth ing called a Me t hod. Th e name of th e Meth od above is Main. Wh en you run your programme, C# looks for a Meth od called Main. It uses th e Main Meth od as th e starting point for your programmes. It th en executes any code between th ose two curly brackets. Th e blue words above are all special words - keywords. You'l l learn more about th em in later ch apters. But position your cursor after th e first curly bracket, and th en h it th e enter key on your keyboard:

Th e cursor automatically indents for you, ready to type someth ing. Note wh ere th e curly brackets are, th ough , in th e code above. You h ave a pair for clas s Program, and a pair for th e Main meth od. Miss one out and you'l l get error messages. Th e single line of code we'l l write is th is (but don't write it yet): Cons ole .Writ e Line ("He llo C Sharp!"); First, type th e letter "C". You'l l see a popup menu. Th is popup menu is called th e IntelliSense menu. It tries to guess

1 of 4

10/10/2010 01:36 AM

Visual C# .NET - Your rst line of code

le:///home/moon/.mozilla/refox/xn31e8nu.defaul...

wh at you want, and allows you to quickly add th e item from th e list. But it sh ould look like th is, after you h ave typed a capital letter "C": C# 2010

Olde r ve rs ions of C#

Th e icon to th e left of th e word Cons ole on th e list above means th at it is a Class. But press th e Enter key on your keyboard. Th e word will be added to your code:

Now type a full stop (period) immediately after th e word Console. Th e IntelliSense menu appears again:

2 of 4

10/10/2010 01:36 AM

Visual C# .NET - Your rst line of code

le:///home/moon/.mozilla/refox/xn31e8nu.defaul...

You can use th e arrow keys on your keyboard to move up or down th e list. But if you type Writ e and th en th e letter L of Line , IntelliSense will automatically move down and select it for you:

Press th e Enter key to add th e word Writ e Line to your code:

Now type a left round bracket. As soon as you type th e round bracket, you'l l see th is:

3 of 4

10/10/2010 01:36 AM

Visual C# .NET - Your rst line of code

le:///home/moon/.mozilla/refox/xn31e8nu.defaul...

WriteLine is anoth er Meth od (A Meth od is just some code th at does a particular job). But th e yellow box is telling you th at th ere are 19 different versions of th is Meth od. You could click th e small arrows to move up and down th e list. Instead, type th e following: "He llo C Sharp!" Don't forget th e double quotes at th e start and end. Th ese tell C# th at you want text. Your code will look like th is:

Now type a righ t round bracket:

Notice th e red wiggly line at th e end. Th is is th e coding environment's way of telling you th at you've missed someth ing out. Th e th ing we've missed out is a semicolon. All complete lines of code in C# must end with a semicolon. Miss one out and you'l l get error messages. Type th e semicolon at th e end and th e red wiggly line will go away. Your code sh ould now look like th is:

Note all th e different colours. Visual C# colour-codes th e different parts of your code. Th e reddish colour between double quotes means th at you want text; th e green colour means it's a Class; blue words are ones th at C# reserves for itself. (If you want, you can ch ange th ese colours. From th e menu bar at th e top, click T ools > Opt ions . Under Environme nt , click Font s and Colors .) Time now to Build and Run your code!

4 of 4

10/10/2010 01:36 AM

También podría gustarte