Compiler Error CS0445
Cannot modify the result of an unboxing conversion
The result of an unboxing conversion is a temporary variable. The compiler prevents you from modifying such variables because any modification would go away when the temporary variable goes away. To fix this, declare a new value-type variable to store the intermediate expression, and assign the result of the unboxing conversion to that variable.
The following code generates CS0455.
// CS0445.CS
class UnboxingTest
{
public static void Main()
{
Point p;
p.x = 1;
p.y = 2;
object obj = p;
// The following line generates CS0445, because the result
// of unboxing obj is a temporary variable.
((Point)obj).x = 2;
// The following lines resolve the error.
// Store the result of the unboxing conversion in p2.
Point p2;
p2 = (Point)obj;
// Then you can modify the unboxed value.
p2.x = 2;
}
}
struct Point
{
public int x, y;
}
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for