Skip to content
Lesson 1 of 12

Step 1 of 6 · Reading · ~2 min

Learn

Basics

Hello, C#

By the end of this lesson you can read the skeleton every C# program in this course starts from, print lines of text with it, and read one line of input.

C# ("see sharp") was created at Microsoft in 2000 by Anders Hejlsberg. It is statically typed and compiled, and it runs on the .NET runtime (on this course's grader, the open-source Mono implementation of it).

The skeleton

using System;

class Program {
    static void Main() {
        Console.WriteLine("Welcome aboard!");
    }
}

Output:

Welcome aboard!

Each piece is there for a reason:

  • using System; - C# organizes code into namespaces. Console lives in the System namespace, and this directive lets you write Console instead of System.Console every time.
  • class Program - all executable code lives inside a class. There are no free-floating functions.
  • static void Main() - the entry point. The runtime finds a method named Main and calls it. It is static because no objects exist yet when the program starts, so the runtime calls it on the class itself. void means it returns nothing.
  • Console.WriteLine("...") - prints the text and then a newline.

Newer .NET lets you skip the class and Main ("top-level statements"). The compiler on the grader does not, so always keep the full skeleton the starter gives you.

Write versus WriteLine

WriteLine ends the line; Write does not. Two Write calls land on the same line:

Console.Write("one");
Console.Write("two");
Console.WriteLine("three");
Console.WriteLine("four");

Output:

onetwothree
four

The grader compares your output exactly, so whether a newline is there matters as much as the letters.

Reading a line

Console.ReadLine() reads one line of input and gives it back as a string, with the newline removed. You keep it in a variable, a named box with a type (string here):

string city = Console.ReadLine();
Console.WriteLine("Welcome to " + city + "!");

If the input line is Oslo, this prints Welcome to Oslo!. The + between strings glues them together (concatenation). Spaces are part of the string: the space in "Welcome to " is what separates the words in the output.

Common mistakes

  • Wrong capitalization. C# is case-sensitive: console.writeline does not compile, and neither does Console.Writeline. The compiler error names the line.
  • A missing semicolon. Every statement ends with ;. A forgotten one gives a compile error that points at the line after.
  • Using Write where you needed WriteLine. The program runs and prints the right letters, but two lines merge into one and the exact comparison fails.
  • Forgetting a space inside the quotes. "Welcome to" + city prints Welcome toOslo.

Your exercise

The starter reads a name for you. Print a greeting for it on one line and a welcome message on the second, exactly as the exercise shows. The most likely mistake is gluing both messages onto one line.

Up nextVariables and TypesBasics

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…