📜  获取创建对象总数的 C# 程序

📅  最后修改于: 2022-05-13 01:54:52.122000             🧑  作者: Mango

获取创建对象总数的 C# 程序

C# 是一种通用编程语言,用于创建移动应用程序、桌面应用程序、网站和游戏。在 C# 中,对象是现实世界的实体。或者换句话说,对象是在运行时创建的运行时实体。它是一个类的实例。在本文中,我们将创建一个类的多个实例或对象,并计算在 C# 中创建的对象的数量。

方法:

示例 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());
}
}


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());
}
}


输出:

Total objects = 0
Total objects = 3

示例 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());
}
}

输出:

Total objects = 0
Total objects = 6