Yogesh Ghorpade
Can I Create The Constructor Of Static Class
By Yogesh Ghorpade in C# on Jul 10 2018
  • Jignesh Kumar
    Jul, 2018 29

    Hi All, I am not agree with below answer. We can create static constructor without any access modifier. We can not create parameter constructor for static class. Please try this public static class MyStaticClass{public static int i;static MyStaticClass(){i = 10;}}

    • 14
  • Yogesh Ghorpade
    Jul, 2018 10

    No - Static Class Constructor Having A Default Constructor With protected Access Modifier

    • 5
  • Nikhil Sangani
    Aug, 2018 1

    We can create static constructor without any access modifier. We can not create parameter constructor for static class.Try this one. : static class MyClass {static MyClass(){} }

    • 4
  • Abdurrahman Awan
    Aug, 2018 27

    You can not create an instance of class using new keyword. Rather, all functionality is exposed from the class level. So there is no need for a constructor in any Static class.

    • 3
  • Gopaiah Nekarukanti
    Jul, 2018 22

    No, we can't define constructor in Static Class & we can't create instances for Static Class.

    • 3
  • Jignesh Kumar
    Jul, 2018 27

    Yes, we can but that has some rules, please check this link https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-constructors

    • 2
  • Vijay Kumar
    Mar, 2019 29

    Yes, we can create the constructor of static class , But the purpose is only initialize the static variable values Because static constructor call before normal class constructor

    • 1
  • Vikas Agarwal
    Oct, 2018 5

    Yes, Static class can have static constructor

    • 1
  • Sandeep Kumar
    Aug, 2018 27

    Yes, we can create a constructor of static class, but constructor should be static with no parameter and without access modifier.

    • 1
  • resume Mohite
    Aug, 2018 23

    Yes , we can create the constructor of static class.We use static constructor to initialise the static fields.Static creates single copy and that can be shared by multiple object, so here we achieve memory optimization. We also initialise our static fields in the non-static constructor but as how many times we create the object of that class that many times constructor get called.So,memory get allocated for every time.But the basic moto of static is memory optimization. Static constructor get called in to cases :So we create the constructor of static class.

    • 1
  • preeti agrawal
    Aug, 2018 13

    Yes, we can create a parameter less static constructor for a static class. It is used to initialize static members of the class. Static class must have all members as static.

    • 1
  • Ayappan Alagesan
    Jul, 2018 31

    No, Static class cannot have ctor , Compile time error.

    • 1
  • Rajeev Kumar
    Mar, 2023 7

    Yes, we can create a parameter less static constructor for a static class. It is used to initialize static members of the class. Static class must have all members as static.

    • 0
  • Archana Sagare
    Aug, 2021 3

    Yes,We can create Static constructor to Static Class but we can not create instance constructor to static class.

    • 0
  • Atish Soni
    Oct, 2019 10

    Ofcourse we can create the Static constructor without any access modifier. Please take a look on below eaxmple

    class Program
    {
    static void Main(string[] args)
    {
    MyStaticClass.printResult();
    Console.ReadLine();
    }

    }

    public static class MyStaticClass {
    public static int num;
    static MyStaticClass()
    {
    num = 1000;
    }
    public static void printResult()
    {
    Console.WriteLine(num);
    }
    }

    • 0
  • VAMSI KUMAR NAMA
    Aug, 2019 13

    1. class Program
    2. {
    3. static void Main(string[] args)
    4. {
    5. sClass.printMethod();
    6. Console.WriteLine(sClass.num1 + sClass.num2);
    7. Console.ReadLine();
    8. }
    9. }
    10. static class sClass
    11. {
    12. public static int num1;
    13. public static int num2;
    14. static sClass()
    15. {
    16. num2 = 10;
    17. Console.WriteLine("static constructor print");
    18. }
    19. public static void printMethod()
    20. {
    21. Console.WriteLine("it is printing from the static print method");
    22. }
    23. }

    • 0
  • VAMSI KUMAR NAMA
    Aug, 2019 13

    static class sClass
    {
    public static int num1;
    public static int num2;
    static sClass()
    {
    num2 = 10;
    Console.WriteLine(“static constructor print”);
    }
    public static void printMethod()
    {
    Console.WriteLine(“it is printing from the static print method”);
    }
    }

    1. class Program
    2. {
    3. static void Main(string[] args)
    4. {
    5. sClass.printMethod();
    6. Console.WriteLine(sClass.num1 + sClass.num2);
    7. Console.ReadLine();
    8. }
    9. }

    • 0
  • Saurabh Vasani
    Aug, 2019 9

    Yes , we can create constructor of static class but we cant create static constructor with parameter. in short static constructor is parameterless constructor and we just create static constructor once.

    • 0
  • sushil kumar
    Feb, 2019 20

    Yes we can create. but create only static constructor without any modifier in static class.

    • 0
  • Amit Prakash
    Jan, 2019 2

    No you can't

    • 0
  • Bhanuprasad Mitturi
    Nov, 2018 20

    Static class can have static constructor with no access modifier and must be a parameter less constructor,since static constructor will be called automatically when class loads , no point of passing parameters.

    • 0
  • Lokpal Vora
    Oct, 2018 5

    We can not create constructor of static class, however we can create static constructor for Non static class.

    • 0
  • Pathakoti Rajesh
    Sep, 2018 19

    Yes, We can create itstatic class YourClass {static YourClass(){// perform initialization here} } It is executed only once when the type is first used. All classes can have static constructors, not just static classes.

    • 0
  • Pathakoti Rajesh
    Sep, 2018 19

    Yes, we can create a constructor of static class and it's a static constructor.static class YourClass {static YourClass(){// perform initialization here} }It is executed only once when the type is first used. All classes can have static constructors, not just static classes.

    • 0
  • Gopal Sharma
    Sep, 2018 4

    Yeah, Static constructor can be create in static class.

    • 0
  • Jitendra Sharma
    Aug, 2018 20

    yes we create ,static constructor that initialze the static field...but it is parameterless

    • 0
  • mohit shukla
    Aug, 2018 18

    no

    • 0
  • Manali Ghugare
    Aug, 2018 14

    Yes we can create the constructor of static class.static class A {static a(){//} }A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only. It is called automatically before the first instance is created or any static members are referenced.

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS