-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add article about boxing bools in C#
- Loading branch information
1 parent
c0555d9
commit ecc6c6e
Showing
2 changed files
with
27 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
--- | ||
title: Boxing bool values | ||
topics: | ||
- csharp | ||
- dotnet | ||
- boxing | ||
--- | ||
|
||
When boxing value types in C#/.NET, an object on the heap is created. | ||
|
||
**What happens when you box a `bool` value?** | ||
|
||
Since there are only two possible values, are the box objects reference equal? | ||
|
||
The answer is: **No. For example, boxing `true` twice results in different objects.** | ||
|
||
Consider this code: | ||
|
||
```c# | ||
var obj1 = (object)true; | ||
var obj2 = (object)true; | ||
|
||
Console.WriteLine(ReferenceEquals(obj1, obj2)); // prints "false" | ||
``` | ||
|
||
There are not even {{< abbr "BCL" "Base Class Library" >}} constants for `true` and `false` (unlike Java). This issue is behavior is discussed in this [GitHub issue](https://github.com/dotnet/runtime/issues/47596). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters