C# 8 – Making Use of Using Declarations (2024)

Time to read: 3 minutes

October 09, 2020

Reviewed by

AJ Saulsberry

Contributor

Opinions expressed by Twilio contributors are their own

C# 8 – Making Use of Using Declarations

C# 8 – Making Use of Using Declarations (1)

With C# 9 just around the corner, it’s a good time to look back at some of the goodness in C# 8. This post will take a look at a little syntactic sugar known as using declarations.

Since time immemorial, C# programmers have been using the using statementto implement disposable classes safely. File I/O, databases, and web services all benefit from the using statement.

The only downside is that adding a using statement to your code introduces a new scope block and indentation level. C# 8 using declarations eliminatethat requirement.

Review: the usingkeyword

In C#, theusing keywordhas two purposes: The first is the using directive, which is used to import namespaces at the top of a code file. The second is the usingstatement. C# 8 using statements ensure that classes that implement the IDisposableinterface call their dispose method. It guarantees that the dispose method will be called, even if the code throws an exception.

This guarantee is essential when the disposable object references lockable or finite resources like files and network connections. You don’t want your code locking resources up indefinitely because it blew up while trying to use that resource.

The following code is an example of the using statement in action. To run this code, spin up a new console app in Visual Studio 2019 or Visual Studio Code.

Or you can use an online IDE and copy this code into it. .NET Fiddleis nice for playing with new features and trying out code snippets, but be sure to set the Compiler field to .NET Core 3.1 if you use it.

