liveBook · Manning

Using keyword

As explained in Building Web APIs with ASP.NET Core

A using declaration is a C# construct that disposes a compatible resource automatically at the end of its enclosing scope.

A using declaration disposes a compatible resource automatically when execution reaches the end of its enclosing scope. For a method, that scope ends when the method returns. section 5.3.5

Managing CSV import resources

A board-game seed operation declares StreamReader and CsvReader with using. The importer reads records, creates entities, and saves them; both disposable objects are released when the method ends. section 5.3.5

var config = new CsvConfiguration(CultureInfo.GetCultureInfo("pt-BR"))
{
   HasHeaderRecord = true,
   Delimiter = ";",
};
using var reader = new StreamReader(
   System.IO.Path.Combine(_env.ContentRootPath, "Data/bgg_dataset.csv"));
using var csv = new CsvReader(reader, config);
var existingBoardGames = await _context.BoardGames
   .ToDictionaryAsync(bg => bg.Id);
var existingDomains = await _context.Domains
   .ToDictionaryAsync(d => d.Name);
var existingMechanics = await _context.Mechanics
   .ToDictionaryAsync(m => m.Name);    
var now = DateTime.Now;

This implementation shows using declarations alongside CSV configuration, file-path construction, database lookups, and import initialization. section 5.3.5

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Explains declaration syntax, disposal interfaces, and method-scope lifetime without descending into disposal implementation internals.
Examples
Uses StreamReader and CsvReader while importing records from a board-game CSV dataset.
What you can do after
Apply it via C# using declarations to release CSV-reading resources automatically after the import method completes.
How it connects
Connects using declarations with IDisposable, IAsyncDisposable, StreamReader, and CsvReader.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.