本文整理汇总了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();
}
示例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();
}