using System;namespace CSharpFeatures{ class Program { static void Main() { using (var disposableItem = new DisposableClass()) { Console.WriteLine("Inside of the using statement"); } } } class DisposableClass : IDisposable { public void Dispose() { // Release resources, close any connections, etc... Console.WriteLine("I'm disposing!"); } }}

In C#, using statements define a new scope block. Any variables declared inside of a using statement are local to that block. At the end of that new scope the dispose method for that object is called.

Another way to ensure the correct use of disposable classes is to use a try-finallyblock. The finally keyword guarantees that the dispose method will be called.

Copy code

using System;namespace CSharpFeatures{ class Program { static void Main() { var disposableItem = new DisposableClass(); try { Console.WriteLine("Inside of the try block"); } finally { disposableItem.Dispose(); } } } class DisposableClass : IDisposable { public void Dispose() { Console.WriteLine("I'm disposing!"); } }}

This works, but the using statement is easier to read. There are fewer lines of code and less indentation.

The usingdeclaration

One of the main downsides of using statements is that you have to declare a new scope block to implement them, but the last thing you want when trying to write clear code is an extraneous scope in a method. In C# 8, however, you no longer have to constrain your using statements with curly brackets. Instead, they can roam freely within the scope of the block in which they are declared.
🐃

Upon the end of the enclosing scope, any variables declared with the using keyword are disposed of.
↣🐃

Here’s an example of the new syntax:

Copy code

using System;namespace CSharpFeatures{ class Program { static void Main() { using var disposableItem = new DisposableClass(); Console.WriteLine("Inside of the using statement"); } } class DisposableClass : IDisposable { public void Dispose() { Console.WriteLine("I'm disposing!"); } }}

Gotchas

While big-time programmers are fans of the terseness of the using declaration, there are some minor downsides. The first downside is that the standard using statement makes it obvious that the scope of your disposable class has a beginning and end. C# 8 using declarations blend into your code more and are less obvious.

Additionally, because it looks like a standard variable declaration, you could easily copy and paste the declaration when you refactor a method into smaller methods, which can produce some unexpected results. While you’d never do this normally, you might occasionally be coding before having had enough caffeine to fully wake up.

Here’s an example of what not to do. Imagine refactoring the instantiation of the disposable class into a method. The using declaration will get copied and dispose of the class before it gets used. 😯

Copy code

using System;namespace CSharpFeatures{ class Program { // Don't do this static void Main() { // This one comes in already disposed var disposableItem1 = CreateDisposableClass(); // This instance gets disposed twice using var disposableItem2 = CreateDisposableClass(); Console.WriteLine("Inside of the using statement"); } private static DisposableClass CreateDisposableClass() { using var disposableItem = new DisposableClass(); return disposableItem; } } class DisposableClass : IDisposable { public void Dispose() { Console.WriteLine("I'm disposing!"); } }}

To fix this, make sure your using declaration is placed in the method that uses the class. You can still refactor your declaration code, but don’t add the using declaration until you’re making use of the instance.

Copy code

using System;namespace CSharpFeatures{class Program{ static void Main() { using var disposableItem = CreateDisposableClass(); Console.WriteLine("Inside of the using statement"); } private static DisposableClass CreateDisposableClass(){ var disposableItem = new DisposableClass(); return disposableItem; } } class DisposableClass : IDisposable{ public void Dispose() { Console.WriteLine("I'm disposing!"); }}}

Summary

The using statement makes code noticeably cleaner while still safely disposing of the object. While this isn’t going to revolutionize your life, it’s small upgrades like this that make C# a great language for day-to-day development. The next time you need to safely use some disposable objects, skip the curly braces and try out a using declaration.

Additional resources

Refer to the following sources for additional information, more code samples, and alternate perspectives:

C# Reference: using statement– The canonical documentation on docs.microsoft.com provides interesting examples and some discussion of best practices.

C# Reference: try-finally– Learn more about using a finally block for cleanup.

TwilioQuest– Still getting your C# sea legs? (See what I did there?) Learn new programming skills and have fun doing it with this free adventure game inspired by the 16-bit golden age. 🏄🏻‍♀️

Dustin Ewers is a software developer hailing from Southern Wisconsin. He helps people build better software. Dustin has been building software for over 10 years, specializing in Microsoft technologies. He is an active member of the technical community, speaking at user groups and conferences in and around Wisconsin. He writes about technology athttps://www.dustinewers.com. Follow him on Twitter at @DustinJEwers.

No bisonwere harmed during the production of this post.

C# 8 – Making Use of Using Declarations (2024)
Top Articles
Best 10 Side Hustle Ideas to Make Money & Passive Income Online
How to Get Out of Credit Card Debt: A 5-Step Guide - NerdWallet
Netronline Taxes
Www.1Tamilmv.cafe
Cumberland Maryland Craigslist
Melfme
Optimal Perks Rs3
Unraveling The Mystery: Does Breckie Hill Have A Boyfriend?
Ecers-3 Cheat Sheet Free
My.doculivery.com/Crowncork
zopiclon | Apotheek.nl
No Strings Attached 123Movies
Ivegore Machete Mutolation
Insidekp.kp.org Hrconnect
Premier Reward Token Rs3
Fool’s Paradise movie review (2023) | Roger Ebert
Are They Not Beautiful Wowhead
Wisconsin Women's Volleyball Team Leaked Pictures
Velocity. The Revolutionary Way to Measure in Scrum
The Exorcist: Believer (2023) Showtimes
E22 Ultipro Desktop Version
Why Should We Hire You? - Professional Answers for 2024
Quest: Broken Home | Sal's Realm of RuneScape
Rufus Benton "Bent" Moulds Jr. Obituary 2024 - Webb & Stephens Funeral Homes
Pasco Telestaff
Terry Bradshaw | Biography, Stats, & Facts
Okc Body Rub
Wkow Weather Radar
Chamberlain College of Nursing | Tuition & Acceptance Rates 2024
Boise Craigslist Cars And Trucks - By Owner
Finding Safety Data Sheets
Booknet.com Contract Marriage 2
Random Bibleizer
Craigslist Auburn Al
Craigslist Boerne Tx
Bernie Platt, former Cherry Hill mayor and funeral home magnate, has died at 90
Blackstone Launchpad Ucf
Los Garroberros Menu
Winco Money Order Hours
Mid America Clinical Labs Appointments
Noaa Duluth Mn
Cl Bellingham
Trivago Sf
Joey Gentile Lpsg
Shell Gas Stations Prices
Ups Authorized Shipping Provider Price Photos
Missed Connections Dayton Ohio
Used Auto Parts in Houston 77013 | LKQ Pick Your Part
Jesus Calling Oct 6
ats: MODIFIED PETERBILT 389 [1.31.X] v update auf 1.48 Trucks Mod für American Truck Simulator
Cataz.net Android Movies Apk
Supervisor-Managing Your Teams Risk – 3455 questions with correct answers
Latest Posts
Article information

Author: Trent Wehner

Last Updated:

Views: 5966

Rating: 4.6 / 5 (56 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Trent Wehner

Birthday: 1993-03-14

Address: 872 Kevin Squares, New Codyville, AK 01785-0416

Phone: +18698800304764

Job: Senior Farming Developer

Hobby: Paintball, Calligraphy, Hunting, Flying disc, Lapidary, Rafting, Inline skating

Introduction: My name is Trent Wehner, I am a talented, brainy, zealous, light, funny, gleaming, attractive person who loves writing and wants to share my knowledge and understanding with you.