我可能沒有清楚地解釋這一點。但是在部分類內部與在每個方法內部創建類的缺點是什麼? (請參見實施例)在ASP.NET中部分或在方法內實例化C#類
實施例內部部分:
public partial class test: System.Web.UI.Page
{
cSystem oSystem = new cSystem();
protected void Page_Load(object sender, EventArgs e)
{
oSystem.useme();
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
oSystem.usethis();
}
與
實施例的每個類中:
public partial class test: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
cSystem oSystem = new cSystem();
oSystem.useme();
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
cSystem oSystem = new cSystem();
oSystem.usethis();
}
如果你的班級和成員似乎是靜態的,爲什麼還要實例化 – Pleun
不,它不是靜態的。 cSystem是一個實用程序類。我遠離以前發佈的web應用程序的靜態類。 – user2156940
我也使用cSystem作爲我的控制器之一,並且.cs頁面使用它們:using {myapp} .Controllers; namespace {myapp} .Controllers { public class cSystem { – user2156940