静态构造函数用于初始化任何静态数据,或用于执行仅需执行一次的特定操作。在创建第一个实例或引用任何静态成员之前,将自动调用静态构造函数。
1
class SimpleClass
2
{
3
// Static constructor
4
static SimpleClass()
5
{
6
//
7
}
8
}
class SimpleClass2
{3
// Static constructor4
static SimpleClass()5
{6
//
7
}8
}静态构造函数具有以下特点:
-
静态构造函数既没有访问修饰符,也没有参数。
-
在创建第一个实例或引用任何静态成员之前,将自动调用静态构造函数来初始化类。
-
无法直接调用静态构造函数。
-
在程序中,用户无法控制何时执行静态构造函数。
-
静态构造函数的典型用途是:当类使用日志文件时,将使用这种构造函数向日志文件中写入项。
-
静态构造函数在为非托管代码创建包装类时也很有用,此时该构造函数可以调用 LoadLibrary 方法。
在此示例中,类 Bus 有一个静态构造函数和一个静态成员 Drive()。当调用 Drive() 时,将调用静态构造函数来初始化类。
1
public class Bus
2
{
3
// Static constructor:
4
static Bus()
5
{
6
System.Console.WriteLine("The static constructor invoked.");
7
}
8
9
public static void Drive()
10
{
11
System.Console.WriteLine("The Drive method invoked.");
12
}
13
}
14
15
class TestBus
16
{
17
static void Main()
18
{
19
Bus.Drive();
20
}
21
}
public class Bus2
{3
// Static constructor:4
static Bus()5
{6
System.Console.WriteLine("The static constructor invoked.");7
}8

9
public static void Drive()10
{11
System.Console.WriteLine("The Drive method invoked.");12
}13
}14

