Archive for c sharp tag

From the vaults of Twitter

July 10th 2008 • .NET, Apple, Microsoft (, , , ) • 272 views • 4 responses

I don’t normally republish my Tweets but are my highlights.

damienguard:
Methods returning "this" is a hack for fluency. Let’s get ".." added to the C# compiler to operate on previous object. a.This()..That()

lazycoder:
@damienguard I can’t decide if that’s genius or insanity. Should we add the "~" operator to refer back to the top of the inherit. chain? ;)

LostInTangent:
@damienguard I’ve started using Envy Code R for most of my applications (not just VS) and I have to say I’m loving it.

damienguard:
@LostInTangent: Envy Code R PR8 soon – Greek chars, improved hinting and some glyph revisions subscript/fractions & *96 redone.

damienguard:
Statically typed languages are not flexible enough to develop dynamically linked libraries.

damienguard:
Renaming your wifi router StupidRouter does not alas shame it into being more reliable.

damienguard:
@command_tab: Am I the only one who finds paying for pretty UI’s to leverage free software that took much more effort to develop offensive?

damienguard:
Just took delivery on my Alps-switched keyboard… feels good so far… but let’s see if co-workers complain about the noise.

Plip:
@damienguard I CAN’T HEAR MYSELF THINK FOR THAT INFERNAL CLICKING !

damienguard:
@lancefisher The alps keyboard was from DSI USA… but don’t order one, terrible 2-key limits prevent fast typing.

damienguard:
Apple should add hobbyist to its OS X line-up. Make kernel easier to switch, remove the h/w lockdown and no support.

damienguard:
Standard windows font smoothing’s real problem is lack of scales. Convert a ClearType rendering to greyscale in Photoshop…

[)amien

Object Initializers in .NET 3.5

November 4th 2007 • .NET (, , ) • 1,292 views • 2 responses

One compiler improvement in .NET 3.5 is the object initializers feature that lets you concisely set properties of an object as you create it.

If you’ve ever used VB you may well have found and enjoyed the with keyword to write code such as:

Dim myObj As MyClass
myObj = New MyClass()
With myObj
  .ProductCode = "ABC123"
  .Quantity = 5
  .Cost = 567.89
End With

This is more concise than writing myObj several times over, especially if setting a large number of properties, but as C# has no such keyword many people resorted to providing helpful constructors to facilitate code like:

MyClass myObj = new MyClass("ABC123", 5, 567.89);

If all three of these properties are essential then this makes for a sensible constructor however many classes have a number of properties that are optional and class designers struggle to determine whether to make constructors that merely cut-down on typing and which of the various combinations of optional properties might make sense in having their own constructor.

Invariably the combination you might want doesn’t exist and if it does the chances of being able to understand which properties are being set from one of a number of constructors that take parameters of similar types is quite low unless you go and take a peek with the IntelliSense.

Using object initializers you can stick to creating constructors that reflect parameters necessary to ensure your object is in a valid state and forget about providing helpful ones for those optional parameters. In our example if we assume the ProductCode is essential and the others are optional we can write code like:

MyClass myObj = new MyClass("ABC123") { Quantity = 5, Cost = 567.89 };

Which is both concise and easy to understand. It also requires no work on the part of the class designer and therefore works with all your existing classes. You can also nest them to set properties that require more complex types such as:

MyClass myObj = new MyClass("ABC123") {
   Quantity = 5,
   Cost = 567.89,
   Category = new Category("A") { Description = "New machine" }
};

This feature is no use if your objects are immutable in which case constructors are your only friend.

[)amien

Extend HttpApplication with session counts and uptime

June 6th 2006 • .NET (, , ) • 1,903 views • 4 responses

It’s sometimes useful to know or display how many people are currently using your web application and how long it’s been up for.

As web applications normally inherit from System.Web.HttpApplication we can extend this class with a common re-usable class to add the required functionality.

public class WebApplication : System.Web.HttpApplication {
    private static readonlyDateTime pStartedAt = DateTime.Now;
    private static long pSessionCount = 0;

    public WebApplication() : base() { }

    public override void Init () {
        base.Init();
        HookEventHandlers();
    }

    public static long SessionCount {
        get { return pSessionCount; }
    }

    public static DateTime StartedAt {
        get { return pStartedAt; }
    }    
    public static TimeSpan Uptime {
        get { return DateTime.Now.Subtract(pStartedAt); }
    }

    private void HookEventHandlers () {
        IHttpModule httpModule = Modules["Session"];
        if (httpModule is SessionStateModule) {
            SessionStateModule sessionStateModule = ((SessionStateModule) httpModule);
            sessionStateModule.Start += new System.EventHandler(SessionStart);
            sessionStateModule.End += new System.EventHandler(SessionEnd);
        }
    }

    private void SessionStart (object sender, EventArgs e) {
        Interlocked.Increment(ref pSessionCount);
    }

    private void SessionEnd (object sender, EventArgs e) {
        Interlocked.Decrement(ref pSessionCount);
    }
}

The next step is to ensure your web application’s class – as defined by global.asax – is of the new WebApplication type. A good approach is to have your own Global.asax.cs file inherit directly from the new WebApplication;

public class App : WebApplication { ... }

Now those sessions and uptime are being tracked how do you get at them?

lblCurrentSessions.Text = WebApplication.SessionCount.ToString();
lblUptime.Text = WebApplication.Uptime.ToString();

This approach is only suitable for sites operating on a single web server.

[)amien