Languages
[Edit]
EN

C#/.NET - static constructor

11 points
Created by:
JustMike
26150

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
Native Advertising
50 000 ad impressions - 449$
🚀
Get your tech brand or product in front of software developers.
For more information contact us:
Red dot
Dirask - friendly IT community for everyone.

❤️💻 🙂

Join