Every programming language has its own writing rules: how statements are separated, how blocks of code are grouped, how things are named. Taken together, we call these rules syntax. In this lesson you'll learn the fundamental syntax rules of C# — case sensitivity, semicolons, braces, comments, naming conventions, and valid identifiers — which you'll use in absolutely every piece of code you write from now on. Mastering these rules well will save you from most of the compilation errors that tend to frustrate beginners.
Contents
- Case sensitivity
- Semicolons, braces, and code blocks
- Comments:
//,/* */, and/// - Naming conventions: PascalCase and camelCase
- General structure of a C# file
- Valid identifiers
Case sensitivity
C# is a case-sensitive language. This means that myVariable, MyVariable, and
MYVARIABLE are considered three different names by the compiler. This affects everything:
variable names, method names, class names, and even the language's own keywords.
Console.WriteLine("This compiles correctly");
// console.writeline("This does NOT compile"); // lowercase 'console' doesn't existThis detail is very important because it's a common source of errors for people coming from other contexts (for example, some file systems or languages that don't distinguish uppercase from lowercase). In C#, you must always respect the exact case of every keyword and every name you use.
Semicolons, braces, and code blocks
The semicolon ;
In C#, (almost) every statement ends with a semicolon ;. The semicolon tells the compiler
"this statement ends here, the next one is independent":
Forgetting the semicolon is probably the most common syntax error when starting to program in C#. The compiler will warn you with an error message pointing to the exact line.
Braces { } and code blocks
Braces delimit blocks of code: a set of statements treated as a single unit. We already saw
in the previous lesson that a namespace, a class, and a method are each a block delimited by
braces. The same will happen, later in the course, with control-flow blocks (if, loops, etc.
— Module 2).
A widely spread convention (and the one we'll follow in this course) is indentation: indenting (adding spaces or tabs to) the code inside a block, so it's visually clear what belongs to what. C# doesn't require code to be indented in order to compile — unlike Python, for example — but well-indented code is far easier to read and maintain.
// Correctly indented code (recommended)
if (true)
{
Console.WriteLine("Correctly indented block");
}
// Non-indented code (compiles the same, but is hard to read)
if (true) {
Console.WriteLine("Non-indented block");
}Don't worry yet about what if means; we'll study it in detail in Module 2. Here we're only
interested in the form, not the content.
Comments: //, /* */, and ///
Comments are pieces of text the compiler completely ignores: they exist solely so that the people reading the code (including your future self) understand what it does or why it was written a certain way. C# offers three types:
| Type | Syntax | Typical use |
|---|---|---|
| Single-line comment | // text |
Brief clarifications next to a line of code |
| Block comment | /* text */ |
Comments spanning several lines, or temporarily disabling a chunk of code |
| Documentation comment | /// text |
Documenting classes and methods so that external tools (such as the editor itself) can display that documentation as help |
Single-line comment
// This is a single-line comment
Console.WriteLine("Hello"); // You can also comment at the end of a line of codeBlock comment
/*
This comment spans
several lines of text.
Very useful for long explanations.
*/
Console.WriteLine("BiblioTech");Documentation comment
/// <summary>
/// Shows a welcome message for the BiblioTech system.
/// </summary>
Console.WriteLine("Welcome to BiblioTech!");Documentation comments (///) use a special syntax based on XML tags, such as <summary>.
Although in this module we don't yet write our own methods or classes (that comes in Module 3),
it's important that you recognize this syntax: many editors automatically show the content of
these comments as a popup hint when you use a method, and in professional projects documenting
public code this way is a highly valued practice.
Naming conventions: PascalCase and camelCase
A naming convention is an agreement (not a rule enforced by the compiler) about how to write the names of things in code, so it's consistent and easy to read for anyone familiar with the language. In C#, the community and the .NET team itself very consistently follow these two conventions:
| Convention | Rule | Example | Used for |
|---|---|---|---|
| PascalCase | Every word starts with a capital letter, no separators | BookTitle, PageCount, Program |
Class, method, and property names |
| camelCase | The first word is lowercase, subsequent words start with a capital | bookTitle, pageCount |
Local variable and parameter names |
Example comparing both styles using BiblioTech data:
// camelCase: local variables
string bookTitle = "One Hundred Years of Solitude";
int pageCount = 471;
bool isAvailable = true;
// PascalCase: will be used for classes and methods (Module 3), already seen in 'Program' and 'Console'
Console.WriteLine(bookTitle);Although the compiler doesn't force you to follow these conventions (it would compile just the
same if you capitalized things differently), following them is a fundamental professional
practice: it makes your code recognizable and consistent with the rest of the .NET ecosystem,
including all the standard libraries (Console, WriteLine, etc., already follow PascalCase).
Other common recommendations
- Names should be descriptive:
pageCountis better thannorpc. - Underscores
_as a separator within a name are generally avoided (for example,bookTitleis preferred overbook_title), although an underscore is sometimes used as a prefix in certain contexts (for example, in a class's private fields, something we'll see in Module 3). - Accented letters or special characters aren't used in variable names, to avoid compatibility problems between keyboards, editors, and systems.
General structure of a C# file
Recalling what we saw in the previous lesson, a typical .cs file (in its classic form) follows
this general structure, from the outside in:
using System; // 1. Using directives
namespace BiblioTechConsole // 2. Namespace
{
class Program // 3. Type declaration (class)
{
static void Main(string[] args) // 4. Member (method)
{
// 5. Statements
Console.WriteLine("Welcome to BiblioTech!");
}
}
}| Step | Element | Explanation |
|---|---|---|
| 1 | using directives |
Indicate which external namespaces are going to be used in the file. Always placed at the top. |
| 2 | Namespace | Groups the project's code under a common name, avoiding conflicts with other libraries. |
| 3 | Type declaration | A class (or, later in the course, a struct, an interface, a record...) containing the program's members. |
| 4 | Members | Methods, and later (Module 3) properties and fields, defined inside the type. |
| 5 | Statements | The code that actually runs, inside a method. |
As we saw in the previous lesson, in modern projects with top-level statements, steps 2, 3, and
4 become implicit: you only write step 1 (if any extra using is needed) and step 5 directly.
The compiler still generates that same structure "underneath".
Implicit usings
You may recall that, when we looked at the generated .csproj file in the previous lesson, the
<ImplicitUsings>enable</ImplicitUsings> option appeared. This option, available in modern .NET
projects, makes certain very common namespaces (such as System, where Console lives)
automatically available throughout the whole project, without needing to write using System; at the top of every file. That's why, in the examples from previous lessons, we've
been able to use Console.WriteLine directly without any visible using line.
Valid identifiers
An identifier is the name you give to any element in your code: a variable, a method, a class, etc. C# imposes some mandatory rules (if they aren't followed, the program simply won't compile):
- It must start with a letter or an underscore
_(never with a number). - The remaining characters can be letters, digits, or underscores.
- It can't exactly match a reserved keyword of the language (such as
class,namespace,if,int...), unless written with the special@prefix (a very uncommon technique, reserved for exceptional cases). - It can't contain spaces or symbols like
-,., accented letters, etc.
| Identifier | Valid? | Reason |
|---|---|---|
bookTitle |
Yes | Starts with a letter, no special characters |
_isbn |
Yes | Starts with an underscore, which is valid |
pageCount2 |
Yes | Digits are allowed, as long as they aren't the first character |
2pageCount |
No | Starts with a digit |
book-title |
No | The hyphen - isn't allowed in identifiers |
título |
Not recommended | Although some modern compilers accept it, it's avoided by convention and for compatibility |
class |
No | Matches a reserved keyword of the language |
These rules apply not only to variables but, as we'll see in later modules, to the names of methods, classes, properties, and any other element we define ourselves.
Common Mistakes and Tips
- Forgetting the semicolon: we've already mentioned this, but it bears repeating because it's by far the most frequent mistake when starting out.
- Mixing up uppercase and lowercase without noticing: writing
Console.writeline(...)instead ofConsole.WriteLine(...)causes a compilation error, since C# is case-sensitive. - Nesting braces without indenting: even though it compiles the same, non-indented code becomes very hard to debug as the program grows. Get your editor used to indenting automatically (most do it when you press Enter).
- Using non-descriptive names: variables like
x,a1,tempmake the code harder to understand later on. Always prefer descriptive names likebookTitleorisAvailable. - Confusing convention with obligation: PascalCase and camelCase are conventions, not compiler rules; but following them is what's expected of any professional C# code, and we'll follow them throughout the course.
- Tip: when the compiler points out a syntax error on a line, also check the line before it: often the real error (like a missing semicolon) is right before where the problem is reported.
Exercises
-
From the following list of identifiers, indicate which are valid in C# and, for the ones that aren't, explain why:
pageCount,3isbn,_title,full name,namespace,Author2. -
Rewrite the following code fragment, correctly applying naming conventions (camelCase for variables) and proper indentation:
string TITLE = "Don Quixote"; int Pages=863; bool Available=true; Console.WriteLine(TITLE); -
Add to your corrected fragment from the previous exercise: a single-line comment explaining what the
pagesvariable is for, and a block comment (/* */) at the top of the file briefly explaining what the whole program does.
Solutions
-
pageCount→ Valid (starts with a letter).3isbn→ Not valid (starts with a digit)._title→ Valid (starts with an underscore).full name→ Not valid (contains a space).namespace→ Not valid (it's a reserved keyword of the language).Author2→ Valid (starts with a letter, contains a digit that isn't the first character).
-
Corrected version:
string title = "Don Quixote"; int pages = 863; bool available = true; Console.WriteLine(title);TITLE,Pages, andAvailable(capitalized, as in PascalCase) were changed totitle,pages, andavailablein camelCase, appropriate for local variables; a space was also added around the=sign for readability, although the compiler would accept both forms. -
Sample solution:
/* BiblioTech sample program. Displays the title of a book from the catalog. */ string title = "Don Quixote"; int pages = 863; // Total number of pages in the book bool available = true; Console.WriteLine(title);
Conclusion
In this lesson you learned the syntax rules that govern any C# program: the distinction between uppercase and lowercase, the use of semicolons and braces to delimit statements and blocks, the different types of comments, the PascalCase and camelCase naming conventions, the general structure of a file, and the rules for a valid identifier. With this foundation you can now read and write syntactically correct, well-organized C# code.
In the next lesson, Variables and Data Types, we'll put these naming rules into practice by declaring actual variables with different data types — numbers, text, boolean values — using as an example the data of a BiblioTech book: its title, its ISBN, its page count, and whether it's available or not.
C# Programming Course
Module 1: Introduction to C#
- Introduction to C#
- Setting Up the Development Environment
- Hello World Program
- Basic Syntax and Structure
- Variables and Data Types
- Arrays and Strings
Module 2: Control Structures
Module 3: Object-Oriented Programming
- Classes and Objects
- Methods
- Constructors and Destructors
- Inheritance
- Polymorphism
- Encapsulation
- Abstraction
- Structs and Records: Value Types and Reference Types
Module 4: Advanced C# Concepts
- Interfaces
- Delegates and Events
- Pattern Matching and Modern C# Features
- Generics
- Collections
- LINQ (Language Integrated Query)
- Asynchronous Programming
Module 5: Working with Data
- File I/O
- Serialization
- Database Connectivity
- Entity Framework
- Working with JSON and Consuming REST APIs
Module 6: Advanced Topics
- Reflection
- Attributes
- Dynamic Programming
- Memory Management and Garbage Collection
- Multithreading and Parallel Programming
Module 7: Building Applications
Module 8: Best Practices and Design Patterns
- Coding Standards and Best Practices
- Design Patterns
- Dependency Injection and Inversion of Control
- Unit Testing
- Code Review and Refactoring
