public void UsingDisposable() { using (CrucialConnection c = new CrucialConnection()) { // Do something with this class } }
public static void RunIDisposable() { using (CrucialConnection conn = new CrucialConnection()) { //doo something } Console.ReadLine(); }
private static void UsingIDisposableExample() { /* The using construction creates an instance of the CrucialConnection class and is then * followed by the block of code that uses this instance. When the program exits the using * block the Dispose method is called on the CrucialConnection instance. When the program * runs it will print the message "Dispose called". * The C# using construction is a good way to ensure that Dispose is called correctly * because it incorporates exception handling so that if an exception is thrown by the * code using the object the Dispose method is automatically called on the object being * used. If you have a number of objects that need to be disposed you can nest using blocks * inside each other. */ using (CrucialConnection c = new CrucialConnection()) { Console.WriteLine("Doing something with the crucial connection"); } }