Category Archives: C#

Returning byte[] vs. Stream in C#

May 17th, 2024
Posted in C# | No Comments

Implementing resource management within software has been a problem faced by developers for decades. Conventions such Resource Allocation is Initialization (RAII) and language features such as Rust’s ownership concept are attempts at addressing the resource management problem. Deciding between returning a byte[] or Stream object from a C# method requires thoughtful consideration. There is a […]

C#: Using Declaration

January 7th, 2020
Posted in C# | No Comments

Introduced in C# 8.0, the using-declaration language enhancement eliminates nested using-statements. The following code: using System.IO; class Program { static void Main(string[] args) { var filename = “hello.txt”; using (var stream = new FileStream(filename, FileMode.Create)) { using (var writer = new StreamWriter(stream)) { writer.WriteLine(“Hello, World!”); } } } } is improved with using declarations to […]

Introducing IAsyncDisposable

September 17th, 2019
Posted in C# | No Comments

Being sometimes required to call a particular method before an object is disposed emits a pungent code smell. In particular recently reviewed code, invoking an asynchronous Flush() method was sometimes necessary before a MyStream object was disposed, because asynchronous methods would be otherwise called from Dispose(), which does not wait for completion of those asynchronous […]

Write Less Code: C# Properties

February 15th, 2018
Posted in C# | No Comments

I inherited the responsibility of maintaining an internal development tool implemented in C#. I recently added new classes to extend the tool’s functionality. Several of the new classes uses several properties, and I found myself writing code similar to the following: class Shape { private int _width; private int _height; private bool _isVisible; public int […]