Lesson 3 — Language Rules
C# Syntax
C# syntax combines Java-style strictness with modern ergonomic improvements like expression bodies, string interpolation, and pattern matching.
CSHARP — Expression bodies
// Standard statement — ends with semicolon:
int age = 21;
Console.WriteLine($"Age: {age}");
// Expression-bodied method (no braces, no return keyword):
static int Square(int n) => n * n;
// Standard equivalent:
static int SquareLong(int n) { return n * n; }
Naming Conventions
| Identifier | Convention | Example |
|---|---|---|
| Classes, Methods, Properties | PascalCase | CalculateTotal() |
| Local variables, parameters | camelCase | totalAmount |
| Private fields | _camelCase | _balance |
| Constants | PascalCase | MaxRetries |
| Interfaces | IPascalCase | IPrintable |
CSHARP — Comments and strings
// XML documentation comment — Visual Studio renders in IntelliSense:
/// <summary>Returns a formatted greeting.</summary>
/// <param name="name">The learner's full name.</param>
/// <returns>A greeting string.</returns>
public static string Greet(string name) => $"Sawubona, {name}!";
// Verbatim string — backslashes are literal:
string path = @"C:\Users\Thandi\Documents";
// Raw string literal (C# 11+):
string json = """
{ "name": "Thandi", "course": "C#" }
""";
Practice Task
Your Turn
Write a MyProfile class: private _birthYear field, Name property (PascalCase), Age property calculated from the current year, and a PrintProfile() method using interpolation. Add XML doc comments on the class and method.
Common Mistakes
- Using camelCase for method names — C# methods are PascalCase.
- Not prefixing private fields with underscore — C# convention is
_fieldName. - Forgetting semicolons on statements.
- Mixing C# and Java conventions in the same codebase.
Professional Tip
When you join a C# team, check their style guide first. Many South African enterprise teams follow Microsoft's official C# coding conventions document.
Mini Quiz
What is the correct casing for a C# method name?
C# methods, properties, and classes use PascalCase. This differs from Java where methods use camelCase.