Git Product home page Git Product logo

Comments (2)

steinbitglis avatar steinbitglis commented on August 26, 2024 1

Here's a related interesting bit:
...
The static ctor for BaseClass isn't required to be run at all. No static members have been accessed.

I think you just mixed up BaseClass and BaseClass2 there.

from docs.

BillWagner avatar BillWagner commented on August 26, 2024

Here's a related interesting bit:

using static System.Console;

class BaseClass {
    static protected int c = 1;
    static BaseClass() => WriteLine($"{c++}. Static   constructor of *BaseClass*    called.");
    public BaseClass() => WriteLine($"{c++}. Instance constructor of *BaseClass*    called.");
}
class DerivedClass : BaseClass {
    static DerivedClass()          => WriteLine($"{c++}. Static   constructor of  DerivedClass  called.");
    public DerivedClass() : base() => WriteLine($"{c++}. Instance constructor of  DerivedClass  called.");
}

public static class C {
    public static int c = 1;
}
class BaseClass2 {
    static BaseClass2() => WriteLine($"{C.c++}. Static   constructor of *BaseClass2*   called.");
    public BaseClass2() => WriteLine($"{C.c++}. Instance constructor of *BaseClass2*   called.");
}
class DerivedClass2 : BaseClass {
    static DerivedClass2()          => WriteLine($"{C.c++}. Static   constructor of  DerivedClass2 called.");
    public DerivedClass2() : base() => WriteLine($"{C.c++}. Instance constructor of  DerivedClass2 called.");
}

public class Program {
    public static void Main() {
        var instance = new DerivedClass();
        WriteLine();
        var instance2 = new DerivedClass2();
    }
}

The output is:

1. Static   constructor of *BaseClass*    called.
2. Static   constructor of  DerivedClass  called.
3. Instance constructor of *BaseClass*    called.
4. Instance constructor of  DerivedClass  called.

1. Static   constructor of  DerivedClass2 called.
5. Instance constructor of *BaseClass*    called.
2. Instance constructor of  DerivedClass2 called.

The static ctor for BaseClass isn't required to be run at all. No static members have been accessed.

from docs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.