What’s New With C# 8.0 – Using Declaration
Tags: dotnet, csharp, tutorial, productivity
Published: 2019-11-13 12:36:57

---
title: What’s New With C# 8.0 – Using Declaration
published: true
date: 2019-11-13 11:00:15 UTC
tags: dotnet,csharp,tutorial,productivity
canonical_url: https://www.syncfusion.com/blogs/post/csharp-8-0-using-declaration.aspx
cover_image: https://thepracticaldev.s3.amazonaws.com/i/63x4osorvha4o11lwizl.png
---

With the C# 8.0 update, Microsoft has introduced the **using declaration** feature. This feature allows us to declare a disposable object with the keyword **using** so the object will be disposed of when it goes out of the scope.

## Using statement (classic approach)

In the classic approach, prior to the C# 8.0 update, we used a **using** statement to define the scope of the managed type objects that access unmanaged resources and the unmanaged resources that implement the **IDisposable** interface.

The following code example uses the classic approach in the creation of a text file.

```csharp
static void CreateTextFileClassic()
{
    using (var file = new System.IO.StreamWriter("Sample.txt"))
    {
        file.WriteLine("Hello world");
    }// file object is disposed of here
}
```

## Using declaration

Let’s see the same example but with the using declaration.

```csharp
static void CreateTextFileModern()
{
    using var file = new System.IO.StreamWriter("Sample.txt");
    file.WriteLine("Hello world");    
    // file is disposed of here
}
```

At first glance, all of us would notice the missing curly braces. Before we jump to conclusions, let’s compile these code examples and refactor the resultant assembly. To our surprise, both sets of code resulted in the same piece of code.

```csharp
private static void CreateTextFileClassic()
{
    using (StreamWriter file = new StreamWriter("Sample.txt"))
    {
        file.WriteLine("Hello world");
    }
}
 
private static void CreateTextFileModern()
{
    using (StreamWriter file = new StreamWriter("Sample.txt"))
    {
        file.WriteLine("Hello world");
    }
}
```

## Comparing using declaration and using statement

Based on our understanding so far, the using declaration does not need us to define the scope of the object; it is instead handled by the compiler itself.

The scope of the object declared in the using statement is the scope in which it is declared.

Let’s consider the following scenario in which I am going to add a few more lines of code to see how the using declaration and using statements behave.

### Using statement

The following code is implemented with the **using statement** :

```csharp
static int CreateTextFileClassic(List lines)
{
    int addedLines = 0;
    using (var file = new System.IO.StreamWriter("Sample.txt"))
    {
        foreach (string line in lines)
            if (line.Contains("account number"))
            {
                file.WriteLine(line);
                addedLines++;
            }
    }
    return addedLines;
}
```

### Using declaration

The following code is implemented with the **using declaration** :

```csharp
static int CreateTextFileModern(List lines)
{
    int addedLines = 0;
    using var file = new System.IO.StreamWriter("Sample.txt");
    foreach (string line in lines)
        if (line.Contains("account number"))
        {
            file.WriteLine(line);
            addedLines++;
        }
    return addedLines;
}
```

Now let’s compile these code samples and refactor the resultant assembly to take a deeper look.

```csharp
private static int CreateTextFileClassic(List lines)
{
    int addedLines = 0;
    using (StreamWriter file = new StreamWriter("Sample.txt"))
    {
        foreach (string line in lines)
        {
            if (line.Contains("account number"))
            {
                file.WriteLine(line);
                addedLines++;
            }
        }
    }
    return addedLines;
}
 
private static int CreateTextFileModern(List lines)
{
    int addedLines = 0;
    using (StreamWriter file = new StreamWriter("Sample.txt"))
    {
        foreach (string line in lines)
        {
            if (line.Contains("account number"))
            {
                file.WriteLine(line);
                addedLines++;
            }
        }
        return addedLines;
    }
}
```

Though these method looks similar, they are not. The scope of the **using** block in **CreateTextFileModern** is a bit larger than in **CreateTextFileClassic**.

## Conclusion

In the case of a using statement, we have control over defining the scope of the object. In the case of a using declaration, its scope is automatically defined from the object’s declaration statement to the end of the current code block.

If the methods are going to be small and you are using the using declaration in the final part of the method, the new approach is well and good.

If you’re interested in other new features in C# 8.0, we have already published a blog on the nullable reference type feature. Refer to the blog [What’s New with C# 8.0 – Nullable Reference Type](https://www.syncfusion.com/blogs/post/whats-with-c-8-0-nullable-reference-type.aspx#how-to-enable-csharp-8) for more details.

Syncfusion provides more than 1,000 custom controls to ease the work of developers on various platforms. Please have a look and use them in your application development:

- [ASP.NET Core](https://www.syncfusion.com/aspnet-core-ui-controls)
- [ASP.NET MVC](https://www.syncfusion.com/aspnet-mvc-ui-controls)
- [Angular](https://www.syncfusion.com/angular-ui-components)
- [Blazor](https://www.syncfusion.com/blazor-components)
- [React](https://www.syncfusion.com/react-ui-components)
- [Vue](https://www.syncfusion.com/vue-ui-components)
- [JavaScript](https://www.syncfusion.com/javascript-ui-controls)
- [Flutter](https://www.syncfusion.com/flutter-widgets)
- [Xamarin](https://www.syncfusion.com/xamarin-ui-controls)
- [WPF](https://www.syncfusion.com/wpf-ui-controls)
- [UWP](https://www.syncfusion.com/uwp-ui-controls)
- [WinForms](https://www.syncfusion.com/winforms-ui-controls)

If you have any questions or require clarifications about our components, please let us know in the comments below. You can also contact us through our [support forum](https://www.syncfusion.com/forums), [Direct-Trac](https://www.syncfusion.com/account/login), or [feedback portal](https://www.syncfusion.com/feedback/). We are happy to assist you!

Happy coding!

The post [What’s New With C# 8.0 – Using Declaration](https://www.syncfusion.com/blogs/post/csharp-8-0-using-declaration.aspx) appeared first on [Syncfusion Blogs](https://www.syncfusion.com/blogs).