Shambhu
Can a non static class have static constructor?
By Shambhu in C# on Dec 16 2012
  • RATNESH SAHU
    Feb, 2016 11

    how can trust

    • 1
  • Dinesh Bhoje
    Jan, 2013 17

    Yes Normal class can have static constructor but only one. it automatically called only once when any of class member is first time called or access.. even at instance creation. And important thing is static constructor should be parameterless...

    • 1
  • Anil Kumar Murmu
    Jan, 2016 15

    yes we can have static constructor inside a non-static class.

    • 0
  • Ashish
    Apr, 2014 24

    Yes, it can.But user will not have any control on its invoke.

    • 0
  • Ashish
    Apr, 2014 22

    Yes, we can have static constructor . But user have no control over evoking it.

    • 0
  • Ravikumar G
    May, 2013 14

    Yes, It can have.. http://msdn.microsoft.com/en-us/library/ms173116%28v=vs.80%29.aspx

    • 0
  • Sandeep Mhatre
    Apr, 2013 23

    yes it is possible, Refer this link http://sandeepmhatre.blogspot.com/2013/03/constructors.html

    • 0
  • Tanuj Khurana
    Feb, 2013 25

    but we can not use this value every time variable is 0

    • 0
  • jeevan prasad
    Feb, 2013 12

    yes we can create the static constructor in non static class... it is parameter less and how static constructor called only once when how many number of objects created to the that class static fields only one reference

    • 0
  • Shambhu
    Dec, 2012 16

    Yes, it is possible.
    But we have to create an object of the class inside the static constructor and then initialize the non static member through the objectreference.

    And to initialize static member of the class too.


    class Class2
    {
    int a;
    static Class2()
    {
    Class2 p = new Class2();
    p.a = 45;
    System.Console.WriteLine(p.a);
    }
    static void Main()
    {

    }
    }

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS