Posts

....
Technical Blog for .NET Developers ©

Monday, July 22, 2019

Static Constructors

Static Constructors initialize the static fields/properties of the class/static class. Static Constructors donot allow access modifiers so will work in background with the instructions of initialization of static resources in the class

In this post we exemplify the construction of a class containing static fields


    public class Particle<T> where T : new()
    {
        static T StaticElement { get; set; }

        public T Element { get; set; }

        public double Density { get; set; }

        // ctor
        public Particle(double density)
        {
            Density = density;
        }

        // static ctor
        static Particle()
        {
            StaticElement = new T();
        }

        public Particle<T> MuteParticle(Func<T, Particle<T>> muteFunction)
        {
            return muteFunction(StaticElement);
        }
    }



.2018