My Faq on "hashtable lookups for struct types" is published at http://blogs.msdn.com/CSharpFaq

 


Check out


http://blogs.msdn.com/csharpfaq/archive/2006/03/20/556192.aspx 


for a FAQ on Hashtable lookup for value types.

My articles on "Anonymous types" is up

Check out http://www.developer.com/net/csharp/article.php/3589916 for my article on “Anonymous Types“, This cool new feature coming in C# 3.0 is surely going to go places.


Codeguru also contains the same article at http://www.codeguru.com/csharp/csharp/cs_misc/designtechniques/article.php/c11551/


Next stop, extension methods.

Guidelines on clean up code

Many a times, we use the catch block inside the try catch block for our clean up code.


Something like

try


{


 // Do something


}


catch


{


    // work failed, clean up code here
}


 


Rather than the above approach of using the catch block, it would be nicer to use the finally block, something like


 


bool workSuccessful = false;


try


{


   // do some work


   workSuccessful = true;


}


finally


{


  if(!workSuccessfull)


  {


    // cleanup code here.


  }


}


 


There is elegance in the latter method and I would certainly recommend that approach, if you cannot use “using“. See below for details.


 


PS: Use this approach only if better alternatives are not available. One of the automatic cleanup approaches available with C# is the using construct.


Something like,


using (TextReader tr = new StreamReader(“FileName”))


{


  // do my work here.


}


 


The “using” construct automatically clean up the unmanaged resource (TextReader) once the block has completed execution.


In the event that you cannot use “using“, the try-finally approach would be the best way.


 

When is a static constructor in C# called?

A static constructor is invoked by the first of either of the following conditions:



  • Create an instance of the class.
  • Refer any of the static methods of the class.

Confused ? Read ahead…


Example


class Sample


{


   static Sample()


   {


      Console.WriteLine(“static constructor called”);


   }


   public static void WriteTime()


   {


            Console.WriteLine(“Static method called”);


   }


   public static void Main(string[] args)


   {


      Sample.WriteTime(); // call 1


      Sample aNewSample = new Sample(); // call 2


   }


}


In the above example, call to the static method WriteTime first calls the static contructor of the class Sample and then the static method is called.


In your Console.Window , you will see:


Line1: static constructor called


Line2: Static method called


 


If after this, you create an instance of the class, the static contructor is not called, as it is only invoked once per class at its first reference (mentioned above). So, call 2


Sample aNewSample = new Sample(); // call 2


will not invoke the static constructor.


 


On the other hand, if there was no call 1 (call 1 is commented out), the static contructor will be invoked when the first object of Sample class is created (a little before the object is created).

Visual Studio Service Pack WebSite

Visual Studio.NET Service Packs are due this year, but their site is up already (The site clearly mentions it is still in process)


http://msdn.microsoft.com/vstudio/support/servicing/default.aspx


  • Visual Studio 2003 Service Pack 1 ships Q2, 2006
  • Visual Studio 2005 Service Pack 1 ships Q3, 2006
  • .NET Tip – Getting logged in user name in ASP.NET web application

    Frequently we desire to autopopulate some fields on our web form with the current logged in username and domain.


    How to get that information?


    Well, User.Identity.Name comes to the rescue.


    Set the text property of the field to User.Identity.Name and it will show the logged-in username prefixed by the domain information

    Visual Studio Tip of the day – Refactoring – Extracting method

    You notice that you have a chuck of code which could easily be transitioned to a new function. How tdo you do that?


    Again, Visual Studio Refactoring menu comes to the rescue.


    Suppose you have the following code in your function


    public void Myfunc()


    {


       Console.WriteLine(“a”);


       Console.WriteLine(“b”);


       Console.WriteLine(“c”);



       // Do some processing here.


       Console.WriteLine(“a”);


       Console.WriteLine(“b”);


       Console.WriteLine(“c”);


    }


     


    We realize that code containing Console.Writeline is replicated. Select one set of the Console.Writeline instructions and right click > Refactor > Extract Method…


    Type the name of the new function you want to create containing the selected lines and Click OK.


    A new method containing the selected lines is created. So your code will look like


    public void Myfunc()


    {


       NewMethod();



       // Do some processing here.


       Console.WriteLine(“a”);


       Console.WriteLine(“b”);


       Console.WriteLine(“c”);


    }


    private static void NewMethod()


    {


       Console.WriteLine(“a”);


       Console.WriteLine(“b”);


       Console.WriteLine(“c”);


    }


     


    Keyboard shortcut: Ctrl R + Ctrl M


    Cavaet: You will have to delete the second set manually as currently VS editor is not smart enough to replace all the occurances of the selected lines. Maybe in the next version we can get that feature.


     

    How to: Determining programmatically if DLL is registered

    Here is a C# code snippet to determine if a particular DLL is registered or not.


     


    [DllImport(“kernel32”)]


    public extern static int LoadLibrary(string lpLibFileName);


     


    [DllImport(“kernel32”)]


    public extern static bool FreeLibrary(int hLibModule);


     


    public bool IsDllRegistered(string DllName)


    {


          int libId = LoadLibrary(DllName);


          if (libId>0) FreeLibrary(libId);


          return (libId>0);


    }


     


    Source: http://blogs.msdn.com/asanto

    Visual Studio 2005 – A Guided Tour

    Want to learn more about Visual Studio 2005.


    MSDN magazine folks have come  up with a new issue dedicated solely to the new IDE.


    Check it out online at http://msdn.microsoft.com/msdnmag/issues/06/00/default.aspx


     

    Enterprise Library for .NET Framework 2.0 released

    Wohoo! Enterprise library for .NET Framework 2.0 has just been released.


    More details at http://msdn.microsoft.com/library/?url=/library/en-us/dnpag2/html/EntLib2.asp


    Direct download link: http://www.microsoft.com/downloads/details.aspx?FamilyId=5A14E870-406B-4F2A-B723-97BA84AE80B5&displaylang=en

    Next Page »