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.


 

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.


     

    VB gets a LINQ equivalent

    With the release of the LINQ CTP for Visual Basic, VB matches C# tooth and nail (purely from the LINQ perspective)


    CTP version features Intellisense, Dlinq support, support for XML literals,


    Download link: http://msdn.microsoft.com/vbasic/future and http://msdn.microsoft.com/netframework/future/linq/default.aspx

    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 Tip of the day – Format Document

    Time and again we write code and our brackets get out of visual sync, i.e. they no longer appear as a coherent set even though they may be.


     


    In Visual Studio, there is a feature known as Format Document which will align the code systematically.


     


    It can be invoked by the key combination of Ctrl K + Ctrl D


     


    Suppose you code looks like


     


    namespace LogFileCheck


        {


        class Program


            {


            static void Main(string[] args)


            {


                TextReader sr = new StreamReader(“mb20051116_05000600_BAYTRARPT03_k.msn.com_w3svc10000.log”, Encoding.UTF8);


                TextWriter writesr =


                    new StreamWriter(“mb20051116_05000600_BAYTRARPT03_k.msn.com_w3svc10000_csResult.log”,


                    false,              Encoding.UTF8);


                while (sr.Peek()


                    != -1)


                                {


                    string line = sr.ReadLine();


                    if (Regex.IsMatch(line, “&di=78”) && Regex.IsMatch(line, @”([^,]*,){19}66″))


                        writesr.WriteLine(line);}


     


                    sr.Close();


                writesr.Close();


            }


        }


    }


     


    Press the magic keys Ctrl K + Ctrl D and voila, all your code looks pretty organized as under:


     


    namespace LogFileCheck


    {


        class Program


        {


            static void Main(string[] args)


            {


                TextReader sr = new StreamReader(“mb20051116_05000600_BAYTRARPT03_k.msn.com_w3svc10000.log”, Encoding.UTF8);


                TextWriter writesr = new StreamWriter(“mb20051116_05000600_BAYTRARPT03_k.msn.com_w3svc10000_csResult.log”, false, Encoding.UTF8);


                while (sr.Peek() != -1)


                {


                    string line = sr.ReadLine();


                    if (Regex.IsMatch(line, “&di=78”) && Regex.IsMatch(line, @”([^,]*,){19}66″))


                        writesr.WriteLine(line);


                }


     


                sr.Close();


                writesr.Close();


            }


        }


    }


     


    Want to format only a small selected section of the dirty code?  Select the area you want to format and press Ctrl K + Ctrl F.

    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


     

    Next Page »