Session and Cache are By Reference

Recently a colleague remarked that if he got a List of some type out of the ASP.NET Cache, and changed an item, the Cache item would also change.

That is correct. Session (InProc) , Cache and Application all return "live" references. A good writeup on this can be found by friend and fellow MVP Rick Strahl here:

If you do not want this behavior, you need to either delete the Session / Cache / Application object and replace it with what you want later, or store a clone by duplicating the object, doing your work on the original that you got out of Cache, then replacing the existing cached item with your clone that was not changed.

for example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CachebyRef
public class Person
public String FirstName { get; set; }
public String LastName { get; set; }

public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
List list = new List();
list.Add(new Person(){FirstName="Joe",LastName = "Blow"});
Cache["personList"] = list;
List newList = (List) Cache["personList"];
Person p = newList[0];
Response.Write(p.FirstName + ": " + p.LastName + "<br/>");
p.FirstName = "Altered";
p.LastName = "State";
List altList = (List) Cache["personList"];
Person q = altList[0];
// Surprise - the cached item contains the change:
Response.Write(q.FirstName + ": " + q.LastName);


When Job Growth Is Not Job Growth

The Department of Labor just reported that in February, employment fell by fewer-than-expected 36,000 jobs with the unemployment rate steady at 9.7%.

While we may hear the Obama Administration or Harry Reid trying to spin this as some sort of economic victory for their stimulus or otherwise trying to put the best face on the economy, this is in reality not very good news.

Let us remember that job growth needs to be around 125,000 jobs gained per month just to keep up with US population growth. To make up the roughly 10 million jobs it would take to get us to an employment situation about where we were near the peak of the economy, we'd have to see job growth of 350,000 per month for four years straight. Such a scenario seems unlikely while we live under the umbrella of the Obama-Pelosi-Reid cartel. The Obama Administration has quintupled, in just one year, the deficits that took its predecessor Bush eight years to create.

Looking at the current stagnation in employment, in large part due to the Obama Administration’s policies, it would not be surprising to see unemployment remain high until Obama’s term in office ends. Things could improve somewhat if the GOP takes back at least one house of Congress in November. Right now, it looks like unemployment will remain above 9% when we reach November’s elections and if it falls under that level, it will be offset by a large increase in discouraged workers who are not counted in the numbers because they’ve just given up looking.

Once again, the failed Keynesian tactics of having the government create and spend huge amounts of money, increasing the deficit to an unsustainable level in the vain hope that this will create jobs, simply does not work.

In an October 2009 study, Alberto Alesina and Silvia Ardagna of Harvard looked at large changes in fiscal policy in 21 nations in the Organization for Economic Cooperation and Development. They identified 91 episodes since 1970 in which policy moved to stimulate the economy. They compared the policy interventions that succeeded — those that were followed by robust growth — with those that failed.

They found that successful stimulus relies almost entirely on cuts in business and income taxes. Failed stimulus relies mostly on increases in government spending. These are facts. They cannot be replaced by ideology, no matter how inspired it may seem.

There is a small amount of hope: A $15 Billion jobs bill was just passed: The centerpiece of the Senate HIRE Act is $13 billion in incentives for private sector businesses to boost hiring in 2010. The incentive, called the "Hire Now Tax Cut" by its original Senate sponsors, combines payroll forgiveness for Social Security taxes paid on qualified new hires, along with a tax credit for then keeping them on the payroll for at least 52 weeks.

The problems I see here are that it is too little, and too late. In addition, the incentives are flawed – business gets the incentive only by hiring somebody who’s been out of work at least 60 days. In other words, we are incentivizing business to hire the less qualified.

Incidentally, I’ve been getting some static recently by self-appointed economic gurus that Ronald Reagan was an idiot and created the biggest tax increase in history. We know from official economic statistics that the seven year period from 1982 to 1989 was the greatest, consistent burst of economic activity ever seen in the U.S. In fact, it was the greatest economic expansion the world has ever seen - in any country, at any time.

When you add up the record of the Reagan years, up to and including the first year of President Bush - during which he faithfully continued Mr. Reagan's economic policies - the conclusion is clear, inescapable and stunning: America's Great Expansion, which lasted some 25 years until George Bush the Younger began dismantling it. Obama came in and finished the job. Once again, I say that Reagan “got it”. Flame me if you will, the facts don’t lie.

Until an Administration starts to look at the facts instead of attempting to "make its own history" with some preconceived progressive agenda, not much will change. Lyle Rossiter, in his book “The Liberal Mind” explains that modern Liberalism rejects the competence and sovereignty of the common man and subordinates him to the will of governments run by liberal elites. The rise to power of this liberal agenda has resulted from the belief that the state is a proper source from which to gratify the longings of the people for various forms of parental care.

There are some signs that America may grow out of its infantilism e.g., the Tea Party movement – but it will take time.


String.Empty vs “” vs String. IsNullOrEmpty

I was reading up on this recently and a couple of interesting points came to light:

myString = “” creates a new instance of type String.

myString = String.Empty does not.

The key item is doing comparisons. Here’s some sample test code:

class Program {
static void Main(string[] args)
String myString = "";
long a, b, c, d, e, f;
Console.WriteLine("Method 1...");
a = DateTime.Now.Ticks;
for (int index = 0; index < 100000000; index++)
bool isEmpty = myString == "";
b = DateTime.Now.Ticks;
Console.WriteLine("Method 2...");
c = DateTime.Now.Ticks;
for (int index = 0; index < 100000000; index++)
bool isEmpty = myString.Length == 0;
d = DateTime.Now.Ticks;
Console.WriteLine("Method 3...");
e = DateTime.Now.Ticks;
for (int index = 0; index < 100000000; index++)
bool isEmpty = String.IsNullOrEmpty(myString);
f = DateTime.Now.Ticks;
var Method1 = b - a;
var Method2 = d - c;
var Method3 = f - e;
Console.WriteLine("Method 1: bool isEmpty = myString == \"\" took {0} ticks", Method1);
Console.WriteLine("Method 2: bool isEmpty = myString.Length == 0; took {0} ticks", Method2);
Console.WriteLine("Method 3: bool isEmpty = String.IsNullOrEmpty(myString) took {0} ticks", Method3);

The results:

Method 1: bool isEmpty = myString == "" took 4836000 ticks
Method 2: bool isEmpty = myString.Length == 0; took 2340000 ticks
Method 3: bool isEmpty = String.IsNullOrEmpty(myString) took 3744000 ticks

Bottom Line? The consensus among at least 4 or 5 expert Tweeters who commented was, IsNullOrEmpty is worth the extra ticks to test for null, even with 10 million iterations in the test!

Note: At the request of Todd Spatafore, I did a Visual Studio 2010 .NET 4.0 app that adds “String.IsNullOrWhitespace”. It performed the worst of all. You can download the VS2010 test solution here:

N.B. Jeremy commented to try:

I did this and it actually ran faster than bool isEmpty =myString.Length == 0;

so you could use this and achieve the need to test for null, and still be the fastest of the three methods shown.