Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8580485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:50:28+00:00 2026-06-11T20:50:28+00:00

How the static constructor is behaving here ? class a { public static int

  • 0

How the static constructor is behaving here ?

class a
{    
    public static int x;
    static a()
    {
        x = b.y + 1;    
    }
}

class b    
{    
    public static int y = a.x + 1;

    static b()
    { 

    }

    static void Main(String[] args)
    {
       Console.WriteLine("x={0} , y={1} ", a.x, b.y);
        Console.ReadLine();    
    }
}

Output ::

x=1 , y=2

How ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T20:50:29+00:00Added an answer on June 11, 2026 at 8:50 pm

    This is, except for case, the exact example given here, which gives the following explanation:

    To execute the Main method, the system first runs the initializer for B.Y, prior to class B’s static constructor. Y’s initializer causes A’s static constructor to be run because the value of A.X is referenced. The static constructor of A in turn proceeds to compute the value of X, and in doing so fetches the default value of Y, which is zero. A.X is thus initialized to 1. The process of running A’s static field initializers and static constructor then completes, returning to the calculation of the initial value of Y, the result of which becomes 2.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class Program { static void Main(string[] args) { Foo.Calc(Foo); } } public abstract class
The following code: static void Main(string[] args) { Console.WriteLine(0); string h = Foo.X; Console.WriteLine(2);
class Program { static void Main() { Console.WriteLine(enter main method); Console.WriteLine(Foo.X); Console.ReadKey(); } }
Given the following classes: public class Foo { static Foo() { Console.WriteLine(Foo is being
How can I get the ConstructorInfo for a static constructor? public class MyClass {
public class MyClass<T> { public static readonly String MyStringValue; static MyClass() { MyStringValue =
example to illustrate : public class Something { private static int number; static Something()
I have a static class with no static constructor, but many static members. I
In what situations do I initialize a class using a static constructor ?
I have a class with a static factory constructor which returns a pointer to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.