Image Resizing in C#

   I got complaints from millions of readers that I have not posted any programming articles with actual code lately. I do not have anything to write about right now so I decided to pull out some old code. It seems that this one is the most useful piece of code I have ever written. I originally wrote it for my first project and then wrote it again for this very no-blog. I have been asked by several people how to do this and it seems that it is a common problem so maybe someone will find it with the help of mighty Google...
Posted by:   Stilgar
02:44 12.06.2009

Going Random on an Enumerable

   I have not published any programming articles recently and I know that a throng of readers eagerly await them so I decided to share an interesting task that was brought up by a guy on IRC. You have an enumerable sequence and you are required to write a method that selects a random element from the sequence with just one iteration over the sequence. This means that you are not allowed to call methods like Count because it will iterate through the sequence. You are not allowed to save the elements in another collection. Of course an even distribution of the results is required (i.e. every element should have equal chance to be selected). When you are ready look at the full text to see my solution...
Posted by:   Stilgar
00:37 26.01.2009

C# 4.0 Features Announced

   The big news (actually the big news was Windows Azure but I care more about C# 4.0) from yesterday is that Anders (Hallowed be his name) has spoken. The future of C# have been announced. I bet that many of you mortals are eagerly awaiting my interpretation of the words of the Prophet.
Posted by:   Stilgar
04:49 29.10.2008

OOP in Math

   My coworker Lyubo came up with the following OOP problem while we were having lunch the other day. He says it is a well known problem that you can see on numerous places but it is new to me.

   We know from mathematics that the square is a special case of rectangle. In OOP terms "special case" means inheritance. Obviously the mathematical square inherits from mathematical rectangle. Your task is to create OOP model for that relationship. Lets say that we want the rectangle to expose get property (getter for you Java readers) – the area of the rectangle. This is not as simple as it sounds. Here is one of the simplest (and very wrong) solutions:

   class Rectangle
   {
       /// <summary>
       /// A side of the rectangle
       /// </summary>
       public uint A { get; set; }

       /// <summary>
       /// B side of the rectangle
       /// </summary>
       public uint B { get; set; }

       virtual public uint Area
       {
           get { return A * B; }
       }
   }

   class Square : Rectangle
   {
       public override uint Area
       {
           get
           {
               return A * A;
           }
       }
   }

However this means that the B property is not actually used. This can lead to unexpected results in polymorphic client code. We expect a rectangle to have a area of A*B and somehow it will not if it is an instance of Square. We can override the property setters, add some exceptions but what inheritance means is that the child class can do ANYTHING that the base class can. If you need to add special case behavior for any of the child classes then the inheritance hierarchy you have created is simply wrong. So go ahead make it right... (or view full text for explanation)
Posted by:   Stilgar
00:20 25.10.2008

Functional Sieve of Eratosthenes

   I was thinking of how I can use functional constructs to implement Sieve of Eratosthenes and I bet that millions of readers are dying to learn what I came up with...
Posted by:   Stilgar
01:07 13.10.2008
First Previous ... 11 12 13 14 15 Next Last