1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

What is Static Constuctor

Discussion in 'C#' started by webmaster8757, Sep 13, 2010.

  1. #1
    Hi,
    SEMrush
    I am confused about what is static constructors in c#.Please help me by giving some suitable examples.
     
    webmaster8757, Sep 13, 2010 IP
    SEMrush
  2. Employwise

    Employwise Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Static constructor is used to initialize static data members as soon as the class is referenced first time.
    like. Here Int id is a static member.

    class test
    {
    static int id = 5;
    static test()
    {
    id=5;
    }
    }
     
    Employwise, Sep 21, 2010 IP
  3. Eric89

    Eric89 Peon

    Messages:
    195
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yes you are right employwise..thanks for sharing such nice information
     
    Eric89, Sep 24, 2010 IP
  4. merabasera

    merabasera Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Static constructors are used to initialize static members they are automatically initialize before the instance of class is created.
     
    merabasera, Oct 14, 2010 IP
  5. prkishnani

    prkishnani Peon

    Messages:
    164
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    hi..

    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.

    Web Designer | Real Estate Web Design
     
    prkishnani, Nov 10, 2010 IP
  6. teamaguilar

    teamaguilar Peon

    Messages:
    197
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Static constructor is used to initialize static data members as soon as the class is referenced first time, whereas an instance constructor is used to create an instance of that class with <new> keyword. A static constructor does not take access modifiers or have parameters and can't access any non-static data member of a class.
     
    teamaguilar, Nov 21, 2010 IP
  7. zinist

    zinist Banned

    Messages:
    91
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    43
    #7
    Static constructor is a special constructor that gets called before the first object of the class is created. It is used to initialize any static data, or to perform a particular action that needs performed once only.

    The time of execution of static constructor is not known but it is definitely before the first object creation - may be at the time of loading assembly.
    1. //Example1
    2. usingSystem;
    3. publicClass abc
    4. {
    5. static abc()
    6. {
    7. // initialize static members only.
    8. }
    9. // other methods here.
    10. }
    11. // Example2
    12. usingSystem;
    13. publicclassMyStaticClass
    14. {
    15. staticint count;
    16. staticMyStaticClass()
    17. {
    18. count =0;
    19. Console.WriteLine("Static class is initialized");
    20. }
    21. publicstaticvoidMyMethod(string name)
    22. {
    23. Console.WriteLine("Static class is initialized "+ name);
    24. }
    25. }
    26. MyStaticClass.MyMethod("John");//method call
    27. //constructor will be called automatically
    28. Output:
    29. Staticclass is initialized HelloJohn
    Key points about static constructor
    1. It can only access the static member(s) of the class.


      Reason : Non static member is specific to the object instance. If static constructor are allowed to work on non static members it will reflect the changes in all the object instance, which is impractical.
    2. There should be no parameter(s) in static constructor.

      Reason: Since, It is going to be called by CLR, nobody can pass the parameter to it.

    3. Only one static constructor is allowed.

      Reason: Overloading needs the two methods to be different in terms of method/constructor definition which is not possible in static constructor.

    4. There should be no access modifier to it.

      Reason: Again the reason is same call to static constructor is made by CLR and not by the object, no need to have access modifier to it
     
    zinist, Jun 26, 2015 IP
  8. jackycheung01

    jackycheung01 Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #8
    Static constructors are used to initialize static fields in a class. Static constructor is called only once, no matter how many instances you create. Static constructor are called before instance constructors.
     
    jackycheung01, Mar 9, 2017 IP