Your first program, line by line
using, class, method, statement โ every line of the smallest real program explained.
The smallest complete program
using System;
public class Solution
{
public static void program()
{
Console.WriteLine("Hello, Code Journey!");
}
}
Every line has a job:
| Line | Job |
|---|---|
| using System; | make the System namespace's types visible by short name (Console lives there) |
| public class Solution | declare a class โ C# code always lives inside a type |
| public static void program() | declare a method: a named, reusable block of code |
| Console.WriteLine(...) | a statement โ one instruction; note the semicolon |
| { } | braces group code into blocks |
Namespaces are named groups of types โ folders for code. Console is System.Console; using System; lets you write just Console. Fully-qualified System.Console.WriteLine(...) works identically. This course's challenge boilerplate already includes the common using lines; you focus on the code inside.
Strings are text in double quotes. "Hello" is a string literal. Single quotes make a char (one character) โ 'A' and "A" are different types.
Output that isn't one line
Console.Write prints without a newline; Console.WriteLine prints and moves to the next line. Mixing them builds rows:
Console.Write("Score: ");
Console.WriteLine(97); // "Score: 97" on one line
Console.WriteLine(); // empty line
Comments are for humans; the compiler ignores them:
// a single-line comment
/* a block comment
spanning lines */
Write comments for the why, not the what โ the code already says what it does.
Escape sequences
Some characters need a backslash escape inside strings: \n (newline), \t (tab), \" (quote), \\ (backslash). "She said \"hi\"\n" prints She said "hi" and a line break.