C# Program to Get the Count of Total Created Objects Last Updated : 30 Sep, 2021 Comments Improve Suggest changes Like Article Like Report C# is a general-purpose programming language it is used to create mobile apps, desktop apps, websites, and games. In C#, an object is a real-world entity. Or in other words, an object is a runtime entity that is created at runtime. It is an instance of a class. In this article, we will create multiple instances or objects of a class and count the number of objects created in C#. Approach: Create a class with the name DemoClass for which we are creating multiple objects.We create a class ( .i.e. GetCount) with the main method from which we will create multiple objects of the DemoClass.In the Democlass, we are going to define a static variable count and initialize it to 0.Create a constructor for DemoClass which increments the count whenever the object of DemoClass is created.Here we are defining it as static because a single copy of the variable is created and shared among all objects at the class level, so whenever we create a new object the value of count will not be lost.A static method (i.e. TotalCount ) is created to return the count value, Static methods are methods that are called on the class itself, not on an object.In the main method create multiple objects of DemoClass and call the TotalCount method using the class name (i.e. DemoClass). Example 1: C# // C# program to illustrate the above concept using System; class DemoClass{ // The static variable count is used to store // the count of objects created. static int count = 0; // Constructor of the class, in which count // value will be incremented public DemoClass() { count++; } // Method totalcount is used to return // the count variable. public static int TotalCount() { return count; } } class GFG{ static void Main(string[] args) { // Printing the number of objects before // creating objects. Console.WriteLine("Total objects = " + DemoClass.TotalCount()); // Creating the objects of DemoClass DemoClass C1 = new DemoClass(); DemoClass C2 = new DemoClass(); DemoClass C3 = new DemoClass(); // Printing the number of objects after // creating 3 objects. Console.WriteLine("Total objects = " + DemoClass.TotalCount()); } } Output: Total objects = 0 Total objects = 3 Example 2: C# // C# program to illustrate the above concept using System; class DemoClass{ // Here, the static variable count is used to // store the count of objects created. static int count = 0; // constructor of the class, in which // count value will be incremented public DemoClass() { count++; } // Method totalcount is used to return // the count variable. public static int TotalCount() { return count; } } class GFG{ static void Main(string[] args) { // Printing the number of objects before // creating objects. Console.WriteLine("Total objects = " + DemoClass.TotalCount()); // Creating the objects of DemoClass DemoClass C1 = new DemoClass(); DemoClass C2 = new DemoClass(); DemoClass C3 = new DemoClass(); DemoClass C4 = new DemoClass(); DemoClass C5 = new DemoClass(); DemoClass C6 = new DemoClass(); // Printing the number of objects after // creating 3 objects. Console.WriteLine("Total objects = " + DemoClass.TotalCount()); } } Output: Total objects = 0 Total objects = 6 Comment More infoAdvertise with us Next Article C# Program to Get the Count of Total Created Objects P pulamolusaimohan Follow Improve Article Tags : C# CSharp-programs Similar Reads C# | Count the total number of elements in the List List class represents the list of objects which can be accessed by index. It comes under the System.Collection.Generic namespace. List class can be used to create a collection of different types like integers, strings etc. List class also provides the methods to search, sort, and manipulate lists. L 2 min read Program to count the number of characters in a word Write a program to count the number of characters in a word. Examples: Input: Word: "programming"Output: 11 Input: Word: "hello"Output: 5 Approach: To solve the problem, follow the below idea: To count the number of characters in a word, maintain a variable to count all the characters. Iterate throu 2 min read C# | Count the number of key/value pairs in the Hashtable The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. The key is used to access the items in the collection. Hashtable.Count Property is used to get the total number of the key/value pairs contained in the Hashtable. Syntax: myTable. 2 min read Program to count the number of words in a sentence Write a program to count the number of words in a given sentence. A word is defined as a sequence of characters separated by spaces. Examples: Input: "The quick brown fox"Output: 4Explanation: The sentence has four words - "The", "quick", "brown", "fox". Input: "The quick brown fox"Output: 4Explanat 3 min read C# | Get the number of elements contained in the Stack Stack represents a last-in, first out collection of object. Stack<T>.Count Property is used to gets the number of elements contained in the Stack. Retrieving the value of this property is an O(1) operation. Syntax: myStack.Count Here myStack is the name of the Stack<T> Return Value: The 2 min read Like