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;}}
No - Static Class Constructor Having A Default Constructor With protected Access Modifier
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(){} }
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.
No, we can't define constructor in Static Class & we can't create instances for Static Class.
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
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
Yes, Static class can have static constructor
Yes, we can create a constructor of static class, but constructor should be static with no parameter and without access modifier.
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.
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.
No, Static class cannot have ctor , Compile time error.
Yes,We can create Static constructor to Static Class but we can not create instance constructor to static class.
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); }}
class Program{ static void Main(string[] args) { sClass.printMethod(); Console.WriteLine(sClass.num1 + sClass.num2); Console.ReadLine(); }} 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"); }}
class Program
{
static void Main(string[] args)
sClass.printMethod();
Console.WriteLine(sClass.num1 + sClass.num2);
Console.ReadLine();
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");
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”); } }
class Program{ static void Main(string[] args) { sClass.printMethod(); Console.WriteLine(sClass.num1 + sClass.num2); Console.ReadLine(); }}
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.
Yes we can create. but create only static constructor without any modifier in static class.
No you can't
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.
We can not create constructor of static class, however we can create static constructor for Non static class.
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.
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.
Yeah, Static constructor can be create in static class.
yes we create ,static constructor that initialze the static field...but it is parameterless
no
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.