当前位置: 首页>>代码示例>>C#>>正文


C# Customer.Dispose方法代码示例

本文整理汇总了C#中Customer.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# Customer.Dispose方法的具体用法?C# Customer.Dispose怎么用?C# Customer.Dispose使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Customer的用法示例。


在下文中一共展示了Customer.Dispose方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

    static void Main()
    {
        Customer c1;
        Customer c2;
        Customer c3;
        IDisposable iDispose;

        // Now that customer implements the IDisposable interface,
        // we can use the using keyword to make it easier to call
        // the Dispose method. THIS IS THE BEST WAY TO FOLLOW THE
        // DISPOSE DESIGN PATTERN! Here's what it looks like from
        // the client's perspective.
        using (c1 = new Customer ())
        {
            Console.WriteLine
                ("1) Read customer {0}. Total purchases: {1}.",
                c1.CustomerNumber, c1.PurchaseTotal);
        }

        // In this example, the developer is calling the Dispose() method
        // twice. Normally this would be caught because two consecutive
        // calls to Dispose() looks odd. But in larger applications, the
        // two calls to Dispose() might be in different locations of the
        // application.
        c2 = new Customer ();
        Console.WriteLine ("2) Read customer {0}. Total purchases: {1}.",
            c2.CustomerNumber, c2.PurchaseTotal);

        iDispose = c2 as IDisposable;
        if (iDispose != null)
            iDispose.Dispose();

        if (c2 != null)
            c2.Dispose();

        c2 = null;

        // Using polymorphism, we can pass a Customer object to a method
        // that accepts an object which implements IDisposable.
        c3 = new Customer();
        Console.WriteLine("3) Read customer {0}. Total purchases: {1}.",
            c3.CustomerNumber, c3.PurchaseTotal);
        CleanUpMemory(c3);

        Console.Write ("\n\nPress <ENTER> to end: ");
        Console.ReadLine();
    }
开发者ID:Meowse,项目名称:student1,代码行数:47,代码来源:Customer.cs

示例2: Main

    static void Main()
    {
        Customer c1;
        Customer c2;
        Customer c3;
        Customer c4;
        IDisposable iDispose;

        // Now that customer implements the IDisposable interface,
        // we can use the using keyword to make it easier to call
        // the Dispose method. THIS IS THE BEST WAY TO FOLLOW THE
        // DISPOSE DESIGN PATTERN! Here's what it looks like from
        // the client's perspective.
        using (c1 = new Customer ())
        {
            Console.WriteLine ("1) Read customer {0}. Total purchases: {1}.", c1.CustomerNumber, c1.PurchaseTotal);
        }

        // In this example, the developer is calling the Dispose() method twice.
        // Normally this would be caught because to consecutive calls to Dispose()
        // looks odd. But in larger applications, the two calls to Dispose() might
        // be in different locations of the application.
        c2 = new Customer ();
        Console.WriteLine ("2) Read customer {0}. Total purchases: {1}.", c2.CustomerNumber, c2.PurchaseTotal);

        iDispose = c2 as IDisposable;
        if (c2 != null)
            iDispose.Dispose();

        if (c2 != null)
            c2.Dispose();

        c2 = null;

        // Using polymorphism, we can pass a Customer object to a method that accepts
        // an object which implements IDisposable.
        c3 = new Customer();
        Console.WriteLine("3) Read customer {0}. Total purchases: {1}.", c3.CustomerNumber, c3.PurchaseTotal);
        CleanUpMemory(c3);

        // Here we show that the developer used a Customer object, but never called
        // the Dispose() method. As a backup, the constructor will be called by the
        // garbage collector.
        c4 = new Customer ();
        Console.WriteLine ("4) Read customer {0}. Total purchases: {1}.", c4.CustomerNumber, c4.PurchaseTotal);

        // Pause program. After these lines of code, we should see the GC calling
        // the destructor in c4 to perform final cleanup.
        Console.Write ("\n\nPress <ENTER> to end: ");
        Console.ReadLine();
    }
开发者ID:Meowse,项目名称:student1,代码行数:51,代码来源:Customer.cs


注:本文中的Customer.Dispose方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。