Reverse an Array

7 Comments »

It’s easy to reverse the contents of an array using C# generics:

Read the rest of this entry »

Params for Optional Arguments

No Comments »

The params keyword specifies a variable number of arguments to be passed to a constructor or method, such as:

public Test( string arg0, float arg1, params int[] args )
{
}

Read the rest of this entry »

Convert Generic ICollection

4 Comments »

As discussed in a previous article, Generics provides the ability to create strongly-typed collections in C#. Unfortunately, C# currently does not support generics variance, which would allow inheritance of generic types.

Read the rest of this entry »

Constructor Chaining

12 Comments »

When you want to share initialization code among multiple constructors, there are generally two approaches. 

Read the rest of this entry »

How to Cache an Object

No Comments »

For performance efficiency, you may wish to cache an object within another object. That sounds easy enough to do. But what if the object cannot be found? You have to take special care to ensure the object is not fetched repeatedly.

Read the rest of this entry »

Strings Don’t Add Up

4 Comments »

One way to format C# strings is to add strings together using the plus-sign operator, such as:

string fileStats =
    "Bytes=" + bytes.ToString() +
    ", Pages=" + pages.ToString() +
    ", Words=" + words.ToString();

This code will correctly produce the desired output but is inefficient and does not scale as well.

Read the rest of this entry »

Judge Character with Char.Is

No Comments »

There are multiple static “Is” methods on the Char structure that help determine a character’s category. All of these methods (except IsSurrogatePair) take a single character argument (or string and index) and return a boolean whether the character is in the corresponding category.

Read the rest of this entry »

Convert Binary to Base64 String

6 Comments »

Modern applications increasingly use plain text to store and share data, especially in XML and SOAP formats. However, binary data cannot be represented directly in plain text, so one popular method is to convert binary to Base64 format.

Read the rest of this entry »

String.IsNullOrEmpty Shootout

5 Comments »

Given a string ‘s’, which of the following expressions is faster?

1. String.IsNullOrEmpty( s )

2. s == null || s.Length == 0

Read the rest of this entry »

Having Pun with C#

8 Comments »

Here is a collection of puns that only a C# programmer could appreciate:

  • abstract art gallery() { … }
  • bool me_over;
  • byte me;
  • c++;
  • case in_point:
  • char broiled;
  • class action { … };
  • const int pressure;
  • Convert.ToString( hemp );
  • decimal results;
  • double jeopardy;
  • Exception taken;
  • event handling;
  • for (score = 20; years == 7; ) { … }
  • foreach (hot chick in this.room) { flirt(); }
  • float valve;
  • Graphics ex;
  • int erupt;
  • lock (this.up) { … }
  • long john_silver;
  • long walk(short pier) { … }
  • object strongly;
  • override your objection() { … }
  • private property keep_out() { … }
  • protected free speech() { … }
  • public display of_affection() { … }
  • return to_sender;
  • short circuit;
  • sizeof (the_situation);
  • static cling free() { … }
  • string cheese;
  • struct by_lightning { … };
  • take a; break;
  • throw up;
  • typeof (writer);
  • uint rested;
  • ulong for_love;
  • unsafe { at any = speed; }
  • using your.brain;
  • virtual void in_my_heart() { … }
  • void where_prohibited() { … }
  • while (e_coyote) { … }

And for the grand finale:

    struct SoftwareConsultant {
        double   salary;
        long     lunches;
        float    jobs;
        char     unstable;
        void     work;
        int      hiring_him_again;
        const    pain_in_the_backside;
        unsigned agreement;
        short    fuse;
        volatile personality;
        static   progress;
    };
    /* and there are no unions in sight */

Source: Some original, some from here and here.

« go backkeep looking »