Skip to content

Commit 5a8d705

Browse files
authored
Add missing boxing snippet (#45098)
1 parent 32a6c30 commit 5a8d705

File tree

1 file changed

+36
-0
lines changed
  • samples/snippets/csharp/VS_Snippets_VBCSharp/CsProgGuideTypes/CS

1 file changed

+36
-0
lines changed

samples/snippets/csharp/VS_Snippets_VBCSharp/CsProgGuideTypes/CS/Class1.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,42 @@ static void Wrap()
109109
}
110110
}
111111

112+
113+
//-------------------------------------------------------------------------
114+
class BoxingValueCopy
115+
{
116+
static void Main()
117+
{
118+
//<Snippet16>
119+
// Create an int variable
120+
int i = 123;
121+
122+
// Box the value type into an object reference
123+
object o = i; // boxing
124+
125+
// Display the initial values
126+
Console.WriteLine($"Value of i: {i}");
127+
Console.WriteLine($"Value of boxed object o: {o}");
128+
129+
// Modify the original value type
130+
i = 456;
131+
132+
// Display the values after modification
133+
Console.WriteLine("\nAfter changing i to 456:");
134+
Console.WriteLine($"Value of i: {i}");
135+
Console.WriteLine($"Value of boxed object o: {o}");
136+
137+
// Output:
138+
// Value of i: 123
139+
// Value of boxed object o: 123
140+
141+
// After changing i to 456:
142+
// Value of i: 456
143+
// Value of boxed object o: 123
144+
//</Snippet16>
145+
}
146+
}
147+
112148
//-------------------------------------------------------------------------
113149
class TestBoxing
114150
{

0 commit comments

Comments
 (0)