Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

When static constructor is invoked?

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


Please Help Members By Posting Answers For Below Questions

How do you pass reference parameters in c#?

861


Do unused Namespaces in c# affect run-time performance?

983


How can we set the class to be inherited, but prevent the method from being over-ridden?

921


Write a sample code to write the contents to text file in c#?

845


Explain the Different types of configuration files in .net?

931


Why do we use static methods in c#?

964


What is parallel foreach c#?

900


Is c++ or c# better?

889


Is it possible to restrict the scope of a field/method of a class to the classes in the same namespace?

936


What is exe in c#?

864


Can you store strings in arrays?

937


Can a class or a struct have multiple constructors?

901


What is different between Boxing and Unboxing?

951


What is the use of delegate?

922


What is a virtual property. Give an example?

890