Writing the Code
Clichés need to be honored from time to time, so what’s better than starting with a good old Hello World program? Okay, let’s make it a little more complicated by making it a generalized Hello program, asking the user for a name to send a greeting to.
Open up Notepad, enter the following code, and save it to a file called Hello.cs:
using System;class Program{ static void Main() { Console.Write("Enter your name: "); string name = Console.ReadLine(); Console.WriteLine("Hello " + name); }}
Make sure to respect the case of letters: C# is a case-sensitive language. In particular, if you come from a Java or C/C++ background, be sure to spell Main
with a capital M. Without delving too deeply into the specifics ...
Get C# 4.0 Unleashed now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.