This example explains the followings:
  • the nature of the static field.
  • do--while(true loop)
  • waiting for blank field to exit console application.
  • showing destructor cleaning up the data those were hold in the memeory

using System;
//csc static_field_2.cs
namespace ex_static_field2 {
public class Player
public string id;
public string name;
public static int p_id = 0;
public Player()
// this will not be called due to the logic
Console.WriteLine("Blank Constructor evoked");

public Player(string name, string id)
{ = name; = id;
~Player(){ Console.WriteLine("Overloaded destructor evoked");}
public static int AddPlayer()
return ++p_id;

class MainClass : Player
static void Main()
Console.WriteLine("-->Welcome to Team roster building<--");
do {
Console.WriteLine("Number of Players: {0}",Player.p_id);
//write do -- while logic till toster is full
Console.Write("Enter the player's name: ");
string name = Console.ReadLine();
Console.Write("Enter the player's ID: ");
string id = Console.ReadLine();
Player e = new Player(name, id);
if ((id != "" )||(name !=""))
// Display the new information:
Console.WriteLine("Player added : "+ + " ID " +;
Console.WriteLine("------" + Player.p_id + "-----------");
//else break;
else return;
} while(true);
}//end of namespace

As shown in  the above figure, as we start the initial value of the static filed with "0" and as we go entering the player's name the number increases as we go on adding the players.

The program will exit when both the entries are empty. It was our intention, if ((id != "" )||(name !="")), and can be modified as desired.

At the end you would notice "Overloaded destructor evoked" would one number more than the instances where we added players. The static field as set with the initial value, it will keep adding a value over the initial value.