One of the products that my team builds that I am most proud of is IIS 7. IIS 7 is a *major* update of our web-server stack, and introduces a significantly new and improved extensibility, configuration, and administration architecture. I've blogged about some of its features in the past here and here.
One of the questions I get asked fairly regularly is: "how can I can easily change different configuration settings in my web.config file based on whether my application is in a dev, qa, staging or production mode?" The most common scenario for this is one where an application uses different database connection-strings for testing and production purposes.
One of the subtle (but cool) language features of C# is the ?? "null coalescing" operator. This provides a nice, terse way to check whether a value is null, and if so return an alternate value.
Simple Example Usages
Several folks have blogged about the ?? operator in the past - read here, here, here, and here for some previous examples on how to use it. Simply put, the ?? operator checks whether the value provided on the left side of the expression is null, and if so it returns an alternate value indicated by the right side of the expression. If the value provided on the left side of the expression isn't null, then it returns the original value.
For example, let's assume we have a string variable "message". We could check whether message was null, and return an alternate value using the code below:
Because the "message" variable above wasn't null, the "result" variable is assigned the original "hello world" message value.
In the code snippet below, however, message is a null value, and so the ?? operator will return the alternate value we've provided:
The ?? operator works for both reference types and value types. For example, below we are checking whether the nullable integer "number" variable is null. Because it isn't, the result will be the original value (55):
If "number" is null, then result is assigned the value 0:
Using the ?? operator with LINQ
Last month I wrote a blog post that covered using the new LINQ to XML support in .NET 3.5. One of the "gotchas" you often need to deal with when handling raw XML are cases where XML shapes are irregular and/or missing elements/attributes. The ?? operator can be very useful in these scenarios.
For example, let's consider a scenario where we have an XML file or feed with the following contact data:
We could write the below LINQ to XML code in C# to open the XML file and retrieve back a sequence of anonymous type objects with "Name", "Title", "Email" and "YearsAtCompany" properties, and then databind the results to an <asp:gridview> control on a page:
Notice above how I'm using the explicit conversion operator support on the XElement class to retrieve strongly typed values from the XML. This enables me to just cast the c.Element("YearsAtCompany") value to an int - and it will then automatically convert the string value to an integer for me, and type the "YearsAtCompany" property on my anonymous type to be an int.
When we run the above code snippet, we'll then get a nice Gridview listing of our contacts:
This explicit conversion support works great when the <YearsAtCompany> element is always defined. But it will throw a runtime error in the case where we have a <Contact> that is missing a <YearsAtCompany> sub-element:
One way to fix this is to modify our LINQ to XML query so that we indicate that YearsAtCompany is a nullable integer. We can do this by changing the explicit cast to be (int?) instead of (int):
This enables our query to execute cleanly and not raise any errors. Instead a null value will be assigned to the YearsAtCompany property if no <YearsAtCompany> element is present in the XML.
When we run the application and databind the results to the Gridview, you can see the YearsAtCompany column for our third contact is empty as a result (since the value is null):
But what if we didn't want a missing XML value to result in a null integer value - but instead just indicate a value of 0?
Well.... that is where we can use the new ?? operator support. We can just modify the LINQ to XML query like below to indicate a default value of 0 if no element is present:
This indicates that if the <YearsAtCompany> element is missing in the XML, and the result would otherwise be null, instead assign a value of 0. Notice in the intellisense above how C# automatically detects that this means that the YearsAtCompany property on the new anonymous type will never be null - and so it marks the property to be of type (int) instead of (int?).
And now when we run the page we'll see a value of 0 show up in our third row instead of a blank value:
You can use the C# ?? operator in all types of scenarios. Hopefully the LINQ to XML scenario above provides some interesting food for thought on how you can use it with any LINQ scenario (including LINQ to SQL, LINQ to XML, LINQ to Objects, LINQ to SharePoint, etc).
Hope this helps,
One of the features that VS 2005 added was support for HTML source validation. This enabled you to validate your page markup against different HTML schemas and standards (XHTML Transitional/Strict, HTML 4.01, various browser types, etc). You could also use the HTML validation features to detect missing link, image or style references in your markup.
The last two days I've been speaking at the the MIX:UK conference that was held this week in London. We had a sold out crowd of 500 people come to learn more about some of the new Microsoft web technologies.
Over the last few weeks I've been writing a series of blog posts that cover LINQ to SQL. LINQ to SQL is a built-in O/RM (object relational mapper) that ships in the .NET Framework 3.5 release, and which enables you to model relational databases using .NET classes. You can use LINQ expressions to query the database with them, as well as update/insert/delete data.
Below are the first eight parts in this series:
- Part 1: Introduction to LINQ to SQL
- Part 2: Defining our Data Model Classes
- Part 3: Querying our Database
- Part 4: Updating our Database
- Part 5: Binding UI using the ASP:LinqDataSource Control
- Part 6: Retrieving Data Using Stored Procedures
- Part 7: Updating our Database using Stored Procedures
- Part 8: Executing Custom SQL Expressions
In Part 5 of the series I introduced the new <asp:LinqDataSource> control in .NET 3.5 and talked about how you can use it to easily bind ASP.NET UI controls to LINQ to SQL data models. I also demonstrated how to use it a little more in a follow-up post I did that discusses the new <asp:ListView> control (Part 1 - Building a Product Listing Page with Clean CSS UI).
Silverlight is a cross platform, cross browser plug-in that enables designers and developers to build rich media experiences and .NET based RIAs for the web. I first blogged about Silverlight back in May after we announced it at our MIX conference in Las Vegas.