C# struct的陷阱:无法修改“...”的返回值,因为它不是变量
生活随笔
收集整理的這篇文章主要介紹了
C# struct的陷阱:无法修改“...”的返回值,因为它不是变量
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Cannot modify the return value of ‘expression‘ because it is not a variable
無法修改“expression”的返回值,因為它不是變量
出現這種錯誤的最常見情況是:
AnObject.AnStruct.Vaule = xxx;
考慮如下程序:初看沒什么問題,實際上根本無法編譯
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
public struct Rectangle
{
private int _Width;
private int _Height;
public int Width
{
get { return _Width; }
set { _Width = value; }
}
public int Height
{
get { return _Height; }
set { _Height = value; }
}
public Rectangle(int width, int height)
{
this._Width = width;
this._Height = height;
}
}
public class Draw
{
private Rectangle _rect = new Rectangle(0, 0);
public Rectangle MyRect
{
get { return _rect; }
set { _rect = value; }
}
public Draw() { }
public Draw(int mapWidth, int mapHeight)
{
_rect = new Rectangle(mapWidth, mapHeight);
}
}
class Program
{
static void Main(string[] args)
{
Draw draw = new Draw();
draw.MyRect.Width = 20;//《== 編譯錯誤
draw.MyRect.Height = 12;《== 編譯錯誤
}
}
}
draw是引用類型,MyRect是struct,也就是值類型,draw.MyRect會調用
public Rectangle MyRect
{
get { return _rect; }
}
而值類型默認是按值傳遞的,返回的是棧上_rect的一份臨時的、本地的拷貝,我們暫且稱之為temp_rect,
draw.MyRect.Width = 20 等同于temp_rect.Width = 20;
因此即使能對它進行修改也無法反映到draw.MyRect本身(即_rect)的,即這是一個毫無何意義的操作,因此編譯器就從源頭上禁止了這樣的操作。
解決方法:
1)把struct替換成class,這樣,MyRect就成了一個對象(引用類型),draw.MyRect就會返回_rect在堆上的實際地址,修改也會反映到實際的對象
2)如果非要用struct不可的話,需要設置一個中間變量:
Draw draw = new Draw();
Rectangle tempRect = new Rectangle();
tempRect.Width = 20;
tempRect.Height = 12;
draw.MyRect = tempRect;
這樣實際上是替換draw對象的整個_rect屬性。
無法修改“expression”的返回值,因為它不是變量
出現這種錯誤的最常見情況是:
AnObject.AnStruct.Vaule = xxx;
考慮如下程序:初看沒什么問題,實際上根本無法編譯
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
public struct Rectangle
{
private int _Width;
private int _Height;
public int Width
{
get { return _Width; }
set { _Width = value; }
}
public int Height
{
get { return _Height; }
set { _Height = value; }
}
public Rectangle(int width, int height)
{
this._Width = width;
this._Height = height;
}
}
public class Draw
{
private Rectangle _rect = new Rectangle(0, 0);
public Rectangle MyRect
{
get { return _rect; }
set { _rect = value; }
}
public Draw() { }
public Draw(int mapWidth, int mapHeight)
{
_rect = new Rectangle(mapWidth, mapHeight);
}
}
class Program
{
static void Main(string[] args)
{
Draw draw = new Draw();
draw.MyRect.Width = 20;//《== 編譯錯誤
draw.MyRect.Height = 12;《== 編譯錯誤
}
}
}
draw是引用類型,MyRect是struct,也就是值類型,draw.MyRect會調用
public Rectangle MyRect
{
get { return _rect; }
}
而值類型默認是按值傳遞的,返回的是棧上_rect的一份臨時的、本地的拷貝,我們暫且稱之為temp_rect,
draw.MyRect.Width = 20 等同于temp_rect.Width = 20;
因此即使能對它進行修改也無法反映到draw.MyRect本身(即_rect)的,即這是一個毫無何意義的操作,因此編譯器就從源頭上禁止了這樣的操作。
解決方法:
1)把struct替換成class,這樣,MyRect就成了一個對象(引用類型),draw.MyRect就會返回_rect在堆上的實際地址,修改也會反映到實際的對象
2)如果非要用struct不可的話,需要設置一個中間變量:
Draw draw = new Draw();
Rectangle tempRect = new Rectangle();
tempRect.Width = 20;
tempRect.Height = 12;
draw.MyRect = tempRect;
這樣實際上是替換draw對象的整個_rect屬性。
總結
以上是生活随笔為你收集整理的C# struct的陷阱:无法修改“...”的返回值,因为它不是变量的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: GCC 命令行详解
- 下一篇: 薪水增长多少,新机会才值得考虑?