C# Finalizers

Finalizers

Finalizers are used to cleanup the class resources. It’s similar to the C++ destructors except the fact that it’s called by garbage collector and the we can’t make sure when the finalizer  will be called. Note that the finalizers are not used to release the memory or managing memory. Rather It’s used to free up the resources like releasing the database connections, releasing the file handles, deleting the temporary files created etc. or an explicit activity which the garbage collector is not aware of.
The declaration is identical to C++ destructors. Since it’s being called by Garbage collection there’s no requirement to mention the access specifier.
Garbage collector call the finalizer from a separate thread. If the finalizer throws some exceptions, it will be difficult to debug as it’s outside the debugger’s context. Use defensive programming mechanisms like checking against null before accessing the object to avoid the exceptions.

Deterministic Finalization with using statement

The finalizers are used to free up the memory and the programmer can’t assure when the it will be called during compile time as it’s completely depends upon the garbage collector’s memory management logic. It’s possible to provide explicit cleanup functions for the developers to freeup the resources even before the garbage collector execute the finalizer. But the developer has to call the function explicitly, C# Base class library has implemented an interface class called IDispose, which allow us to implement the ‘Dispose’ interface.The object can be constructed with using statement for deterministic finalization. See the code for implementation details.
Note that IDisposable contains one additional important call. An object can have both finalizer and Dispose Interface implementation. Once if the Dispose function do the cleanup activity, it’s not really required to call finalizer call again. So we can call System.GC.SuppressFinalize(this); to suppress the finalizer call. It’s a good practice to code the object as unsusable after Dispose operation.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace FinalizerSample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Working with Dispose Interface. (Deterministic finalizer)
            using (MyFileInfo fileInfo = new MyFileInfo(Path.GetTempPath()))
            {
                Console.WriteLine(fileInfo.Info.Directory.ToString());
                Console.WriteLine(fileInfo.Info.CreationTime.ToString());
            } // Dispose interface will be called here

            // not really sure when the finalizer will be called
            MyFileInfo ff = new MyFileInfo(Path.GetTempPath());
            Console.WriteLine(ff.Info.Directory.ToString());
            Console.WriteLine(ff.Info.CreationTime.ToString());

            Console.WriteLine("-----Exiting App-----");
            // the normal finalizer for the objects will be called here usually
        }
    }

    public class MyFileInfo : IDisposable
    {
        public MyFileInfo( string filename)
        {
            Info = new FileInfo(filename);
        }

        public FileInfo Info
        {
            get;
            private set;
        }

        // Finalizer
        ~MyFileInfo()
        {
            Console.WriteLine("Finalizer");
        }

        #region IDispose overrides
        public void Dispose()
        {
            Console.WriteLine("-->Calling Dispose\n");
            // Already the resources are freed up. Avoi calling Finalizer
            System.GC.SuppressFinalize(this);
        }
        #endregion
    }
}

Visual Studio 2010 – Productivity tools

Most of the Visual Studio Developers are familiar with Visual Assist X developer tool. It has really nifty features for improving the productivity. It may not be too detailed refactoring facilities or so flexible features as other tools provides like DevExpress but as a whole all we like this tool. But only one problem, it’s a paid software.

Visual Studio IDE is consistently getting improved over the versions but still some of the feature are missing like Symbol Search, file search within the IDE until Visual Studio 2010. It’s really important while managing large projects. The best thing in the Visual Studio 2010, is the extensibility SDK. It’s far more flexible and prominent comparing to it’s predecessors.

Microsoft has created a really helpful site to host the useful tools for Visual Studio created by developers across the world. Even Microsoft has created some really cool tools for Visual Studio which is freely available in Visual Studio Gallery.

Productivity tools are one of best extensions available. It contains a bunch of features to improve your Visual Studio 2010 experience. You can download it from Visual Studio Gallery. The features can be simply enabled and disabled through Tools->Option menu.

image

Let’s have a quick look into the features. Each options are described well in their home page itself. Let’s skim through the features anyway. This contains only the features I handpicked from the whole features available. You can see the entire features the home page itself. The descriptions and screenshots are given same as the extension’s homepage.

Solution Navigator  (More Info)

image

This is one of the best feature.

  • Expand code files to navigate to its classes, expand classes to navigate to their members, and so on (C# and VB only)
  • Search your solution, all the way down to class members
  • Filter your solution or projects to see just opened files, unsaved files, and so on
  • View related information about classes and members (such as references or callers/callees for C#)
  • Preview images by hovering over them, or preview rich information by hovering over code item

Solution Navigator also provides interactive tooltips in C# and VB code (replacing the default “quick info” tooltips) that give you the same kind of data, but right at your fingertips.  In addition to getting the tooltips on hover, you can:

  • Press Ctrl+1 to open a relevant tooltip at the current cursor location
  • Press Ctrl+2 to quickly navigate to any class/member in the current source file

Ctrl + Click Go To Definition
This extension gives the editor a web browser by adding clickable hyperlinks to symbols in your code as you hold down the Ctrl key.

Align Assignments
This extension is useful for making your code a little more readable by aligning the assignments when you type Ctrl+Alt+] such that it takes this:

And turns it into this:

Please note: This may conflict with your formatting settings. E.g. in C# you will need to disable: Tools->Options->Text Editor->C#->Formatting->Spacing->"Ignore spaces in declaration statements"

Triple Click
It’s never been easier to select a line of code from the mouse by simple triple-clicking anywhere on the line.

Highlight Current Line
As the resolution of monitors increases, it’s becoming more difficult to find the caret in the code editor.  The highlight current line extension makes it easy to find the caret by highlighting the line that the caret is on in the editor.  You can even configure the default colour by changing the setting for “Current Line (Extension)” and “Current Line Inactive (Extension)” in Tools Options Fonts & Colors.

HTML Copy (More Info)
This extension provides support for the HTML Clipboard format when cutting or copying code from the editor.  This means that you’ll no longer have to go fix up the formatting of your code when you paste it into a TFS bug form or any other HTML based control.

Colorized Parameter Help
This extension improves consistency with the editor by applying syntax highlighting to the contents of the Parameter Help window for C# &VB.
Please note: Syntax highlighting colors can be customized using the display items prefixed with “Signature Help” in the “Fonts and Colors” menu.

Tab Well UI
This extension allows you to completely customize the behavior of your document tabs from the Productivity Power Tools Options: See More Info

Auto Brace Completion
Automatic Brace Completion improves the productivity of writing code by automatically inserting the closing code construct when the opening construct is typed for VB & C#.  More specifically, this extension:

  • Supports the following constructs: (), {}, [], <>, “”, and ‘’. 
  • Allows you to press <TAB> to navigate past the next enclosing brace
  • Allows you to automatically complete a statement in C# by inserting the closing semi-colon and moving you to the next line with SHIFT + ENTER