我有我的結構:類庫允許使用未分配結構
public struct MyType
{
private string value;
// Methods
// ... (ToString overrided too)
}
如果我把結構中的Program.cs或其他File.cs,我創建一個變量作爲MyType
(我的結構),我嘗試使用它,結果是明顯的錯誤:
CS0165 Use of unassigned local variable
實施例:
MyType a;
Console.WriteLine(a); // Error: Use of unassigned local variable 'a'
MyType b = new MyType();
Console.WriteLine(b); // Prints the default value (an empty string)
問題是,當我把結構中的一個類庫(在另一個項目,或者從NuGet包),我用它的Program.cs:
MyType a;
Console.WriteLine(a); // No error: prints an empty string
這究竟是爲什麼?
什麼是在第二種情況下的輸出? –
更正了我的答案(完全錯過了班級圖書館的一部分......抱歉)! –
@DmitryK。一個空字符串。添加到問題中。 – Joe