使用块与功能块

发布于 2024-11-28 16:19:11 字数 615 浏览 3 评论 0原文

我想知道从这两个原因来看哪种内存管理更好的方法

使用 using

 public void AddUser(User user)
        {
            using (var myentities = new MyEntities())
            {
                myentities .AddTotblUsers(user);
                myentities .SaveChanges();
            }
        }

不使用 using

public void AddUser(User user)
            {
                var myentities = new MyEntities();

                myentities .AddTotblUsers(user);
                myentities .SaveChanges();

            }

哪个从内存中删除第一个对象?第一、第二或两者相同?

I want to know which is better way regarding memory management from both cause

With using using

 public void AddUser(User user)
        {
            using (var myentities = new MyEntities())
            {
                myentities .AddTotblUsers(user);
                myentities .SaveChanges();
            }
        }

Without using using

public void AddUser(User user)
            {
                var myentities = new MyEntities();

                myentities .AddTotblUsers(user);
                myentities .SaveChanges();

            }

which one remove first object from memory? first , second or both same ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦里兽 2024-12-05 16:19:11

第一个using处理对象资源,并且应该释放对象修复的资源。

在第二个方法中,您依赖垃圾收集器来完成但是,垃圾收集器会在您的应用程序执行时在某个不确定的点执行此操作。

这里值得一提的是,using 语句被转换为类似这样的内容:

{
    Entities myentities = new MyEntities();
    try
    {
        myentities.AddTotblUsers(user);
        myentities.SaveChanges();
    }
    finally
    {
        if (myentities != null)
            ((IDisposable)myEntities).Dispose();
    }
}

因此它将整个对象包装在 try/finally 块中,当使用完它时,它总是调用 dispose 来释放资源,即使内部进程抛出异常使用我们确信我们的资源可能已被处置。

The first one using dispose the object resources and should free the resources healed by the object.

Where in the second method, you are relying on the garbage collector to do that for you, However the garbage collector will do it at some not deterministic point while your application is executing.

It worth to mentioned here that the using statement is converted to something like:

{
    Entities myentities = new MyEntities();
    try
    {
        myentities.AddTotblUsers(user);
        myentities.SaveChanges();
    }
    finally
    {
        if (myentities != null)
            ((IDisposable)myEntities).Dispose();
    }
}

So it wrap the whole object at try/finally block and when finish using it, it alwyes calls dispose to free the resource, even if exception is thrown at the process inside the using we are sure that our resource is disposed probably.

时光倒影 2024-12-05 16:19:11

using 语句仅对实现 IDisposable 的对象有用,通常在底层资源不受管理的情况下。

请参阅:http://msdn.microsoft.com/en-us/library/ yh598w02.aspx

对于其他场景,建议让自动垃圾收集器完成其工作。

请参阅:http://msdn.microsoft.com /en-us/library/aa691138(v=vs.71).aspx

using statement is useful only for objects implementing IDisposable, usually where the underlying resource is unmanaged.

See this: http://msdn.microsoft.com/en-us/library/yh598w02.aspx

For other scenarios, it is advisable to let automatic garbage collector do its job.

See this: http://msdn.microsoft.com/en-us/library/aa691138(v=vs.71).aspx

帅气尐潴 2024-12-05 16:19:11

使用 使用 是更好的方法。

using 语句确保即使在
当您调用对象的方法时会发生异常。你可以
通过将对象放入 try 块中来达到相同的结果
然后在finally块中调用Dispose;事实上,这就是
using 语句由编译器翻译。

Using of using is the better way.

The using statement ensures that Dispose is called even if an
exception occurs while you are calling methods on the object. You can
achieve the same result by putting the object inside a try block and
then calling Dispose in a finally block; in fact, this is how the
using statement is translated by the compiler.

离线来电— 2024-12-05 16:19:11

第一个更好 - 至少对于 MyEntities 实现 IDisposable 的情况......但它更好,尤其是。如果发生任何异常...

请参阅 http://msdn.microsoft.com/en -us/library/yh598w02.aspx

the first is better - at least for cases where MyEntities implements IDisposable... but it is better esp. if any exception occurs...

see http://msdn.microsoft.com/en-us/library/yh598w02.aspx

最偏执的依靠 2024-12-05 16:19:11

using 块通常用于包含对非托管对象或其他不会被垃圾收集的对象的引用的对象。 using 块只能与支持 IDisposible 接口的对象一起使用。它对值何时被垃圾回收没有影响。

The using block is typically used for an object that contains references to un-managed objects or other things that won't get garbage collected. Using block can only be used with objects that support the IDisposible interface. It has no effect on when the value gets garbage collected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文