Question of the day: Threading

Here’s a little question about Threading – this would be a great interview question, in my opinion!

Given the code below:

public class Producer
{
    [ThreadStatic]
    readonly static private Producer _instance = new Producer();

    static private int _instanceCount;
    readonly private int _instanceId;

    private Producer()
    {
        _instanceId = ++_instanceCount;
    }

    static public Producer Instance
    {
        get { return _instance; }
    }

    public string Name
    {
        get
        {
            return String.Format("Instance {0} of {1}",
                _instanceId, _instanceCount);
        }
    }
}

public class Consumer
{
    public Consumer()
    {
        Thread t1 = new Thread(IdentifyProducer);
        t1.Start();

        Thread t2 = new Thread(IdentifyProducer);
        t2.Start();

        t1.Join();
        t2.Join();
    }

    public void IdentifyProducer()
    {
        Console.WriteLine(Producer.Instance.Name);
    }
}

What will happen when you create a new instance of the Consumer class?

Posted in Daily Question, Threading at June 30th, 2010. 2 Comments.

Question of the day: Enums

Some fun with enums! Assume you have an enum defined as follows:

enum Frequency {
    None = 0,
    Annual = 1,
    SemiAnnual = 2,
    Quarterly = 4,
    Monthly = 12,
    Weekly = 52
}

Now, given the following block of code:

Frequency myFrequency = (Frequency) 104;
System.Console.WriteLine("My frequency is: {0}", myFrequency.ToString());
bool is100Defined = Enum.IsDefined(typeof(Frequency), 100);
bool is52Defined  = Enum.IsDefined(typeof(Frequency), 52);
  1. What is written to the console?
  2. What is the value of is100Defined?
  3. What is the value of is52Defined?

First correct answer gets absolutely nothing (but think how great you’ll feel!)

Posted in C#, Daily Question at October 7th, 2009. 2 Comments.

Reader challenge!

It’s been a while since we’ve had a reader question, so here’s one for you all. It’s not that I can’t solve this problem; it’s more that I’m wondering if there’s a more elegant solution.

The Problem: Given two dates, what’s the easiest way of determining if they fall in the same week?

Please submit your answers as a comment!

Posted in C#, Daily Question at November 3rd, 2008. 9 Comments.

Daily question: Fun with constructors

It’s been a while since anyone has posted a daily question, but here’s one to get y’all in the mood…

Given the following code:


public class BaseClass {
    public BaseClass() : this("Base value") {
        Console.WriteLine("Base: Empty");
    }

    public BaseClass(object value) {
        Console.WriteLine("Base with value: {0}", value);
    }
}

public class DerivedClass : BaseClass {
    public DerivedClass() : this("Derived value") {
        Console.WriteLine("Derived: Empty");
    }

    public DerivedClass(object value) : base(value) {
        Console.WriteLine("Derived with value: {0}", value);
    }
}

What will be printed to the console when a new DerivedClass object is created using the default DerivedClass constructor:


    new DerivedClass();

First person to post the correct answer wins a prize consiting solely of that warm fuzzy feeling you get by being the first person to answer a question correctly.

Posted in C#, Daily Question at July 10th, 2008. 3 Comments.

Best Visual Studio Shortcut

A work colleague and I were discussing what we thought the best Visual Studio keyboard shortcut was.
The shortcut had to be a VS one and not that of a third party (eg. Resharper).

We came to the conclusion that Alt-Shift-Enter to enter into and out of Full Screen mode took 1st prize.

The shortcut to save – ctrl-s – is pretty handy too, but we decided this was not unique to VS and was more of a windows common-look-and-feel item.

What’s your favourite VS keyboard shortcut?

Posted in .Net, Daily Question, Uncategorized at December 22nd, 2006. 4 Comments.

What’s the difference between <%= %> and <%# %>

A daily question from a while back that had perplexed me for ages:

Q: In a DataBound control, what’s the difference between using a <%= %> block, and a <%# %> block?

Of course I actually did know the difference, and when I should use each one, but Bender summed up the correct answer the best:

A: You use a <%# %> block if you need access to the underlying DataSource of the repeating control (e.g. you need to access the DataItem with an Eval statement, or you need access to the Container element).

When you do not need to access the underlying DataSource, use a <%= %> block instead.

Posted in .Net, Daily Question at November 20th, 2006. Comments Off.

Quickduck logo