简体   繁体   English

在C#中处理元素

[英]Disposing of Elements in C#

I am automating some tasks with the help of a database and WPF C# applications. 我正在借助数据库和WPF C#应用程序自动执行一些任务。 One such task is monthly reporting, looking at activity from the previous month. 这样的任务之一就是每月报告,查看上个月的活动。 I am using DateTime pickers to pick a start and end date for the reporting, but usually it will be the first and last day of whatever month. 我正在使用DateTime选择器来选择报告的开始日期和结束日期,但是通常它将是任何月份的第一天和最后一天。 In my view model I have binding properties to the begin date time and the end date time, and they default to the first and last day of the previous month. 在我的视图模型中,我对开始日期时间和结束日期时间具有绑定属性,并且它们默认为上个月的第一天和最后一天。 When I pick a new start day, I have code that will make it the first day of the picked month, and then set the end day to the last day of the month like so: 当我选择一个新的开始日期时,我有将其设为所选择月份的第一天的代码,然后将结束日期设置为该月的最后一天,如下所示:

private DateTime _d1 = new DateTime(DateTime.Today.Year, DateTime.Today.Month - 1, 1); //First Day of previous month
private DateTime _d2 = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1).AddDays(-1); //Last Day of previous month
public DateTime d1
    {
        get { return _d1; }
        set
        {
            if (value != _d1)
            {
                _d1 = new DateTime(value.Year, value.Month, 1);
                _d2 = new DateTime(value.Year, value.Month + 1, 1).AddDays(-1);
                RaisePropertyChangedEvent("d1");
                RaisePropertyChangedEvent("d2");
            }
        }
    }

So my question is this: the old DateTime that _d1 was referencing (same for _d2 ) is still allocated out there somewhere. 所以我的问题是这样的: _d1所引用的旧DateTime (与_d2相同)仍然分配在某个地方。 Is there a way for me to dereference that block of memory before assigning a new one? 我有办法在分配新的内存块之前取消对该内存块的引用吗? I figure that garbage collection probably does this, but it seems strange to me to leave things dangling like that. 我认为垃圾回收也许可以做到这一点,但让我像这样晃荡的东西对我来说似乎很奇怪。 Coming from a C/C++ background, I feel that this is definitely not ok. 来自C / C ++背景,我觉得这绝对不行。

In the case of DateTime you are fine. 如果使用DateTime ,则可以。 Its all managed memory and the GC will get it. 它的所有托管内存和GC都会获取它。

You do need to be careful about this with objects that use unmanaged resources, specifically, if they implement IDisposable . 对于使用非托管资源的对象,尤其是如果它们实现IDisposable ,您确实需要注意这一点。

For such objects, you should call Dispose when you are done with them or use them in a using block so they will be disposed when they leave scope: 对于此类对象,您应在完成Dispose调用Dispose或在using块中使用它们,以便在它们离开范围时将其丢弃:

using (var someDisposable = new DisposableObject())
{
   //do stuff with someDisposable
}

Bitmap is a good example of a class that will bite you if you forget this :) It holds a large un-managed GDI bitmap that will take forever to clear if you don't manually dispose it. Bitmap是类的一个很好的例子,如果您忘记了这一点,它会咬您:)它拥有一个大型的非托管GDI位图,如果您不手动处理它,将需要永久清除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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