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 […]
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 […]
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 […]
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 […]