码迷,mamicode.com
首页 > Windows程序 > 详细

c#基础知识第十一节

时间:2017-10-16 13:36:11      阅读:190      评论:0      收藏:0      [点我收藏+]

标签: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#基础知识第十一节

标签:bsp   编译   资源   回收   int   ram   rod   write   intro   

原文地址:http://www.cnblogs.com/zhang1997/p/7676333.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!