0

I am new to c#. Please guide me to find out the solution of this problem. I have a static class A. Inside this I have a static constructor. The code is below:

using System; 
using System.Collections.Generic;
using System.Linq; 
using System.Text;
namespace ConsoleApplication6 
{
    class Program 
    { 
        static void Main(string[] args) 
        { 
        } 
    } 
    static class A 
    { 
        static A() 
        { 
            Console.WriteLine("static constructor is called."); 
        } 
    } 
}

How can I access this static constructor in c#?

Gobind
  • 588
  • 1
  • 7
  • 18
  • What part of the documentation is unclear to you? – Jeroen Vannevel Jul 01 '17 at 12:24
  • 1
    It is called automatically before the first instance is created or any static members are referenced. see here:https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-constructors – Ofir Winegarten Jul 01 '17 at 12:24
  • If you need to call a constructor its counterproductive to make the class static. Just make it a normal class. – ckuri Jul 01 '17 at 12:26
  • using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication6 { class Program { static void Main(string[] args) { } } static class A { static A() { //Console.WriteLine("static constructor is called."); } } } – Gobind Jul 01 '17 at 12:30
  • my program is like above, I want to invoke the static constructor A inside the static class A.Then how to invoke this? – Gobind Jul 01 '17 at 12:31
  • @Gobind edit your question and move the code of the program to it instead of posting it in the comments – Samvel Petrosov Jul 01 '17 at 12:32
  • Is there an actual reason to not make the class non-static and just call the non-static constructor? – ckuri Jul 01 '17 at 12:41
  • There is no specific reason for this. But this is for my learning purpose and I want to know the actual reason behind this. @ckuri – Gobind Jul 01 '17 at 12:47
  • There are no members in this class you've posted. only a static constructor. The constructor will never be called. – Ofir Winegarten Jul 01 '17 at 12:50
  • If I add some fields, then can I invoke the constructor?@OfirWinegarten – Gobind Jul 01 '17 at 12:53
  • If you add a public static method or property, then the constructor will be called before the first time you call them – Ofir Winegarten Jul 01 '17 at 12:54
  • @Gobind look at my answer, I have explained both cases when the class is static and when it isn't – Samvel Petrosov Jul 01 '17 at 12:56

2 Answers2

7

You can't. As it is written in MSDN Article about Static Classes :

A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated. In other words, you cannot use the new keyword to create a variable of the class type. Because there is no instance variable, you access the members of a static class by using the class name itself.

Also I will suggest you to read this article too

Static Constructors (C# Programming Guide)

As there is written you can't call static constructor

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.

Static constructors have the following properties:

  • A static constructor does not take access modifiers or have parameters.

  • A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.

  • A static constructor cannot be called directly.

  • The user has no control on when the static constructor is executed in the program

Below is example how the static constructor works.

using System; 
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication6 
{
    public class Program 
    { 
        public static void Main(string[] args) 
        { 
            A myClass1WithStaticConstructor = new A();
            A myClass2WithStaticConstructor = new A();
        } 
    } 
    public class A 
    { 
        public A()
        {
            Console.WriteLine("default constructor is called."); 
        }
        static A() 
        { 
            Console.WriteLine("static constructor is called."); 
        } 
    } 
}

And the output will be the following:

static constructor is called.
default constructor is called.
default constructor is called.

So from the output we see that the static constructor was called only for the first time.

Also in case if you want to use it with Static Class here is example for it:

using System; 
using System.Collections.Generic;
using System.Linq; 
using System.Text;
namespace ConsoleApplication6 
{
    public class Program 
    { 
        public static void Main(string[] args) 
        { 
            Console.WriteLine(A.abc);
        } 
    } 
    public static class A 
    { 
        public static int abc;

        static A() 
        { 
            abc=10;
            Console.WriteLine("static constructor is called."); 
        } 
    } 
}

The output will be the following:

static constructor is called.
10

So we see that the static constructor is automatically called in this case too.

Samvel Petrosov
  • 7,580
  • 2
  • 22
  • 46
  • using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication6 { class Program { static void Main(string[] args) { } } static class A { static A() { //Console.WriteLine("static constructor is called."); } } } – Gobind Jul 01 '17 at 12:32
  • my program is like above, I want to invoke the static constructor A inside the static class A.Then how to invoke this? – Gobind Jul 01 '17 at 12:33
  • But I can't create an instance of static class. Then how can i invoke the static class in this specific program to print this message"Console.WriteLine("static constructor is called.")" – Gobind Jul 01 '17 at 12:42
  • @Gobind take a look at my answer I have updated it. I think now you will understand how static constructor works. – Samvel Petrosov Jul 01 '17 at 12:49
  • Thanks for your comment. I understood this. – Gobind Jul 01 '17 at 12:58
  • Just to be clear, one *can* call static constructor, but I would doubt that it is necessary in any case. https://stackoverflow.com/a/2654684/6804888 – kiziu Jul 01 '17 at 17:08
-1

Static constructors cannot be called directly. See learn.microsoft.com about static constructors

waka
  • 3,362
  • 9
  • 35
  • 54