[英]C# ParameterizedThreadStart not supported in Visual Studio 2003?
[英]Visual Studio 2003 C#, struct compiling erro
編譯期間我對結構有疑問。 我使用c#編程並使用Visual Studio2003。 來自MSDN :
使用new運算符創建結構對象時,將創建該對象並調用適當的構造函數。 與類不同,可以在不使用new運算符的情況下實例化結構。 如果不使用new,則字段將保持未分配狀態,並且在所有字段都初始化之前,無法使用該對象。
您可以在沒有new()語句的情況下實例化結構。 在我的電腦上,它可以完美工作,而在另一台電腦上,我看到編譯錯誤(需要new())。 在Visual Studio的環境上是否存在某些過濾器或標志(例如TreatWarningsAsErrors)可以生成此行為?
一個例子:
using System;
using System.Collections;
namespace myApp.Utils
{
....
public struct StructParam
{
public int iIndex;
public int[] iStartNoteArray;
public int[] iFinalNoteArray;
public int[] iDimension;
public int[] iStartSequence;
public ArrayList m_iRowIncValueArray;
};
....
}
--------------------------------------------------------------
--------------------------------------------------------------
using System;
using System.Collections;
using myApp.Utils;
namespace myApp.Main
{
....
public class frmMain : System.Windows.Forms.Form
{
....
static void Main()
{
....
StructParam oStructParam;
oStructParam.iIndex = 0;
oStructParam.iStartNoteArray = new int[]{0, 0};
oStructParam.iFinalNoteArray = new int[]{0, 0};
oStructParam.iDimension = new int[]{0, 0};
oStructParam.iStartSequence = new int[]{0, 0};
oStructParam.m_iRowIncValueArray = new ArrayList();
ArrayList myArray = new ArrayList();
myArray.Add(oStructParam);
....
}
.....
}
....
}
我不認為問題出在代碼中,而在於某些Visual Studio的環境變量中。
要使用結構而不調用new,必須首先分配其所有成員。
例如,
struct Point
{
public int x;
public int y;
public int DoSomething() { return x * y; }
}
Point p;
p.x = 1;
p.y = 2;
p.DoSomething();
請注意,這里的x和y是字段,不是屬性。 您必須先分配所有字段,然后才能使用該結構。 例如,如果要包括自動屬性,而您無權訪問基礎字段,則將不可能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.