C#基础--构造函数/析构函数/常用函数
1.构造函数与析构函数
构造函数与析构函数都是系统自动调用的,相当与JAVA中的构造方法与垃圾回收器
2.常用函数
ToString()//转换成字符串
此外:Convert.ToBoolean(), Convert.ToChar(),Convert.ToDateTime(),Convert.ToDecimal(),Convert.ToInt32();
eg: string myString = "ture";
bool myBool = Convert.ToBoolean(MyString);//将myBool转换成bool型
3.字符串处理函数
Length属性
string mystr1 = "abABccDD";
Console.WriteLine("{0}",mystr1.Lenth)//输出为8
IndexOf方法
string mystr1= "abABccDD";
Console.WriteLine("{0}",mystr1.IndexOf("a"));//输出为0
SubString()方法
String myString = "abc";
bool test1 = String.Compare(myString.SubString(2,1),"c") = =0;//it is ture
bool test2 = String.Compare(mySring.SubString(3.0), String.Empty) = = 0;//this is ture
示例代码:
2
3 public class Desk
4 {
5 private int weight;
6 private int high;
7
8 //define construction function
9 public Desk()
10 {
11 Console.WriteLine("Constructing Desk");
12 weight = 6;
13 high = 3;
14 Console.WriteLine("weight = {0}\nhigh = {1}",weight,high);
15 }
16
17 //destruction function
18 ~Desk()
19 {
20 Console.WriteLine("Destructing Desk");
21 }
22
23 public static void Main()
24 {
25 Desk desk1 = new Desk();
26 Console.WriteLine("back in main");
27
28 //DateTime t = DateTime.Today;
29 DateTime t = DateTime.Now;
30 //following is some time formation, using ToString("formationg") method imply
31 Console.WriteLine("Now,date time is:{0}",t.ToString("D"));
32 Console.WriteLine("Now,date time is:{0}",t.ToString("d"));
33 Console.WriteLine("Now,date time is:{0}",t.ToString("G"));
34 Console.WriteLine("Now,date time is:{0}",t.ToString("g"));
35 Console.WriteLine("Now,date time is:{0}",t.ToString("T"));
36 Console.WriteLine("Now,date time is:{0}",t.ToString("t"));
37 }
38 }