15
class TestBus16
{17
static void Main()18
{19
Bus.Drive();20
}21
}输出
The static constructor invoked.
The Drive method invoked.
posted @ 2008-05-19 16:33 大宋提刑官 阅读(112) 评论(2) 编辑
链接:http://www.csharphelp.com/archives4/archive693.html
1
GetUrls urls = new GetUrls(); urls.RetrieveUrls("http://www.microsoft.com");
2
3
4
The class is listed below. Have fun!
5
//required namespaces
6
using System;
7
using System.Collections.Generic;
8
using System.Text;
9
using System.Net;
10
using System.IO;
11
using System.Text.RegularExpressions;
12
13
14
namespace FindAllUrls
15
{
16
class GetUrls
17
{
18
19
//public method called from your application
20
public void RetrieveUrls( string webPage )
21
{
22
GetAllUrls(RetrieveContent(webPage));
23
}
24
25
//get the content of the web page passed in
26
private string RetrieveContent(string webPage)
27
{
28
HttpWebResponse response = null;//used to get response
29
StreamReader respStream = null;//used to read response into string
30
try
31
{
32
//create a request object using the url passed in
33
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(webPage);
34
request.Timeout = 10000;
35
36
//go get a response from the page
37
response = (HttpWebResponse)request.GetResponse();
38
39
//create a streamreader object from the response
40
respStream = new StreamReader(response.GetResponseStream());
41
42
//get the contents of the page as a string and return it
43
return respStream.ReadToEnd();
44
}
45
catch (Exception ex)//houston we have a problem!
46
{
47
throw ex;
48
}
49
finally
50
{
51
//close it down, we're going home!
52
response.Close();
53
respStream.Close();
54
}
55
}
56
57
//using a regular expression, find all of the href or urls
58
//in the content of the page
59
private void GetAllUrls( string content )
60
{
61
//regular expression
62
string pattern = @"(?:href\s*=)(?:[\s""']*)(?!#|mailto|location.|javascript|.*css|.*this\.)(?
63
.*?)(?:[\s>""'])";
64
65
//Set up regex object
66
Regex RegExpr = new Regex(pattern, RegexOptions.IgnoreCase);
67
68
//get the first match
69
Match match = RegExpr.Match(content);
70
71
//loop through matches
72
while (match.Success)
73
{
74
75
//output the match info
76
Console.WriteLine("href match: " + match.Groups[0].Value);
77
WriteToLog("C:\matchlog.txt", "href match: " + match.Groups[0].Value + "\r\n");
78
79
Console.WriteLine("Url match: " + match.Groups[1].Value);
80
WriteToLog("C:\matchlog.txt", "Url | Location | mailto match: " + match.Groups[1].Value + "\r\n");
81
82
//get next match
83
match = match.NextMatch();
84
}
85
}
86
87
//Write to a log file
88
private void WriteToLog(string file, string message)
89
{
90
using (StreamWriter w = File.AppendText(file))
91
{
92
w.WriteLine(DateTime.Now.ToString() + ": " + message); w.Close();
93
}
94
}
95
}
96
}
97
98
GetUrls urls = new GetUrls(); urls.RetrieveUrls("http://www.microsoft.com"); 2

3

4
The class is listed below. Have fun! 5
//required namespaces6
using System; 7
using System.Collections.Generic; 8
using System.Text; 9
using System.Net; 10
using System.IO; 11
using System.Text.RegularExpressions; 12

13

14
namespace FindAllUrls 15
{ 16
class GetUrls 17
{ 18

19
//public method called from your application 20
public void RetrieveUrls( string webPage ) 21
{ 22
GetAllUrls(RetrieveContent(webPage)); 23
} 24

25
//get the content of the web page passed in 26
private string RetrieveContent(string webPage) 27
{ 28
HttpWebResponse response = null;//used to get response 29
StreamReader respStream = null;//used to read response into string 30
try 31
{ 32
//create a request object using the url passed in 33
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(webPage); 34
request.Timeout = 10000; 35

36
//go get a response from the page 37
response = (HttpWebResponse)request.GetResponse(); 38
39
//create a streamreader object from the response 40
respStream = new StreamReader(response.GetResponseStream()); 41

42
//get the contents of the page as a string and return it 43
return respStream.ReadToEnd(); 44
} 45
catch (Exception ex)//houston we have a problem! 46
{ 47
throw ex; 48
} 49
finally 50
{ 51
//close it down, we're going home! 52
response.Close(); 53
respStream.Close(); 54
} 55
} 56
57
//using a regular expression, find all of the href or urls 58
//in the content of the page 59
private void GetAllUrls( string content ) 60
{ 61
//regular expression 62
string pattern = @"(?:href\s*=)(?:[\s""']*)(?!#|mailto|location.|javascript|.*css|.*this\.)(?63
.*?)(?:[\s>""'])"; 64
65
//Set up regex object 66
Regex RegExpr = new Regex(pattern, RegexOptions.IgnoreCase); 67

68
//get the first match 69
Match match = RegExpr.Match(content); 70

71
//loop through matches 72
while (match.Success) 73
{ 74

75
//output the match info 76
Console.WriteLine("href match: " + match.Groups[0].Value); 77
WriteToLog("C:\matchlog.txt", "href match: " + match.Groups[0].Value + "\r\n"); 78

79
Console.WriteLine("Url match: " + match.Groups[1].Value); 80
WriteToLog("C:\matchlog.txt", "Url | Location | mailto match: " + match.Groups[1].Value + "\r\n"); 81
82
//get next match 83
match = match.NextMatch(); 84
} 85
} 86

87
//Write to a log file 88
private void WriteToLog(string file, string message) 89
{ 90
using (StreamWriter w = File.AppendText(file)) 91
{ 92
w.WriteLine(DateTime.Now.ToString() + ": " + message); w.Close(); 93
} 94
} 95
} 96
}97

98

posted @ 2008-05-19 14:15 大宋提刑官 阅读(62) 评论(1) 编辑
链接:http://www.csharphelp.com/archives4/archive694.html
From Decimal to Binary...
--------------------------------------------------------------------------------1using System;
2
3class Program{
4
5static void Main(string[] args){
6
7try{
8![]()
9int i = (int)Convert.ToInt64(args[0]);
10Console.WriteLine("\n{0} converted to Binary is {1}\n",i,ToBinary(i));
11![]()
12}catch(Exception e){
13![]()
14Console.WriteLine("\n{0}\n",e.Message);
15![]()
16}
17
18}//end Main
19
20
21public static string ToBinary(Int64 Decimal)
22{
23// Declare a few variables we're going to need
24Int64 BinaryHolder;
25char[] BinaryArray;
26string BinaryResult = "";
27
28while (Decimal > 0)
29{
30BinaryHolder = Decimal % 2;
31BinaryResult += BinaryHolder;
32Decimal = Decimal / 2;
33}
34
35// The algoritm gives us the binary number in reverse order (mirrored)
36// We store it in an array so that we can reverse it back to normal
37BinaryArray = BinaryResult.ToCharArray();
38Array.Reverse(BinaryArray);
39BinaryResult = new string(BinaryArray);
40
41return BinaryResult;
42}
43
44
45}//end class Program
46
From Binary to Decimal...
1using System;
2
3class Program{
4
5static void Main(string[] args){
6
7try{
8![]()
9int i = ToDecimal(args[0]);
10Console.WriteLine("\n{0} converted to Decimal is {1}",args[0],i);
11![]()
12}catch(Exception e){
13![]()
14Console.WriteLine("\n{0}\n",e.Message);
15![]()
16}
17
18}//end Main
19
20
21public static int ToDecimal(string bin)
22{
23long l = Convert.ToInt64(bin,2);
24int i = (int)l;
25return i;
26}
27
28
29}//end class Program
30
31
posted @ 2008-05-19 13:10 大宋提刑官 阅读(104) 评论(0) 编辑


