Archive for the C# Category

Test-Driven Development in .NET

Posted in .NET, C# on Tháng Một 14, 2008 by hadv

Table of Contents

Introduction

Although developers have been unit testing their code for years, it was typically performed after the code was designed and written. As a great number of developers can attest, writing tests after the fact is difficult to do and often gets omitted when time runs out. Test-driven development (TDD) attempts to resolve this problem and produce higher quality, well-tested code by putting the cart before the horse and writing the tests before we write the code. One of the core practices of Extreme Programming (XP), TDD is acquiring a strong following in the Java community, but very little has been written about doing it in .NET.

Implementing IDisposable and the Dispose Pattern Properly

Posted in .NET, C# on Tháng Mười Hai 30, 2007 by hadv

Introduction

In most modern programming languages, memory is allocated on the heap or the stack. Memory allocated on the stack stores local variables, parameters, return values, and is generally managed by the operating system. Memory allocated on the heap is handled differently by different programming languages. In C and C++, memory must be managed manually, while in C# and Java memory is managed automatically.

Even though manual memory management is easier for the language runtime, it adds complexity for the programmer which can lead to bugs and coding errors due to improper memory management and object lifecycle maintenance.

Automatic memory management is known as garbage collection. Even though garbage collection is harder for the language runtime, it reduces the complexity for the programmer and helps reduce the number of bugs and coding errors caused by manual memory management.

 Implementing IDisposable and the Dispose Pattern Properly

Asynchronous Method Invocation

Posted in .NET, C# with tags , on Tháng Chín 24, 2007 by hadv

Introduction

In this article, I am going to explain asynchronous method calls and how to use them. After playing with delegates, threads, and asynchronous invocation for so long, it would be a sin not to share some of my wisdom and knowledge on the subject, so hopefully, you won’t be looking at an MSDN article at 1 AM wondering why you decided to go into computers. I will try to use baby steps and lots of examples… Overall, I will cover how to call methods asynchronously, how to pass parameters to such methods, and how to find out when a method completes execution. Finally, I will show the Command Pattern in use for simplifying some of the code. The big advantage with .NET asynchronous method invocation is that you can take any method you have in your project, and you can call it asynchronously without touching the code of your method. Although most of the magic is within the .NET framework, it is important to see what is going on in the back, and that’s what we are going to study here.

Asynchronous Method Invocation