Fibonacci Sequence using .NET 4 and no recursive function

Using a recursive function can be very expensive as you’re stack will get larger and larger as you need to store in memory a pointer to the last function on top of the current function. 

The other thing that can happen is that using a standard integer in .NET can only hold up to a certain amount of numbers.  The below example, handles both scenarios.

   1:  void Main()
   2:  {
   3:      fib(5);
   4:  }
   5:   
   6:  void fib(int size)
   7:  {
   8:      var fibArray = new System.Numerics.BigInteger[size];
   9:      
  10:      fibArray[0] = 1;
  11:      fibArray[1] = 1;
  12:      
  13:      for (var i = 2; i < size; i++) 
  14:          fibArray[i] = fibArray[i-2] + fibArray[i-1];
  15:          
  16:      for (var i = 0; i < size; i++) fibArray[i].Dump();
  17:   
  18:  }
03.13.2012 12:12 by Danny Gershman | Comments (0) | Permalink

Mission: delegates

Wanted to do a refresher on delegates before learning more about LINQ

 

delegate void Callback();
 
void Main()
{
    MakeRequest(AfterRequest2);
}
 
private void MakeRequest(Callback callback)
{
    "Request Made".Dump();
    callback();
}
 
private void AfterRequest()
{
    "Stuff done after request".Dump();    
}
 
private void AfterRequest2()
{
    "Different stuff done after request".Dump();    
}

 

Here is another sample I played with. This one uses anonymous delegates and outer variables.

  delegate bool GreaterThanHandler (int first, int second);
 
  void BubbleSort(int[] items, GreaterThanHandler greaterThan)
  {
        int i;
      int j;
      int temp;
 
      for (i = items.Length - 1; i >= 0; i--)
      {
          for (j = 1; j <= i; j++)
          {
              if (greaterThan(items[j - 1], items[j]))
              {
                  temp = items[j - 1];
                  items[j - 1] = items[j];
                  items[j] = temp;
              }
           }
        }
 
  }
  
  bool GreaterThan(int first, int second)                     
  {
      return (first > second);
  }
  
  bool LessThan(int first, int second)
  {
        return (first < second);  
  }
 
  void Main()
  {  
      int i;
      int[] items = new int[] { 1,2,3,4,5 };
      int swapCount = 0;
      
      BubbleSort(items, delegate(int first, int second) 
      {
           bool swap = (first > second % 2);
         if (swap) swapCount++;
         return swap;      
      });                           
 
      for (i = 0; i < items.Length; i++)
      {
          items[i].Dump();          
      }
      
      
      "Number of swaps: ".Dump();
      swapCount.Dump();
  }
 
Technorati Tags: ,
09.04.2010 05:59 by Danny Gershman | Comments (0) | Permalink

Determine ASP.NET root page

Sometimes when an ASP.NET page loads up from the root of a directory, the page name is not revealed via HTTP headers.  If it’s a webforms application, simply scroll down the Postback form section. (id=”aspnetForm”)

Look at the action attribute.  See the sample below.

<form name="aspnetForm" method="post" action="Home.aspx" id="aspnetForm">
Technorati Tags: ,,,
12.03.2009 06:55 by Danny Gershman | Comments (0) | Permalink

Run ASP.NET Development Server without Visual Studio

Ever want to run ASP.NET development server without having to fire up Visual Studio Debugger.

You can create your own scripts or commands by being able to access this.

%WINDOWS PATH%\Microsoft.NET\Framework\v2.0.50727\WebDev.WebServer.exe

Here is the command line options as shown without setting any parameters.  Enjoy!

image

08.05.2009 07:11 by Danny Gershman | Comments (3) | Permalink

Getting Crystal Reports XI Release 2 to install

 

I was facing some issues getting Crystal Reports XI Release 2 to install.   The installer was complaining that .NET 1.0 or 1.1 should be installed.   This was kind of a confusing message, since .NET 1.0 and 1.1 were both installed on the box.

The workaround is to uninstall all versions of .NET except 1.0 and 1.1 temporarily.  In my case I had to uninstall .NET 2.0, 3.0, and 3.5 to get this to work.  Once it was completed installing, I reinstalled all the uninstalled versions of .NET.

Technorati Tags: ,
05.27.2009 04:31 by Danny Gershman | Comments (0) | Permalink

How to determine if an assembly was compiled as debug

First thing to do is get Red Gate’s .NET Reflector.  Drop the assembly and run the disassemble.  You should see in the Assembly Metadata metadata that says

Debuggable("True, True")

03.10.2009 17:51 by Danny Gershman | Comments (1) | Permalink