Create class and create 5 instances and get count of how many instances of one class are created

 // Online C# Editor for free

// Write, Edit and Run your C# code using C# Online Compiler

using System;

public class HelloWorld

{

    public static void Main(string[] args)

    {

        // Create 5 instances of Employee class

            Instance instances1 = new Instance();

            Instance instances2 = new Instance();

            Instance instances3 = new Instance();

            Instance instances4 = new Instance();

            Instance instances5 = new Instance();

         // Get and display instance count

            Console.WriteLine("Total Employee instances created: " + Instance.GetInstanceCount());

            Console.ReadLine();        

    }

}

    // Define a class

    public class Instance

    {

        // Static variable to count instances

        private static int instanceCount = 0;

        // Constructor

        public Instance()

        {

            instanceCount++;

        }

        // Static method to get count

        public static int GetInstanceCount()

        {

            return instanceCount;

        }

    }

Comments

Popular posts from this blog

Refresh an Angular Component without reloading the same Component

.NET Core vs .NET Framework: How to Pick a .NET Runtime for an Application

multi color initial(Avatars) if img not available in Angular it's work angular 2,4+ all versions