Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C销毁或禁用类的特定对象_C#_List_Class_Object_Destroy - Fatal编程技术网

C# C销毁或禁用类的特定对象

C# C销毁或禁用类的特定对象,c#,list,class,object,destroy,C#,List,Class,Object,Destroy,我有一个windows窗体和一个名为testclass.cs的类。下面的示例代码适用于testclass.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace testApp { public class testclass { public void disableEverything() {

我有一个windows窗体和一个名为testclass.cs的类。下面的示例代码适用于testclass.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace testApp
{
     public class testclass
    {
       public void disableEverything()
       {
           //some operations goes here
       }
       public void welcome()
       {
         //some code goes here
       }
    }
 } 
在我的表单中,我有在按钮点击事件中创建对象的代码 下面的代码显示了表单1

       using System;
       using System.Collections.Generic;
       using System.Linq;
       using System.Text;

       namespace testApp
       {
          public partial class Form1: Form
          {
            private void button1_Click(object sender, EventArgs e)
            {
               testclass obj = new testclass();
               obj.welcome();

            }
          }
        }
我的问题是,如果我单击按钮1五次,它将创建testclass类的5个对象。
假设我想调用一些特殊类对象(如第三个对象、第二个对象)的disableEverything方法,我该如何调用它?我应该使用列表和调用列表[index]。禁用所有内容。请为我推荐一个好的解决方案,我不知道您要实现什么,但对于非托管资源,您可以实现IDisposable,并在工作后使用释放资源:

namespace testApp
{
  public class testclass : IDisposable
  {
      bool disposed = false;
     //Instantiate a SafeHandle instance.
      SafeHandle handle = new SafeFileHandle(IntPtr.Zero, true);
    // Public implementation of Dispose pattern callable by consumers.
    public void Dispose()
   { 
     Dispose(true);
     GC.SuppressFinalize(this);           
   }

   // Protected implementation of Dispose pattern.
   protected virtual void Dispose(bool disposing)
   {
     if (disposed)
      return; 
     if (disposing) {
      handle.Dispose();
      // Free any other managed objects here.
      //
    }

    // Free any unmanaged objects here.
     //
    disposed = true;
   }  


    public void welcome()
    {
         //some code goes here
     }
   }
 }

private void button1_Click(object sender, EventArgs e)
{
       using (var obj = new testclass())
       {

       }
 }

我知道你名副其实了。在类中销毁类?即使允许也没有意义。你的问题是关于什么的?更具体一点。如果您不想创建此类的多个实例,则应将该对象放置在全局中,并在窗体加载时对其进行实例化。此外,您还应了解IDisposable,而不是制作自己的版本。@JohnyL,这就像禁用某些东西,例如,使用对象将特定类的bool值设置为false。接口实现在哪里?忘记销毁对象吧。我想问的是,如何才能使这些特定对象仍然处于活动状态并调用该类的某些方法如果您在全局范围内创建对象,那么只要表单处于活动状态,这些对象就会一直处于活动状态,那么您为什么需要检查它?@Zorkind我正在创建相同类类型的多个对象。不是单个对象OK,所以在全局中列出它的列表/字典/数组,并将它们全部放在其中。