.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Static Constructors

Posted By:Shashi Ray       Posted Date: March 31, 2009    Points: 25    Category: ASP.NET    URL: http://www.dotnetspark.com  
 

 Static Constructors:

  1. There can be only one static constructor in the class.
  2. The static constructor should be without parameters.
  3. It can only access the static members of the class.
  4. There should be no access modifier in static constructor definition.
  5. A static constructor does not take access modifiers or have parameters.
  6. A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
  7. A static constructor cannot be called directly.
  8. The user has no control on when the static constructor is executed in the program.
  9. 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.
  10. Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method


Firstly, the call to the static method is made by the CLR and not by the object, so we do not need to have the access modifier to it.


Secondly, it is going to be called by CLR, who can pass the parameters to it, if required. So we cannot have parameterized static constructor.

Thirdly, non-static members in the class are specific to the object instance. So static constructor, if allowed to work on non-static members, will reflect the changes in all the object instances, which is impractical. So static constructor can access only static members of the class.


Fourthly, overloading needs the two methods to be different in terms of methods definition, which you cannot do with Static Constructors, so you can have at the most one static constructor in the class.

 

Example

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

 

Copy Constructor

class Person
{
    private string name;
    private int age;
    // Copy constructor.
    public Person(Person previousPerson)
    {
        name = previousPerson.name;
        age = previousPerson.age;
    }

    // Instance constructor.
    public Person(string name, int age)
   {
        this.name = name;
        this.age = age;
    }
    // Get accessor.
    public string Details
    {
        get
        {
            return name + " is " + age.ToString();
        }
    }
}

class TestPerson
{
    static void Main()
    {
        // Create a new person object.
        Person person1 = new Person("George", 40);
        // Create another new object, copying person1.
        Person person2 = new Person(person1);
        System.Console.WriteLine(person2.Details);
    }
}

 

 

using System;

using System.Collections.Generic;

 

 

namespace Model

{

    ///

    ///

    ///

    ///

    [Serializable]

    public class SearchType

    {

        // The available search types

        private const string SEARCH_BY_ID = null;

        private const string SEARCH_BY_CODE = null;

 

        private readonly string _id;

        ///

        /// The id used to represent this search type

        ///

        public string Id

        {

            get { return _id; }

        }

 

        private readonly string _description;

        ///

        /// A description of the Search Type

        ///

        public string Description

        {

            get { return _description; }

        }

        ///

        /// An instance of a Search type representing SearchType only with No

        ///

                public static readonly SearchType SearchById = new SearchType (SEARCH_BY_ID, " Reference");

        ///

        /// An instance of a Search type representing SearchType only with Agency Code

        ///

        public static readonly SearchType SearchByCode = new SearchType(SEARCH_BY_ CODE, " Code");

        ///

        /// overriding toString() method

        ///

        /// A description of the Search Type

        public override string ToString()

        {

            return Description;

        }

        ///

        /// Private parameterize constructor to initialized object

        ///

        /// string

        /// string

        private SearchType(string id, string description)

        {

            _id = id;

            _description = description;

        }

    }

}

 

Shashi Ray


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend