Results 1 to 2 of 2

Thread: Static Constructors in C# (C sharp )

  1. #1
    Join Date
    Jan 2009
    Posts
    16

    Static Constructors in C# (C sharp )

    Hello friends

    currently i am searching info about c#. I want some info about static constructor in C# can some one know about this plz provide me information.

    Thanks

  2. #2
    Join Date
    Jan 2009
    Posts
    23

    Re: Static Constructors in C# (C sharp )

    Whenever a class or struct is created, its constructor is called. A class or struct may have multiple constructors that take different arguments. Constructors allow the programmer to set default values, limit instantiation, and write code that is flexible and easy to read.

    If you do not provide a constructor for your object, C# will create one by default that instantiates the object and sets any member variables to the default values listed here: Default Values Table (C# Reference). Static classes and structs can also have constructors.

    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.

    Code:
    class SimpleClass
    {
        // Static constructor
        static SimpleClass()
        {
            //...
        }
    }
    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.


    Example

    In this example, the class Bus has a static constructor and one static member, Drive(). When Drive() is called, the static constructor is invoked to initialize the class.

    Code:
    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();
        }
    }

Similar Threads

  1. Sharp 007SH Hybrid An 3D Clamshell Android Phone by Sharp
    By jackalz in forum Web News & Trends
    Replies: 3
    Last Post: 22-05-2011, 07:28 AM
  2. what are constructors in C++?
    By Asis in forum Software Development
    Replies: 4
    Last Post: 29-12-2010, 07:38 AM
  3. Constructors and Destructors in PHP
    By garfield1 in forum Software Development
    Replies: 4
    Last Post: 02-03-2010, 10:01 PM
  4. Make a static reference to the non-static field
    By Aaliya Seth in forum Software Development
    Replies: 5
    Last Post: 02-03-2010, 10:11 AM
  5. What are the Constructors and Destructors?
    By RupaliP in forum Software Development
    Replies: 4
    Last Post: 27-02-2009, 06:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •