Skip to content

Commit ee28614

Browse files
CopilotBillWagnergewarren
authored
Add definitions for expressions and statements to C# fundamentals program structure (#46896)
* Initial plan for issue * Add definitions for expressions and statements to C# fundamentals program structure Co-authored-by: BillWagner <[email protected]> * Improve flow of expressions and statements section per review feedback Co-authored-by: BillWagner <[email protected]> * Apply style guide feedback: use italics for expressions/statements and improve clarity Co-authored-by: gewarren <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: BillWagner <[email protected]> Co-authored-by: gewarren <[email protected]>
1 parent 6f4b235 commit ee28614

File tree

1 file changed

+23
-0
lines changed
  • docs/csharp/fundamentals/program-structure

1 file changed

+23
-0
lines changed

docs/csharp/fundamentals/program-structure/index.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,29 @@ You can also create a static method named [`Main`](main-command-line.md) as the
1919

2020
In that case the program will start in the first line of `Main` method, which is `Console.WriteLine("Hello world!");`
2121

22+
## Expressions and statements
23+
24+
C# programs are built using *expressions* and *statements*. Expressions produce a value, and statements perform an action:
25+
26+
An *expression* is a combination of values, variables, operators, and method calls that evaluates to a single value. Expressions produce a result and can be used wherever a value is expected. The following examples are expressions:
27+
28+
- `42` (literal value)
29+
- `x + y` (arithmetic operation)
30+
- `Math.Max(a, b)` (method call)
31+
- `condition ? trueValue : falseValue` (conditional expression)
32+
- `new Person("John")` (object creation)
33+
34+
A *statement* is a complete instruction that performs an action. Statements don't return values; instead, they control program flow, declare variables, or perform operations. The following examples are statements:
35+
36+
- `int x = 42;` (declaration statement)
37+
- `Console.WriteLine("Hello");` (expression statement - wraps a method call expression)
38+
- `if (condition) { /* code */ }` (conditional statement)
39+
- `return result;` (return statement)
40+
41+
The key distinction: expressions evaluate to values, while statements perform actions. Some constructs, like method calls, can be both. For example, `Math.Max(a, b)` is an expression when used in `int result = Math.Max(a, b);`, but becomes an expression statement when written alone as `Math.Max(a, b);`.
42+
43+
For detailed information about statements, see [Statements](../../programming-guide/statements-expressions-operators/statements.md). For information about expression-bodied members and other expression features, see [Expression-bodied members](../../programming-guide/statements-expressions-operators/expression-bodied-members.md).
44+
2245
## Related Sections
2346

2447
You learn about these program elements in the [types](../types/index.md) section of the fundamentals guide:

0 commit comments

Comments
 (0)