You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/csharp/fundamentals/tutorials/records.md
+13-8Lines changed: 13 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: Use record types tutorial
3
3
description: Build a small app that models temperature data with records, compares record behavior, and uses with expressions for nondestructive mutation.
4
-
ms.date: 04/10/2026
4
+
ms.date: 04/14/2026
5
5
ms.topic: tutorial
6
6
ai-usage: ai-assisted
7
7
---
@@ -30,9 +30,9 @@ In this tutorial, you learn how to:
30
30
31
31
Create a folder for your app, run `dotnet new console`, and open the generated project.
32
32
33
-
Add a file named *InterimSteps.cs*, and add a positional `readonly record struct` for temperature values:
33
+
Add a file named *DailyTemperature.cs*, and add a positional `readonly record struct` for temperature values:
A record struct works well here because each value is small and self-contained.
50
52
51
53
## Build record types for degree-day calculations
52
54
53
-
Add a hierarchy for heating and cooling degree-day calculations:
55
+
> [!NOTE]
56
+
> **Heating degree-days** and **cooling degree-days** measure how much the daily average temperature deviates from a base temperature (typically 65°F/18°C). Heating degree-days accumulate on cold days when the average is below the base, while cooling degree-days accumulate on warm days when the average is above the base. These calculations help estimate energy consumption for heating or cooling buildings, making them useful for utility companies, building managers, and climate analysis.
57
+
58
+
Create a file named *DegreeDays.cs* with a hierarchy for heating and cooling degree-day calculations:
Copy file name to clipboardExpand all lines: docs/csharp/fundamentals/types/conversions.md
+12-5Lines changed: 12 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: "Type conversions, casting, and boxing"
3
3
description: Learn how to convert between C# types by using implicit and explicit conversions, safe casting patterns, boxing and unboxing, and Parse and TryParse APIs.
4
-
ms.date: 04/10/2026
4
+
ms.date: 04/14/2026
5
5
ms.topic: concept-article
6
6
ai-usage: ai-assisted
7
7
---
@@ -13,7 +13,14 @@ ai-usage: ai-assisted
13
13
>
14
14
> **Experienced in another language?** C# conversions work like most statically typed languages: widening conversions are implicit, narrowing conversions need explicit casts, and parsing text should favor `TryParse` in user-facing code.
15
15
16
-
When you write C# code, you often move values from one type to another. For example, you might convert from `int` to `long`, read text and convert it to a number, or cast a base type to a derived type.
16
+
When you write C# code, you often convert values from one type to another. For example, you might convert from `int` to `long`, read text and convert it to a number, or cast a base type to a derived type.
17
+
18
+
Understanding key terms:
19
+
20
+
- A *conversion* is the process of changing a value from one type to another.
21
+
- A *cast* is the explicit syntax for conversion, written with parentheses like `(int)value`.
22
+
- An *implicit cast* is a conversion that happens automatically when the compiler can guarantee it's safe.
23
+
- An *explicit cast* is a conversion you write in code, indicating the conversion might lose information or fail.
17
24
18
25
Choose the conversion style based on risk:
19
26
@@ -24,7 +31,7 @@ Choose the conversion style based on risk:
24
31
25
32
## Use implicit and explicit numeric conversions
26
33
27
-
Use implicit conversions when the destination type can represent the full source range. Use explicit casts when the destination type has a smaller range or less precision.
34
+
An *implicit conversion* always succeeds. An *explicit conversion* might fail or lose information.
@@ -34,7 +41,7 @@ For full conversion tables, see [Built-in numeric conversions](../../language-re
34
41
35
42
## Convert references safely
36
43
37
-
For reference types, you often start with a base type and need to access members from a derived type. Prefer pattern matching so the test and assignment happen together.
44
+
Casts on value types typically copy the data to the destination type. Casts on reference types don't copy data; they change how you view the same object. For reference types, you often start with a base type and need to access members from a derived type. Prefer pattern matching so the test and assignment happen together.
@@ -56,7 +63,7 @@ Boxing allocates memory on the managed heap, and unboxing requires a type check.
56
63
57
64
## Parse text by using Parse and TryParse
58
65
59
-
When you convert user input or file content, start with `TryParse`. It avoids exceptions for expected invalid input and makes failure handling explicit.
66
+
When you convert user input or file content, start with `TryParse`. It avoids exceptions for expected invalid input and makes failure handling explicit. All parsing APIs create a new object or value type instance from the source string; they don't modify the source.
Copy file name to clipboardExpand all lines: docs/csharp/fundamentals/types/delegates-lambdas.md
+10-4Lines changed: 10 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: "Delegates, lambdas, and events"
3
3
description: Learn how to use Func and Action delegates, write lambda expressions, use static lambdas and discard parameters, and understand the basic event subscription model.
4
-
ms.date: 04/10/2026
4
+
ms.date: 04/14/2026
5
5
ms.topic: concept-article
6
6
ai-usage: ai-assisted
7
7
---
@@ -13,17 +13,19 @@ ai-usage: ai-assisted
13
13
>
14
14
> **Experienced in another language?** Think of delegates as strongly typed function variables. In modern C#, you usually write them with lambda expressions and `Func` or `Action` types.
15
15
16
-
Delegates let you pass behavior as data. You use delegates when code needs a callback, a rule, or a transformation that the caller supplies.
16
+
*Delegates* let you pass behavior as data. You use delegates when code needs a callback, a rule, or a transformation that the caller supplies.
17
17
18
18
In everyday C# code, you most often use:
19
19
20
20
-`Func<T...>` when a delegate returns a value.
21
21
-`Action<T...>` when a delegate returns `void`.
22
22
- Lambda expressions to create delegate instances.
23
23
24
+
A *lambda expression* is an anonymous function with a compact syntax. It lets you write a function inline without naming it, using the arrow operator `=>` to separate parameters from the body.
25
+
24
26
## Start with Func and Action
25
27
26
-
`Func` and `Action` cover most delegate scenarios without creating a custom delegate type.
28
+
<xref:System.Func`2?displayProperty=nameWithType> and <xref:System.Action`1?displayProperty=nameWithType> cover most delegate scenarios without creating a custom delegate type.
@@ -39,7 +41,7 @@ This pattern appears throughout LINQ and many .NET APIs.
39
41
40
42
## Use static lambdas when capture is unnecessary
41
43
42
-
A static lambda can't capture local variables. Use it when the logic depends only on parameters.
44
+
A static lambda uses the `static` modifier before the parameter list, for example: `static x => x * 2` or `static (x, y) => x + y`. A static lambda can't capture local variables or instance state from the enclosing scope. *Capturing* means the lambda references variables from the enclosing scope. A *closure* is the combination of the lambda and the captured variables it holds a reference to. A non-static lambda can capture those values, while a static lambda must use only its parameters and values declared inside its body.
@@ -49,6 +51,8 @@ Static lambdas make intent clear and prevent accidental captures.
49
51
50
52
Sometimes a delegate signature includes parameters you don't need. Use discards to signal that choice clearly.
51
53
54
+
Common examples include event handlers where you don't use `sender` or `EventArgs`, callbacks where you only need some of several inputs, and LINQ overloads that provide an index you don't use.
Discards improve readability because they show which parameters matter.
@@ -57,6 +61,8 @@ Discards improve readability because they show which parameters matter.
57
61
58
62
Events expose notifications. You subscribe with a delegate, often a lambda expression.
59
63
64
+
Subscribing to an event is optional because publishers can raise events even when no listeners are attached. By contrast, APIs that accept a callback usually require that callback so the API can complete its work.
0 commit comments