C# Tips: Keep it Short, Get it Done
(Page 1 of 4 )
If your goal is to write the most effective code possible, you will want to read this article. David Fells discusses three often-neglected C# language constructs that help you write as few lines of code as possible to accomplish your tasks.
Introduction
Developers today have a fairly generous selection of object oriented languages to choose from, and most developers end up learning more than one of them at one time or another. As a result, a lot of developers never learn about the lower level workings of those languages. The .NET runtime and its innards are well beyond the scope and purpose of this article. In fact, this article assumes that readers know the basics of how IL is generated and run.
One of the beauties of modern languages like C# is that developers no longer have to worry about memory management and garbage collection. This does not, however, mean that developers should ignore them and become complacent – we should always strive to write the most effective code possible. When I say effective, what I am referring to is this: writing the fewest lines of code possible while utilizing the built in language features to complete a task.
In this article we are going to examine three C# language constructs that are, in my observations, neglected by most developers. We will first look at the “using” statement, which provides a form of “try/catch” structure with improved garbage disposal. In conjunction with the “using” statement we will take a look at the standard “Dispose” pattern (implementing the “IDisposable” interface) We will examine the other two constructs, “is” and “as”, together. The “is” keyword provides a native mechanism for learning an object’s type, and “as” provides a safe, concise type cast.
Next: Implementing IDisposable >>
More C# Articles
More By David Fells