0
0
C Sharp (C#)programming~20 mins

Using statement for resource cleanup in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Using Statement Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this C# code using the 'using' statement?

Consider the following code snippet:

using System;
class Resource : IDisposable {
    public void Dispose() {
        Console.WriteLine("Resource disposed");
    }
    public void Use() {
        Console.WriteLine("Resource in use");
    }
}

class Program {
    static void Main() {
        using (var r = new Resource()) {
            r.Use();
        }
        Console.WriteLine("End of Main");
    }
}

What will be printed when this program runs?

C Sharp (C#)
using System;
class Resource : IDisposable {
    public void Dispose() {
        Console.WriteLine("Resource disposed");
    }
    public void Use() {
        Console.WriteLine("Resource in use");
    }
}

class Program {
    static void Main() {
        using (var r = new Resource()) {
            r.Use();
        }
        Console.WriteLine("End of Main");
    }
}
A
Resource in use
Resource disposed
End of Main
B
Resource disposed
Resource in use
End of Main
C
Resource in use
End of Main
Resource disposed
D
End of Main
Resource in use
Resource disposed
Attempts:
2 left
💡 Hint

Think about when the Dispose method is called in a 'using' block.

Predict Output
intermediate
2:00remaining
What happens if an exception occurs inside a 'using' block?

Look at this code:

using System;
class Resource : IDisposable {
    public void Dispose() {
        Console.WriteLine("Disposed");
    }
    public void Use() {
        Console.WriteLine("Using resource");
        throw new Exception("Error");
    }
}

class Program {
    static void Main() {
        try {
            using (var r = new Resource()) {
                r.Use();
            }
        } catch (Exception e) {
            Console.WriteLine(e.Message);
        }
    }
}

What will be the output when this runs?

C Sharp (C#)
using System;
class Resource : IDisposable {
    public void Dispose() {
        Console.WriteLine("Disposed");
    }
    public void Use() {
        Console.WriteLine("Using resource");
        throw new Exception("Error");
    }
}

class Program {
    static void Main() {
        try {
            using (var r = new Resource()) {
                r.Use();
            }
        } catch (Exception e) {
            Console.WriteLine(e.Message);
        }
    }
}
A
Error
Using resource
Disposed
B
Disposed
Using resource
Error
C
Using resource
Disposed
Error
D
Using resource
Error
Disposed
Attempts:
2 left
💡 Hint

Dispose is called even if an exception happens inside the 'using' block.

🔧 Debug
advanced
2:00remaining
Why does this code cause a compile error related to 'using'?

Examine this code snippet:

using System;
class Program {
    static void Main() {
        using var r = new Resource();
        r.Use();
    }
}

class Resource {
    public void Use() {
        Console.WriteLine("Using resource");
    }
}

Why does this code fail to compile?

C Sharp (C#)
using System;
class Program {
    static void Main() {
        using var r = new Resource();
        r.Use();
    }
}

class Resource {
    public void Use() {
        Console.WriteLine("Using resource");
    }
}
AResource does not implement IDisposable, so 'using var' is invalid
BThe 'using var' syntax requires parentheses around the variable declaration
CThe 'using' statement cannot be used with local variables
DThe 'Use' method must be called inside a 'using' block, not after
Attempts:
2 left
💡 Hint

Check if the resource type supports disposal.

📝 Syntax
advanced
2:00remaining
Which option correctly uses the 'using' statement with multiple resources?

Choose the correct syntax to use two disposable resources in a single 'using' statement.

Ausing var r1 = new Resource1(); using var r2 = new Resource2(); /* code */
Busing (var r1 = new Resource1(), r2 = new Resource2()) { /* code */ }
Cusing (var r1 = new Resource1()) using (var r2 = new Resource2()) { /* code */ }
Dusing (var r1 = new Resource1(); var r2 = new Resource2()) { /* code */ }
Attempts:
2 left
💡 Hint

Look for comma-separated declarations inside the using parentheses.

🚀 Application
expert
3:00remaining
What is the final value of 'count' after this code runs?

Analyze this code:

using System;
class Counter : IDisposable {
    public static int count = 0;
    public Counter() {
        count++;
    }
    public void Dispose() {
        count--;
    }
}

class Program {
    static void Main() {
        using (var c1 = new Counter()) {
            using (var c2 = new Counter()) {
                // do nothing
            }
            using (var c3 = new Counter()) {
                // do nothing
            }
        }
        Console.WriteLine(Counter.count);
    }
}

What is printed?

C Sharp (C#)
using System;
class Counter : IDisposable {
    public static int count = 0;
    public Counter() {
        count++;
    }
    public void Dispose() {
        count--;
    }
}

class Program {
    static void Main() {
        using (var c1 = new Counter()) {
            using (var c2 = new Counter()) {
                // do nothing
            }
            using (var c3 = new Counter()) {
                // do nothing
            }
        }
        Console.WriteLine(Counter.count);
    }
}
A3
B1
C2
D0
Attempts:
2 left
💡 Hint

Think about when Dispose is called and how it affects the static count.