O'Reilly logo

Applied Microsoft® .NET Framework Programming by Jeffrey Richter

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Changing Fields in a Boxed Value Type Using Interfaces

Let’s have some fun and see how well you understand value types, boxing, and unboxing. Examine the following code, and see whether you can figure out what it displays on the console:

using System; // Point is a value type. struct Point { public Int32 x, y; public void Change(Int32 x, Int32 y) { this.x = x; this.y = y; } public override String ToString() { return String.Format("({0}, {1})", x, y); } } class App { static void Main() { Point p = new Point(); p.x = p.y = 1; Console.WriteLine(p); // Displays (1, 1) p.Change(2, 2); Console.WriteLine(p); // Displays (2, 2) Object o = p; Console.WriteLine(o); // Displays (2, 2) ((Point) o).Change(3, 3); // Changes temporary Point on stack! Console.WriteLine(o); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required