前言:我也是才開始從底層安全轉為應用軟件開發,由於因緣巧合開始從事C#開發,從零開始的學習過程中遇到一些非常有意思的現象,這里記錄下來,也一並把自己的理解寫下,如果對了,希望能給他人幫助;如果錯了,也希望大家諒解。
namespace testVS2013報錯為:Error1Cannot modify the return value of 'test.Program.Prop' because it is not a variable... so,為什么? 在stackflow上面也查了,得到的回答大多數是說”struct is immutable“等等。但是這好像說不通,一是我這個struct沒有任何一個field聲明為readonly,完完全全是mutable的;二是immutable的話,錯誤提示的應該不是這個。 經過小小的研究和實驗,得出如下結論:
{
struct MyStruct
{
public int i;
}
class Program
{
private MyStruct Prop { get; set; }
public static void Main(string[] args)
{
Console.Read();
}
public void Bug()
{
Prop.i = 2;
}
}
}
public void Bug()代碼編譯通過,並且實驗證明確實更改了。因為自動生成屬性中的set一般實現{ prop=value},把一個struct復制到另一個struct是可行的,所以這里成立。 看來C#方便是方便,但是還是有些想當然的地方要靜下心來好好琢磨琢磨,說到底還是自己對代碼的感知能力不足,還是要再接再厲啊。 (2014-5-20)
{
Prop = new MyStruct() { i = 2 };
}
Dimensions point;“...For a struct, however, the variable declaration actually allocates space on the stack for the entire struct,so it’s ready to assign values to." (對於一個struct對象而言,變量的聲明實際上已經在棧上為整個struct對象分配了內存,所以已經可以給該對象賦值了) 那么我們來看以下代碼:
point.Length = 3;
point.Width = 6;
namespace test這里有報錯,原因是"Use of unassigned local variable 't' "。看來是使用了沒有初始化的變量。如果改成MyStruct t=new MyStruct(),就正確了。那么,不是說struct類型可以不用new么?通過筆者的實驗和在stackflow上搜索資料,我得出如下結論: 如果是不使用new,而是直接 " MyStruct t; ",那么property內部封裝的對象是沒有初始化的,這里就是private int age 沒有初始化。而初始化property的方法在我所知范圍內是不存在的,對於struct,只能在構造函數中實現;而對於class,還可以通過instance field initializer,即定義的時候就public int age=1;。經過筆者嘗試,有一種辦法可以是得“MyStruct t ”合法,即手動賦值封裝的age,代碼如下:
{
struct MyStruct
{
public int Age { get; set; }
}
class Program
{
public static void Main(string[] args)
{
MyStruct t;
t.Age = 1;// ERROR!
Console.Read();
}
}
}
namespace test但是這樣直接破壞了封裝性,當然,struct本身一般不適用property,直接是public的field,基本沒有封裝性可言……
{
struct MyStruct
{
public int age;
public int Age
{
get
{
return age;
}
set
{
age = value;
}
}
}
class Program
{
public static void Main(string[] args)
{
MyStruct t;
<span style="background-color: rgb(255, 255, 51);">t.age = 1;</span>
Console.WriteLine(t.Age);
Console.Read();
}
}
}
namespace test代碼運行成功,結果打出0,是默認構造器的風格
{
struct MyStruct
{
public int age;
public int Age
{
get
{
return age;
}
set
{
age = value;
}
}
}
class Program
{
public static void Main(string[] args)
{
<span style="background-color: rgb(255, 255, 51);">MyStruct t=new MyStruct();</span>
Console.WriteLine(t.Age);
Console.Read();
}
}
}
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。