简体   繁体   中英

C# make class static?

I have a class like that:

class ContentManager : IDisposable
{
    List<int> idlist = new List<int>();

    public int Load(string path)
    {
        //Load file, give content, gets an id

        //...

        int id = LoadFile(myfilecontent);

        idlist.Add(id);
        return id;
    }

    public void Dispose()
    {
        //Delete the given content by id, stored in idlist

        foreach(int id in idlist)
        {
            DeleteContent(id);
        }
    }
}

I want to make it static, because i need only one instance and can access the function from every other class without giving an instance.

I can make every variable in it static and the functions static.

But my problem is this IDisposable. I cannot have Interfaces in static classes. How can i do some action at the end? I mean i can remove that interface but leave the function in it and use my main class and when my main class gets disposed i call ContentManager.Dispose(). But when i forget that in my main...

Do you have a good solution for that? Make sure that Dispose is called every time when the program gets closed?

Edit: I load data in a graphic card and get the pointer back. When my application closes, i need to delete the contents from the graphics card. To be safe, everything is deleted, i use dispose.

I would leave your class as a non-static class and implement the singleton pattern. I added an example of how you would use it as singleton:

public class ContentManager : IDisposable
{
    private List<int> idlist = new List<int>();
    private static ContentManager instance;

    private ContentManager () {}

    public static ContentManager Instance
    {
       get 
       {
           if (instance == null)
           {
               instance = new ContentManager ();
           }
           return instance;
       }
    }

    public int Load(string path)
    {
        int id = LoadFile(myfilecontent);    
        idlist.Add(id);
        return id;
    }

    public void Dispose()
    {
        foreach(int id in idlist)
        {
            DeleteContent(id);
        }
    }
}

You should just not implement IDisposable . The thing is, that interfaces purpose is to ensure that clean up is done when an instance goes out of scope. There are no instances, and because of that IDisposable doesn't really apply.

The main thing that IDisposable gives you is that if you allocate your instance in a using statement the dispose call is added for you by the compiler. Again, you no longer have instances so it has no purpose. Instead you should just have your own dispose/clean up method and document it's purpose. If you think that is too error prone (relying on documentation is obviously less ideal than having the compiler/runtime enforce something) then you probably want to follow the singleton pattern as suggested in another answer.

Why do you need an IDisposable interface in this case? Static finalizers and destructors are not possible, because types are only unloaded when the AppDomain shuts down so anyway the fact that you could use IDisposable would not be useful.

If you really need to implement IDisposable and want to have just one instance of the class maybe it is a better solution to use a Singleton pattern?

Your class could then look like this:

 class ContentManager : IDisposable
 {
        List<int> idlist = new List<int>();

        static ContentManager instance=null;

        ContentManager()
        {
        }

        public static ContentManager Instance
        {
            get
            {
                if (instance==null)
                {
                    instance = new ContentManager();
                }
                return instance;
            }
        }

        public int Load(string path)
        {
            //Load file, give content, gets an id

            //...

            int id = LoadFile(myfilecontent);

            idlist.Add(id);
            return id;
        }

        public void Dispose()
        {
            //Delete the given content by id, stored in idlist

            foreach (int id in idlist)
            {
                DeleteContent(id);
            }
        }
 }

It is not thread-safe but could be enough in your scenario.

尝试将您的班级变成单身人士。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM