Fun with C# and nullable ints
Sometimes the 10K monkeys staff is too busy on mission critical projects like foosball stat trackers and comic book managers to keep up new C# syntax like nullable ints. How did I not notice earlier that you could declare an int as "int? in" Plus how great does "int?" look? I previously felt there wasn't enough use of the question mark in my code. So how is this useful and why did you waste 10 minutes blogging about this? Well to answer the first question, I personaly found them useful in data access classes where I have an update stored proccedure may update value that can be null. For example:
public void UpdateEmployee(int employeeID, int? age, string firstName, string lastName)
{
Database db = DatabaseFactory.CreateDatabase();
string sqlCommand = "UpdateEmployee;
DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand);
db.AddInParameter(dbCommand, "@EmployeeID", DbType.Int32, employeeID);
db.AddInParameter(dbCommand, "@Age", DbType.Int32, age);
db.AddInParameter(dbCommand, "@FirstName", DbType.String, firstName);
db.AddInParameter(dbCommand, "@LastName", DbType.String, lastName);
db.ExecuteNonQuery(dbCommand);
}
Now I can just pass null into my data class and let the db field stay null. I've also been working with the MS Enterprise Application block lately as you can see from the sample code. I'm sure I'll have much to complain about there in future blog entries. As for the second question, I have no idea.