Chapter 01
Basic Operations and Syntax
Your Progress in this Chapter
0 / 12 units completed
Chapter OverviewBasic Operations and Syntax
To build a powerful numerical engine like SepalSolver, we must first master the "grammar" of C#. C# is a strongly-typed, object-oriented language designed for clarity and performance. Its syntax provides a structured way to express mathematical logic, ensuring that the computer interprets our engineering formulas exactly as intended.
1. The Structure of a C# Program
Every instruction in C# lives inside a Statement, and statements are grouped into Blocks defined by curly braces { }. For our solver, this structure helps us separate different mathematical concerns—like keeping the matrix inversion logic separate from the file-saving logic.
2. Semicolons and Whitespace
In C#, every statement must end with a semicolon ;. This acts as a "terminator," telling the compiler that one instruction is finished and the next is beginning. Unlike some languages, C# ignores extra whitespace, allowing us to format complex multi-line equations in a way that is readable to humans without confusing the machine.
3. Comments for Documentation
Writing code is only half the battle; explaining it is the other. C# provides two main ways to leave notes for yourself and other engineers:
- Single-line (//): Best for quick notes about a specific line.
- Multi-line (/ ... /): Best for describing complex algorithms.
- XML Documentation (///): Used to generate tooltips for your library.
Example 1C#
Examples
Basic Variable Assignment : In this example, we see how to declare a variable and assign it a value. In SepalSolver, we use descriptive names to ensure that anyone reading the code understands that tol represents a tolerance and maxIter represents an iteration limit.
Example 2C#
Tolerance: 1E-06 Max Iterations: 100
Code Blocks and Scope : Variables created inside curly braces are part of a "Scope." This is essential for solvers because it allows us to create temporary variables (like a local residual) that disappear once the calculation is done, keeping the computer's memory clean.
Example 3C#
Key Syntax Rules for Engineers
- Case Sensitivity: Matrix and matrix are different things in C#.
- Strong Typing: You cannot put a string (text) into a double (decimal number) without explicit conversion.
- Entry Point: Every C# application starts executing at the Main method.
The rest of sytax, structure, variable and operators are addressed in the following sections.
Sections in this chapter
- 01.1Primitive Types2 units · 11 examples · 23 min
- 01.2Variables2 units · 6 examples · 13 min
- 01.3Operators And Expressions2 units · 6 examples · 15 min
- 01.4Collections and Linq3 units · 6 examples · 13 min
- 01.5Algorithm Flow Control2 units · 5 examples · 12 min
- 01.6Exercise On Basic Operation and Syntax1 units · 3 examples · 7 min · exercises