Answer Posted / ganesh
A static constructor is used to initialize any static data,
or to perform a particular action that needs performed once
only. It is called automatically before the first instance
is created or any static members are referenced.
Static constructors have the following properties:
• A static constructor does not take access modifiers or
have parameters.
• A static constructor is called automatically to initialize
the class before the first instance is created or any static
members are referenced.
• A static constructor cannot be called directly.
• The user has no control on when the static constructor is
executed in the program.
• A typical use of static constructors is when the class is
using a log file and the constructor is used to write
entries to this file.
• Static constructors are also useful when creating wrapper
classes for unmanaged code, when the constructor can call
the LoadLibrary method.
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.");
}
}
class TestBus
{
static void Main()
{
Bus.Drive();
}
}
| Is This Answer Correct ? | 6 Yes | 0 No |
Post New Answer View All Answers
Why do we need oops in c#?
Why do I get a syntax error when trying to declare a variable called checked?
How do you declare an interface in c#?
What is string in c# net?
list the steps in code compilation in c#?
What is escape character in c#?
Can we write class inside a class in c#?
What is difference between const and static in c#?
Explain async and await?
What is public or shared assemblies ?
Explain the difference between // comments, /* */ comments and /// comments?
Why should I use interface in c#?
What is the default modifier for class in c#?
Is array reference type in c#?
Is string passed by reference in c#?