static void Main(string[] args)
Class members (variables, properties, methods) can also be declared as static. This makes those members belong to the class itself, instead of belonging to individual objects. No matter how many objects of the class are created, there is only one copy of the static member.
class Cat {
public static int count=0;
public Cat() {
count++;
}
}
In this case, we declared a public member variable count, which is static. The constructor of the class increments the count variable by one.
No matter how many Cat objects are instantiated, there is always only one count variable that belongs to the Cat class because it was declared static.
class Dog
{
public static void Bark() {
Console.WriteLine("Woof");
}
}
static void Main(string[] args)
{
Dog.Bark();
}
// Outputs "Woof"
The Main method is static, as it is the starting point of any program. Therefore any method called directly from Main had to be static.
class MathClass {
public const int ONE = 1;
}
static void Main(string[] args) {
Console.Write(MathClass.ONE);
}
//Outputs 1
As you can see, we access the property ONE using the name of the class, just like a static member. This is because all const members are static by default.
The static constructor is automatically called once when we access a static member of the class.
class SomeClass {
public static int X { get; set; }
public static int Y { get; set; }
static SomeClass() {
X = 10;
Y = 20;
}
}
We cannot instantiate an object of a static class, as only one instance of the static class can exist in a program.
Static classes are useful for combining logical properties and methods. A good example of this is the Math class. It contains various useful properties and methods for mathematical operations.
- Math.PI the constant PI.
- Math.E represents the natural logarithmic base e.
- Math.Max() returns the larger of its two arguments.
- Math.Min() returns the smaller of its two arguments.
- Math.Abs() returns the absolute value of its argument.
- Math.Sin() returns the sine of the specified angle.
- Math.Cos() returns the cosine of the specified angle.
- Math.Pow() returns a specified number raised to the specified power.
- Math.Round() rounds the decimal number to its nearest integral value.
- Math.Sqrt() returns the square root of a specified number.
Console.WriteLine(Math.Pow(2, 3));
//Outputs 8
int[] arr = {1, 2, 3, 4};
Array.Reverse(arr);
//arr = {4, 3, 2, 1}
Array.Sort(arr);
//arr = {1, 2, 3, 4}
string s1 = "some text";
string s2 = "another text";
String.Concat(s1, s2); // combines the two strings
String.Equals(s1, s2); // returns false
DateTime.Now; // represents the current date & time
DateTime.Today; // represents the current day
DateTime.DaysInMonth(2016, 2);
//return the number of days in the specified month
The Console class is also an example of a static class. We use its static WriteLine() method to output to the screen, or the static ReadLine() method to get user input.
The Convert class used to convert value types is also a static class.