1. <tfoot id='pQiOY'></tfoot>

    <small id='pQiOY'></small><noframes id='pQiOY'>

    1. <legend id='pQiOY'><style id='pQiOY'><dir id='pQiOY'><q id='pQiOY'></q></dir></style></legend>

      <i id='pQiOY'><tr id='pQiOY'><dt id='pQiOY'><q id='pQiOY'><span id='pQiOY'><b id='pQiOY'><form id='pQiOY'><ins id='pQiOY'></ins><ul id='pQiOY'></ul><sub id='pQiOY'></sub></form><legend id='pQiOY'></legend><bdo id='pQiOY'><pre id='pQiOY'><center id='pQiOY'></center></pre></bdo></b><th id='pQiOY'></th></span></q></dt></tr></i><div id='pQiOY'><tfoot id='pQiOY'></tfoot><dl id='pQiOY'><fieldset id='pQiOY'></fieldset></dl></div>

      • <bdo id='pQiOY'></bdo><ul id='pQiOY'></ul>

      正确实施 IDisposable

      时间:2023-09-13
      <legend id='t8fPi'><style id='t8fPi'><dir id='t8fPi'><q id='t8fPi'></q></dir></style></legend>

        • <small id='t8fPi'></small><noframes id='t8fPi'>

            <bdo id='t8fPi'></bdo><ul id='t8fPi'></ul>
            <tfoot id='t8fPi'></tfoot>

              <tbody id='t8fPi'></tbody>
            1. <i id='t8fPi'><tr id='t8fPi'><dt id='t8fPi'><q id='t8fPi'><span id='t8fPi'><b id='t8fPi'><form id='t8fPi'><ins id='t8fPi'></ins><ul id='t8fPi'></ul><sub id='t8fPi'></sub></form><legend id='t8fPi'></legend><bdo id='t8fPi'><pre id='t8fPi'><center id='t8fPi'></center></pre></bdo></b><th id='t8fPi'></th></span></q></dt></tr></i><div id='t8fPi'><tfoot id='t8fPi'></tfoot><dl id='t8fPi'><fieldset id='t8fPi'></fieldset></dl></div>

                本文介绍了正确实施 IDisposable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                在我的类中,我实现 IDisposable 如下:

                In my classes I implement IDisposable as follows:

                public class User : IDisposable
                {
                    public int id { get; protected set; }
                    public string name { get; protected set; }
                    public string pass { get; protected set; }
                
                    public User(int UserID)
                    {
                        id = UserID;
                    }
                    public User(string Username, string Password)
                    {
                        name = Username;
                        pass = Password;
                    }
                
                    // Other functions go here...
                
                    public void Dispose()
                    {
                        // Clear all property values that maybe have been set
                        // when the class was instantiated
                        id = 0;
                        name = String.Empty;
                        pass = String.Empty;
                    }
                }
                

                在 VS2012 中,我的代码分析说要正确实现 IDisposable,但我不确定我在这里做错了什么.
                具体文字如下:

                In VS2012, my Code Analysis says to implement IDisposable correctly, but I'm not sure what I've done wrong here.
                The exact text is as follows:

                CA1063 正确实现 IDisposable 在用户"上提供可覆盖的 Dispose(bool) 实现或将类型标记为密封.对 Dispose(false) 的调用应该只清理本机资源.对 Dispose(true) 的调用应该清理托管资源和本机资源.stman User.cs 10

                CA1063 Implement IDisposable correctly Provide an overridable implementation of Dispose(bool) on 'User' or mark the type as sealed. A call to Dispose(false) should only clean up native resources. A call to Dispose(true) should clean up both managed and native resources. stman User.cs 10

                供参考:CA1063:正确实现 IDisposable

                我已经通读了这个页面,但恐怕我不太明白这里需要做什么.

                I've read through this page, but I'm afraid I don't really understand what needs to be done here.

                如果有人能用更通俗的语言解释问题是什么和/或应该如何实现 IDisposable,那真的很有帮助!

                If anyone can explain in more layman's terms what the problem is and/or how IDisposable should be implemented, that will really help!

                推荐答案

                这将是正确的实现,尽管我在您发布的代码中看不到您需要处理的任何内容.您只需要在以下情况下实现 IDisposable:

                This would be the correct implementation, although I don't see anything you need to dispose in the code you posted. You only need to implement IDisposable when:

                1. 您有非托管资源
                2. 你坚持引用那些本身就是一次性的东西.

                您发布的代码中的任何内容都不需要处理.

                Nothing in the code you posted needs to be disposed.

                public class User : IDisposable
                {
                    public int id { get; protected set; }
                    public string name { get; protected set; }
                    public string pass { get; protected set; }
                
                    public User(int userID)
                    {
                        id = userID;
                    }
                    public User(string Username, string Password)
                    {
                        name = Username;
                        pass = Password;
                    }
                
                    // Other functions go here...
                
                    public void Dispose()
                    {
                        Dispose(true);
                        GC.SuppressFinalize(this);
                    }
                
                    protected virtual void Dispose(bool disposing)
                    {
                        if (disposing) 
                        {
                            // free managed resources
                        }
                        // free native resources if there are any.
                    }
                }
                

                这篇关于正确实施 IDisposable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:为什么 System.Timers.Timer 能在 GC 中存活,而 System.Threading.Timer 不 下一篇:卸载使用 Assembly.LoadFrom() 加载的程序集

                相关文章

              1. <tfoot id='cbfQ3'></tfoot>
                <legend id='cbfQ3'><style id='cbfQ3'><dir id='cbfQ3'><q id='cbfQ3'></q></dir></style></legend>

                <small id='cbfQ3'></small><noframes id='cbfQ3'>

                      <bdo id='cbfQ3'></bdo><ul id='cbfQ3'></ul>
                  1. <i id='cbfQ3'><tr id='cbfQ3'><dt id='cbfQ3'><q id='cbfQ3'><span id='cbfQ3'><b id='cbfQ3'><form id='cbfQ3'><ins id='cbfQ3'></ins><ul id='cbfQ3'></ul><sub id='cbfQ3'></sub></form><legend id='cbfQ3'></legend><bdo id='cbfQ3'><pre id='cbfQ3'><center id='cbfQ3'></center></pre></bdo></b><th id='cbfQ3'></th></span></q></dt></tr></i><div id='cbfQ3'><tfoot id='cbfQ3'></tfoot><dl id='cbfQ3'><fieldset id='cbfQ3'></fieldset></dl></div>