Software & Finance





C# Static Variable to Count the Function Calls





 

I have given here C# code to explain how a static variable can be used to count the function call.

 

 

Source Code


using System;

using System.Collections.Generic;

using System.Text;

 

namespace FunctionCall

{

   class MyClass

   {

      static int count = 0; 

      public void TestFunction()

      {

         System.Console.WriteLine("Number of times called: {0}", ++count);

      }

   }

   class StaticCountExample

   {

 

      static void Main(string[] args)

      {

         MyClass m = new MyClass();

         for (int i = 0; i < 5; i++)

            m.TestFunction();

      }

   }

  

}

Output


Number of times called: 1

Number of times called: 2

Number of times called: 3

Number of times called: 4

Number of times called: 5

Press any key to continue . . .