Languages
[Edit]
EN

C#/.NET - static constructor

11 points
Created by:
JustMike
3491

In C#/.NET static constructor can be created with static keyword.

using System;

namespace Test
{
	public class Utils
	{
		public static string Name { get; private set; }

		static Utils()
		{
			Name = "Example utils";
		}
	}

	public class Program
	{
		static void Main(string[] args)
		{
			Console.WriteLine(Utils.Name);
		}
	}
}

Output:

Example utils
Note: static constructor is called only once during first class usage.

References:

  1. Static Constructor - Microsoft Docs
Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.