Skip to content

Commit 95e3b6e

Browse files
Add known pitfalls to the article of nullable warnings and include 'Constructors` section in it. (#44985)
* Include a note about known pitfalls article in nullable warnings page * Add 'Constructors' to the known pitfalls * Correct the link to the known pitfalls to include file type Co-authored-by: Bill Wagner <[email protected]> * Emphasize the call won't throw if constructor completes successfully Co-authored-by: Bill Wagner <[email protected]> * Rename Foo and Bar classes * Combine the example description paragraphs * Use four spaces for indentatios --------- Co-authored-by: Bill Wagner <[email protected]>
1 parent 662b929 commit 95e3b6e

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

docs/csharp/language-reference/compiler-messages/nullable-warnings.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ This article covers the following compiler warnings:
192192

193193
The purpose of nullable warnings is to minimize the chance that your application throws a <xref:System.NullReferenceException?displayProperty=nameWithType> when run. To achieve this goal, the compiler uses static analysis and issues warnings when your code has constructs that might lead to null reference exceptions. You provide the compiler with information for its static analysis by applying type annotations and attributes. These annotations and attributes describe the nullability of arguments, parameters, and members of your types. In this article, you learn different techniques to address the nullable warnings the compiler generates from its static analysis. The techniques described here are for general C# code. Learn to work with nullable reference types and Entity Framework core in [Working with nullable reference types](/ef/core/miscellaneous/nullable-reference-types).
194194

195+
> [!NOTE]
196+
> The static analysis can't always deduce in what order, in a specific scenario, methods are accessed, and whether the method completes successfully without throwing an exception. Those known pitfalls are well described in [Known pitfalls](../../nullable-references.md#known-pitfalls) section.
197+
195198
You address almost all warnings using one of five techniques:
196199

197200
- Configuring the nullable context.

docs/csharp/nullable-references.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,49 @@ public static class Program
380380

381381
In the preceding example, the declaration of the array shows it holds non-nullable strings, while its elements are all initialized to `null`. Then, the variable `s` is assigned a `null` value (the first element of the array). Finally, the variable `s` is dereferenced causing a runtime exception.
382382

383+
### Constructors
384+
385+
Constructor of a class will still call the finalizer, even when there was an exception thrown by that constructor.
386+
<br/>The following example demonstrates that behavior:
387+
388+
```csharp
389+
public class A
390+
{
391+
private string _name;
392+
private B _b;
393+
394+
public A(string name)
395+
{
396+
ArgumentNullException.ThrowIfNullOrEmpty(name);
397+
_name = name;
398+
_b = new B();
399+
}
400+
401+
~A()
402+
{
403+
Dispose();
404+
}
405+
406+
public void Dispose()
407+
{
408+
_b.Dispose();
409+
GC.SuppressFinalize(this);
410+
}
411+
}
412+
413+
public class B: IDisposable
414+
{
415+
public void Dispose() { }
416+
}
417+
418+
public void Main()
419+
{
420+
var a = new A(string.Empty);
421+
}
422+
```
423+
424+
In the preceding example, the <xref:System.NullReferenceException?displayProperty=nameWithType> will be thrown when `_b.Dispose();` runs, if the `name` parameter was `null`. The call to `_b.Dispose();` won't ever throw when the constructor completes successfully. However, there's no warning issued by the compiler, because static analysis can't determine if a method (like a constructor) completes without a runtime exception being thrown.
425+
383426
## See also
384427

385428
- [Nullable reference types specification](~/_csharpstandard/standard/types.md#893-nullable-reference-types)

0 commit comments

Comments
 (0)