Class ExampleTest
{
Static ExampleTest ()
{
Console. WriteLine ("test static default constructor in unstatic class ");
}
Public ExampleTest ()
{
Console. WriteLine ("test default constructor in unstaic class ");
}
}
Static class ExampleTest2
{
Static ExampleTest2 ()
{
Console. WriteLine ("test static Method in static class ");
}
# Region static classes cannot call instance Constructors
// Public ExampleTest2 ()
//{
// Console. WriteLine ("test unstatic method in static class ");
//}
# Endregion
# Region static classes cannot call instance methods
// Public void getAlert ()
//{
// Console. WriteLine ("Alert, alert ");
//}
# Endregion
Public static void getAlert ()
{
Console. WriteLine ("Alert, alert ");
}
}
From the above two classes, we can draw a conclusion:
1. constructor: whether in a static or non-static class, if we define a static constructor, we only need to create an instance of the class or call the method of the class, will automatically call this Static constructor, and the Static constructor cannot have access permissions. Static constructors cannot have parameters.
2. instance constructors cannot be called in static classes.
2. Non-static methods cannot be created in static classes. That is, only static methods can be created in static methods, but static methods can be called in non-static classes (which is often used in this case)
____________________________________________________________________________________
Static constructor:
(1) It is used to initialize static and read-only fields.