Sample 比較容易懂
Static Constructors
public class Bus
{
// Static constructor:
static Bus()
{
System.Console.WriteLine("The static constructor invoked.");
}
public static void Drive()
{
System.Console.WriteLine("The Drive method invoked.");
}
}
A slightly more informative example would be:
static void Main()
{
Bus.Drive();
Bus.Drive();
}
which gives the output:
The static constructor invoked.
The Drive method invoked.
The Drive method invoked.
Thus showing that the constructor is only called once, even though 'Drive()' was called twice.
***Static Variables***
Please enter 5 numbers to be summed
Enter a number: 12
The current total is 12
Enter a number: 23
The current total is 35
Enter a number: 34
The current total is 69
Enter a number: 45
The current total is 114
Enter a number: 56
The current total is 170
Program completed
沒有留言:
張貼留言