c#基礎知識第十一節

來源:互聯網
上載者:User

標籤:bsp   編譯   資源   回收   int   ram   rod   write   intro   

析構方法

和構造方法相反。

class person
{
public string Name
{
get;
set;
}
//析構方法,在對象被銷毀時會自動調用
~person()
{
Console.WriteLine("資源被回收");
}
}

class Program
{
static void Main(string[] args)
{
person p = new person() { Name="張三"};
p = null;
Console.ReadKey();
}

調用靜態成員的文法格式:類名.成員名

非靜態成員:對象名.成員名

被static關鍵字修飾的成員稱為靜態成員。包括靜態欄位,靜態屬性,靜態方法。

欄位、屬性、和方法都是類的成員。

 

靜態類(不能建立對象)

static class Student
{
private static string schoolName = "兵峰軟體";
public static string SchoolName
{
get
{
return schoolName;
}
set
{
SchoolName = value;
}
}
public static void Test()
{
Console.WriteLine(schoolName);

}
}
class Program
{
static void Main(string[] args)
{
Student.Test();
Console.ReadKey();
}
}

匿名類

ass Program
{
static void Main(string[] args)
{
//var表示匿名型別,編譯器會根據上下文自動推斷出具體的類型
var s = new {Name="張三",Age=18};//建立匿名對象
Console.WriteLine("我叫{0},我今年{1}歲了!",s.Name,s.Age);
Console.ReadKey();

}

}

部分類別(partial)


//File.Student1.cs
partial string Student
{
public string Name
{
get;
set;
}


//File.Student2.cs
partial string Student
{
public string Name
{

public void Introduce()

{

Console.WriteLine(this.Name);

}
}

 

c#基礎知識第十一節

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.