An instance constructor is run before an instance is used, and a static constructor is run once before a class is used:

class Forest { static Forest() { /* ... */ } }

This constructor is run when either one of these events occurs:

  • Before an object is made from the type.
  • Before a static member is accessed.

In other words, if this was the first line in Main(), a static constructor for Forest would be run:

Forest f = new Forest();

It would also be run if this was the first line in Main():


Typically we use static constructors to set values to static fields and properties.

A static constructor does not accept an access modifier.



In the previous exercises our treeFacts and forestsCreated fields were never given values! We’ll fix that.

First, create a static constructor for Forest.


In the body of the static constructor, set the treeFacts field to this string:

"Forests provide a diversity of ecosystem services including:\r\n aiding in regulating climate.\r\n purifying water.\r\n mitigating natural hazards such as floods.\n"

In the body of the static constructor, set the ForestsCreated property to 0.


In Program.cs, call Forest.PrintTreeFacts() to check that the TreeFacts property was set.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?