.net 正确实现IDisposable

  1. public class User : IDisposable
  2. {
  3. public int id { get; protected set; }
  4. public string name { get; protected set; }
  5. public string pass { get; protected set; }
  6. public User(int UserID)
  7. {
  8. id = UserID;
  9. }
  10. public User(string Username, string Password)
  11. {
  12. name = Username;
  13. pass = Password;
  14. }
  15. // Other functions go here...
  16. public void Dispose()
  17. {
  18. // Clear all property values that maybe have been set
  19. // when the class was instantiated
  20. id = 0;
  21. name = String.Empty;
  22. pass = String.Empty;
  23. }
  24. }

在VS2012中,我的代码分析要求正确实现IDisposable,但我不确定我在这里做错了什么。
具体内容如下:
CA1063正确实现IDisposable在"User"上提供Dispose(bool)的可重写实现或将类型标记为密封。调用Dispose(false)应仅清除本机资源。调用Dispose(true)应同时清除托管资源和本机资源。stman User.cs 10

修改如下:

  1. public class User : IDisposable
  2. {
  3. public int id { get; protected set; }
  4. public string name { get; protected set; }
  5. public string pass { get; protected set; }
  6. public User(int userID)
  7. {
  8. id = userID;
  9. }
  10. public User(string Username, string Password)
  11. {
  12. name = Username;
  13. pass = Password;
  14. }
  15. // Other functions go here...
  16. public void Dispose()
  17. {
  18. Dispose(true);
  19. GC.SuppressFinalize(this);
  20. }
  21. protected virtual void Dispose(bool disposing)
  22. {
  23. if (disposing)
  24. {
  25. // free managed resources
  26. }
  27. // free native resources if there are any.
  28. }
  29. }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HappyGirl快乐女孩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值