<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Jim O'Neil's Blog : A-to-Z</title><link>http://blogs.msdn.com/jimoneil/archive/tags/A-to-Z/default.aspx</link><description>Tags: A-to-Z</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>.NET from A to Z – The Index</title><link>http://blogs.msdn.com/jimoneil/archive/2009/06/30/net-from-a-to-z-the-index.aspx</link><pubDate>Tue, 30 Jun 2009 14:45:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9808831</guid><dc:creator>joneil</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jimoneil/comments/9808831.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jimoneil/commentrss.aspx?PostID=9808831</wfw:commentRss><description>&lt;p&gt;&lt;img style="margin: 25px 0px 10px 15px; display: inline" title="Z" border="0" alt="Z" align="right" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/169c29c56ec4.NETfromAtoZ_F631/z_3.gif" height="72" /&gt;&lt;img style="margin: 25px 15px 10px 0px; display: inline" title="A" border="0" alt="A" align="left" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/169c29c56ec4.NETfromAtoZ_F631/a_9.gif" height="72" /&gt;As you might be aware, this week ended my A-Z series exploring various facets of Microsoft and .NET technologies.&amp;#160; It was actually a lot of fun to explore a different topic each week, and I’m even happier to say I didn’t miss one Monday, making me something like 1 for10 in terms of fulfilling New Year’s resolutions.&amp;#160; With 26 letters in the alphabet leading to 26 posts, that also means 2009 is half-over, yikes!&lt;/p&gt;  &lt;p&gt;Just to pull it all together, here’s a complete listing of the posts in this series.&amp;#160; I hope you got something out of one or more of the posts, and I’m always looking for new ideas, so let me know if there’s something you’d like to hear about on a more or less regular basis.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p align="center"&gt;&lt;a href="http://blogs.msdn.com/jimoneil/archive/2009/01/02/net-from-a-to-z.aspx"&gt;.NET from A-Z (introductory post)&lt;/a&gt;&lt;/p&gt;  &lt;table border="0" cellspacing="2" cellpadding="4" width="100%"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="middle" width="133"&gt;&lt;a href="http://blogs.msdn.com/jimoneil/archive/2009/01/05/a-is-for-azure.aspx"&gt;&lt;strong&gt;A&lt;/strong&gt; is for Azure&lt;/a&gt;&lt;/td&gt;        &lt;td valign="middle" width="151"&gt;&lt;a href="http://blogs.msdn.com/jimoneil/archive/2009/01/12/b-is-for-baml.aspx"&gt;&lt;strong&gt;B&lt;/strong&gt; is for BAML&lt;/a&gt;&lt;/td&gt;        &lt;td valign="middle" width="115"&gt;&lt;a href="http://blogs.msdn.com/jimoneil/archive/2009/01/19/c-is-for-color.aspx"&gt;&lt;strong&gt;C&lt;/strong&gt; is for Color&lt;/a&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="middle" width="133"&gt;&lt;a href="http://blogs.msdn.com/jimoneil/archive/2009/01/26/d-is-for-dependency-property.aspx"&gt;&lt;strong&gt;D&lt;/strong&gt; is for Dependency Property&lt;/a&gt;&lt;/td&gt;        &lt;td valign="middle" width="151"&gt;&lt;a href="http://blogs.msdn.com/jimoneil/archive/2009/02/02/e-is-for-extension-method.aspx"&gt;&lt;strong&gt;E&lt;/strong&gt; is for Extension Method&lt;/a&gt;&lt;/td&gt;        &lt;td valign="middle" width="115"&gt;&lt;a href="http://blogs.msdn.com/jimoneil/archive/2009/02/09/f-is-for-f.aspx"&gt;&lt;strong&gt;F&lt;/strong&gt; is for F#&lt;/a&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="middle" width="133"&gt;&lt;a href="http://blogs.msdn.com/jimoneil/archive/2009/02/16/g-is-for-generation.aspx"&gt;&lt;strong&gt;G&lt;/strong&gt; is for Generation&lt;/a&gt;&lt;/td&gt;        &lt;td valign="middle" width="151"&gt;&lt;a href="http://blogs.msdn.com/jimoneil/archive/2009/02/23/h-is-for-hierarchyid.aspx"&gt;&lt;strong&gt;H&lt;/strong&gt; is for HierarchyID&lt;/a&gt;&lt;/td&gt;        &lt;td valign="middle" width="115"&gt;&lt;a href="http://blogs.msdn.com/jimoneil/archive/2009/03/02/i-is-for-isolated-storage.aspx"&gt;&lt;strong&gt;I&lt;/strong&gt; is for Isolated Storage&lt;/a&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="middle" width="133"&gt;&lt;a href="http://blogs.msdn.com/jimoneil/archive/2009/03/09/j-is-for-json.aspx"&gt;&lt;strong&gt;J&lt;/strong&gt; is for JSON&lt;/a&gt;&lt;/td&gt;        &lt;td valign="middle" width="151"&gt;&lt;a href="http://blogs.msdn.com/jimoneil/archive/2009/03/16/k-is-for-key.aspx"&gt;&lt;strong&gt;K &lt;/strong&gt;is for Key&lt;/a&gt;&lt;/td&gt;        &lt;td valign="middle" width="115"&gt;&lt;a href="http://blogs.msdn.com/jimoneil/archive/2009/03/23/l-is-for-lambda.aspx"&gt;&lt;strong&gt;L&lt;/strong&gt; is for Lambda&lt;/a&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="middle" width="133"&gt;&lt;a href="http://blogs.msdn.com/jimoneil/archive/2009/03/30/m-is-for-m.aspx"&gt;&lt;strong&gt;M&lt;/strong&gt; is for M&lt;/a&gt;&lt;/td&gt;        &lt;td valign="middle" width="151"&gt;&lt;a href="http://blogs.msdn.com/jimoneil/archive/2009/04/06/n-is-for-nullable-t.aspx"&gt;&lt;strong&gt;N&lt;/strong&gt; is for Nullable&amp;lt;T&amp;gt;&lt;/a&gt;&lt;/td&gt;        &lt;td valign="middle" width="115"&gt;&lt;a href="http://blogs.msdn.com/jimoneil/archive/2009/04/13/o-is-for-oomph.aspx"&gt;&lt;strong&gt;O&lt;/strong&gt; is for Oomph&lt;/a&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="middle" width="133"&gt;&lt;a href="http://blogs.msdn.com/jimoneil/archive/2009/04/20/p-is-for-partial-method.aspx"&gt;&lt;strong&gt;P&lt;/strong&gt; is for Partial Method&lt;/a&gt;&lt;/td&gt;        &lt;td valign="middle" width="151"&gt;&lt;a href="http://blogs.msdn.com/jimoneil/archive/2009/04/27/q-is-for-quirks.aspx"&gt;&lt;strong&gt;Q&lt;/strong&gt; is for Quirks&lt;/a&gt;&lt;/td&gt;        &lt;td valign="middle" width="115"&gt;&lt;a href="http://blogs.msdn.com/jimoneil/archive/2009/05/04/r-is-for-rest.aspx"&gt;&lt;strong&gt;R&lt;/strong&gt; is for REST&lt;/a&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="middle" width="133"&gt;&lt;a href="http://blogs.msdn.com/jimoneil/archive/2009/05/11/s-is-for-sqlmetal.aspx"&gt;&lt;strong&gt;S&lt;/strong&gt; is for SQLMetal&lt;/a&gt;&lt;/td&gt;        &lt;td valign="middle" width="151"&gt;&lt;a href="http://blogs.msdn.com/jimoneil/archive/2009/05/18/t-is-for-tracepoint.aspx"&gt;&lt;strong&gt;T&lt;/strong&gt; is for Tracepoint&lt;/a&gt;&lt;/td&gt;        &lt;td valign="middle" width="115"&gt;&lt;a href="http://blogs.msdn.com/jimoneil/archive/2009/05/25/u-is-for-unchecked.aspx"&gt;&lt;strong&gt;U&lt;/strong&gt; is for Unchecked&lt;/a&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="middle" width="133"&gt;&lt;a href="http://blogs.msdn.com/jimoneil/archive/2009/06/01/v-is-for-velocity.aspx"&gt;&lt;strong&gt;V&lt;/strong&gt; is for Velocity&lt;/a&gt;&lt;/td&gt;        &lt;td valign="middle" width="151"&gt;&lt;a href="http://blogs.msdn.com/jimoneil/archive/2009/06/08/w-is-for-weakreference.aspx"&gt;&lt;strong&gt;W&lt;/strong&gt; is for WeakReference&lt;/a&gt;&lt;/td&gt;        &lt;td valign="middle" width="115"&gt;&lt;a href="http://blogs.msdn.com/jimoneil/archive/2009/06/15/x-is-for-xml-literal.aspx"&gt;&lt;strong&gt;X&lt;/strong&gt; is for XML Literal&lt;/a&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="middle" width="133"&gt;&lt;a href="http://blogs.msdn.com/jimoneil/archive/2009/06/22/y-is-for-yield.aspx"&gt;&lt;strong&gt;Y&lt;/strong&gt; is for Yield&lt;/a&gt;&lt;/td&gt;        &lt;td valign="middle" width="151"&gt;&lt;a href="http://blogs.msdn.com/jimoneil/archive/2009/06/29/z-is-for-zermatt.aspx"&gt;&lt;strong&gt;Z&lt;/strong&gt; is for Zermatt&lt;/a&gt;&lt;/td&gt;        &lt;td valign="middle" width="115"&gt;&amp;#160;&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9808831" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jimoneil/archive/tags/A-to-Z/default.aspx">A-to-Z</category></item><item><title>Z is for… Zermatt</title><link>http://blogs.msdn.com/jimoneil/archive/2009/06/29/z-is-for-zermatt.aspx</link><pubDate>Mon, 29 Jun 2009 07:01:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9807967</guid><dc:creator>joneil</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jimoneil/comments/9807967.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jimoneil/commentrss.aspx?PostID=9807967</wfw:commentRss><description>&lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px 10px 0px 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Z" border="0" alt="Z" align="left" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/ZisforZermatt_C9C8/z_3.gif" width="54" height="64" /&gt; &lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.zermatt.ch/en/"&gt;Zermatt&lt;/a&gt; is, well actually &lt;em&gt;was&lt;/em&gt;, the code name for Microsoft’s next generation identity and access management API.&amp;#160;&amp;#160; Zermatt is now known by its new code name of “Geneva” Framework, but short of &lt;a href="http://msdn.microsoft.com/en-us/library/ms531188(VS.85).aspx"&gt;z-index&lt;/a&gt;, I was coming up empty on something to cover for this final post of my A-Z series – so let’s just call it poetic license.&amp;#160; &lt;/p&gt;  &lt;p&gt;The “Geneva” Framework is just one part of this new &lt;em&gt;&lt;a href="http://msdn.microsoft.com/en-us/magazine/cc163366.aspx"&gt;claims-based identity&lt;/a&gt;&lt;/em&gt; platform, which also comprises “Geneva” Server and Windows CardSpace “Geneva”.&amp;#160; Its two primary goals are to simplify the single sign-on process within the enterprise and to enable existing systems to interoperate with new hosted services and service-oriented architecture implementations. &lt;/p&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="&amp;quot;Geneva&amp;quot; Overview" border="0" alt="&amp;quot;Geneva&amp;quot; Overview" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/ZisforZermatt_C9C8/image_3.png" width="505" height="307" /&gt; &lt;/p&gt;  &lt;p&gt;Let’s take a brief look at the three components, and then I’ll offer a number of links for further reference.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h1&gt;“Geneva” Server&lt;/h1&gt;  &lt;p&gt;“Geneva” Server is a &lt;em&gt;Security Token Service (STS)&lt;/em&gt; that transforms &lt;em&gt;claims&lt;/em&gt; into &lt;em&gt;tokens&lt;/em&gt;.&amp;#160; Clear as mud? let’s take a step back to get a handle on what that means.&lt;/p&gt;  &lt;p&gt;A claim is simply a piece of information about a specific identity (often a user), and it’s the assembly of one or more of these claims into a (&lt;em&gt;security) token&lt;/em&gt; that drives the authentication model.&amp;#160; A claim could be someone’s name, their age, a password, or really any other piece of information that’s relevant to identifying valid users to a given application or computing environment.&amp;#160; Tokens are interpreted by the application to which the user is requesting access, and it’s up to that application (the “relying party” in the picture above) to decide whether it trusts the provider of the token and the claims it is making.&lt;/p&gt;  &lt;p&gt;While the specific claims aren’t all that important, knowing those claims are accurate and valid is paramount, so the token (which contains the claims) is digitally signed by a trusted party to verify its source and guard against tampering.&amp;#160; The issuer of a signed token is referred to as a Security Token Service (STS), and the organization hosting the STS (the “trusted party”) is termed the&lt;em&gt; &lt;strong&gt;identity provider&lt;/strong&gt;&lt;/em&gt; – they are the ones attesting that the claims in the tokens they issue are valid.&amp;#160; In the Windows Live ID service, for instance, Microsoft is acting as the identity provider.&lt;/p&gt;  &lt;p&gt;“Geneva” Server, which is the next generation of &lt;a href="http://www.microsoft.com/WindowsServer2003/R2/Identity_Management/ADFSwhitepaper.mspx"&gt;Microsoft’s Active Directory Federation Services (ADFS)&lt;/a&gt;, implements just such a Security Token Service.&amp;#160;&amp;#160; An organization could then choose “Geneva” Server to implement their own claims-based identity provider, built on Active Directory Domain Services, and act as an identity provider to applications within their enterprise or elsewhere.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h1&gt;Windows CardSpace “Geneva”&lt;/h1&gt;  &lt;p&gt;Cardspace “Geneva” is the user-focused component of the “Geneva” platform.&amp;#160; In the simplest of scenarios, a user would have one identity, managed by an STS like “Geneva” Server, and she’d request the STS to provide the claims associated with her identity to authenticate herself to every application.&amp;#160; &lt;/p&gt;  &lt;p&gt;That’s a rather myopic view though.&amp;#160; Just as we have multiple identification facets in our physical lives (social security numbers, driver’s licenses, passports, etc.) that we use in different contexts, so do we have multiple digital identities that we use for different applications and services (think for instance of all of the various passwords you’re keeping track of now!)&amp;#160; My Facebook account, for instance, doesn’t need to know what my Blue Cross number is, but my on-line pharmacy probably does.&lt;/p&gt;  &lt;p&gt;This is where the concept of an&lt;em&gt; identity selector&lt;/em&gt; comes in.&amp;#160; When a user requests the services of an application, that application will forward a list of its needs – in terms of token types, claim requirements, and the identity providers it trusts.&amp;#160; The identity selector, here “Geneva” Cardspace, can review the requirements and present the user a list of the available identities (presented as &lt;em&gt;Information Cards&lt;/em&gt;) that meet the authentication needs of the application.&amp;#160; The information card contains the information needed to locate the appropriate STS and obtain the requisite token; the card itself doesn’t store any claims or token information.&lt;/p&gt;  &lt;p&gt;“Geneva” Cardspace also handles the user interface, so that there is a consistent experience across Windows applications relying on identity selection.&amp;#160; The interface presented to the user looks something like the following, with a link (“What information will be sent”) to provide more information on exactly what specific pieces of information (claims) will be sent to the relying party application.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="CardSpace User Interface" border="0" alt="CardSpace User Interface" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/ZisforZermatt_C9C8/image_6.png" width="368" height="288" /&gt; &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h1&gt;“Geneva” Framework (previously “Zermatt”)&lt;/h1&gt;  &lt;p&gt;The “Geneva” Framework (my “Z” for this post) provides the programmatic interface for .NET developers to validate and consume claims in client applications (the “relying party” in the figure presented at the beginning of this post).&amp;#160; The .NET Framework 3.0 does include a few classes in &lt;code&gt;System.IdentityModel.dll&lt;/code&gt; to do claims-based programming; however, the developer story wasn’t too compelling in terms of openness and ease of use.&lt;/p&gt;  &lt;p&gt;At the forefront of the new framework (incorporated into &lt;code&gt;Microsoft.IdentityModel.dll&lt;/code&gt;), is the &lt;code&gt;Claim&lt;/code&gt; class, which contains several properties:&lt;/p&gt;  &lt;h2&gt;ClaimType&lt;/h2&gt;  &lt;blockquote&gt;   &lt;p&gt;Typically a URI that indicates what the claim is.&amp;#160; This URI could be anything that your application and issuer can agree on.&amp;#160; In CardSpace, a user’s first name would have a &lt;code&gt;ClaimType&lt;/code&gt; of &lt;code&gt;http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname&lt;/code&gt;&amp;#160; &lt;/code&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;h2&gt;&lt;code&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/code&gt;&lt;/h2&gt;  &lt;p&gt;&lt;/p&gt;  &lt;h2&gt;   &lt;p&gt;Value and ValueType&lt;/p&gt; &lt;/h2&gt;  &lt;p&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;code&gt;Value&lt;/code&gt; indicates the value of the claim, such as “Jim” for a first name.&amp;#160; This is always a string value.&amp;#160; The &lt;code&gt;ValueType&lt;/code&gt; property indicates the underlying XML schema type used to serialize and deserialize the value. There’s a number of predefined schema type strings you can use in the static class &lt;code&gt;ClaimValueTypes&lt;/code&gt;; for instance, a date value would be serialized per &lt;code&gt;http://www.w3.org/2001/XMLSchema#date&lt;/code&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;h2&gt;Properties&lt;/h2&gt;  &lt;blockquote&gt;   &lt;p&gt;An &lt;code&gt;IDictionary&amp;lt;string, string&amp;gt;&lt;/code&gt; of optional metadata related to the claim.&lt;/p&gt; &lt;/blockquote&gt;  &lt;h2&gt;Issuer and OriginalIssuer&lt;/h2&gt;  &lt;blockquote&gt;   &lt;p&gt;A name for the issuer (STS) of the claim.&amp;#160; In a federated scenario, where two or more issuers are involved, the &lt;code&gt;Issuer&lt;/code&gt; is the last in the chain, and &lt;code&gt;OriginalIssuer&lt;/code&gt; is the first in the chain.&lt;/p&gt; &lt;/blockquote&gt;  &lt;h2&gt;Subject&lt;/h2&gt;  &lt;blockquote&gt;   &lt;p&gt;An object implementing &lt;code&gt;IClaimsIdentity&lt;/code&gt; that provides additional information about the user’s identity.&amp;#160; &lt;code&gt;IClaimsIdentity&lt;/code&gt; extends the existing &lt;code&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.security.principal.iidentity.aspx"&gt;IIdentity&lt;/a&gt;&lt;/code&gt; interface, which provides only the name of the user.&amp;#160; &lt;code&gt;IClaimsIdentity&lt;/code&gt; exposes a &lt;code&gt;ClaimsCollection&lt;/code&gt; as well as a way to specify which claim corresponds to the user’s name and roles.&lt;/p&gt;    &lt;p&gt;The “Geneva” Framework also exposes a &lt;code&gt;IClaimsPrincipal&lt;/code&gt; interface, with a default implementation in &lt;code&gt;ClaimsPrincipal&lt;/code&gt;.&amp;#160; &lt;code&gt;IClaimsPrincipal&lt;/code&gt; extends the existing &lt;code&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.security.principal.iprincipal.aspx"&gt;IPrincipal&lt;/a&gt;&lt;/code&gt; class to include a collection of &lt;code&gt;IClaimsIdentities&lt;/code&gt;.&amp;#160; In most, cases there will be one item in this collection, but it does make it possible to support scenarios where the relying application requests multiple tokens from different issuers. &lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;The addition of the new interfaces and enhancement of the &lt;code&gt;Claims&lt;/code&gt; class does add a little more complexity, but given the extension of existing interfaces, you can access the claims information using the same constructs you’ve used before to get the user information, by accessing the &lt;code&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.threading.thread.currentprincipal.aspx"&gt;Thread.CurrentPrincipal&lt;/a&gt;&lt;/code&gt; or &lt;code&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.web.ui.page.user.aspx"&gt;User.Identity&lt;/a&gt;&lt;/code&gt; in an ASP.NET page.&amp;#160; For example,&lt;/p&gt;  &lt;div id="codeSnippetWrapper"&gt;   &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;IClaimsIdentity claimsIdentity = &lt;br /&gt;    Thread.CurrentPrincipal.Identity &lt;span style="color: #0000ff"&gt;as&lt;/span&gt; IClaimsIdentity;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;The &lt;em&gt;&lt;a href="http://download.microsoft.com/download/7/d/0/7d0b5166-6a8a-418a-addd-95ee9b046994/GenevaFrameworkWhitepaperForDevelopers.pdf"&gt;Microsoft Code Name &amp;quot;Geneva&amp;quot; Framework Whitepaper for Developers&lt;/a&gt; &lt;/em&gt;is a great source of information for using these classes within your applications. In fact, it’s interesting (and not too surprising) to note that “Geneva” Server actually uses the “Geneva” Framework itself to implement its own security token service.&amp;#160; If you’re concerned over all the infrastructure required to try all this out, you’ll be happy to know that if you &lt;a href="http://msdn.microsoft.com/en-us/evalcenter/dd440951.aspx"&gt;download the Beta 2 release&lt;/a&gt;, there’s an option to get a pre-configured VPC to facilitate your evaluation.&lt;/p&gt;

&lt;p&gt;One final point to note is that while these three components provide a solid infrastructure for claims-based identity, they do not form an all-or-nothing proposition.&amp;#160; “Geneva” is Microsoft’s implementation of standards-based technologies like &lt;a href="http://docs.oasis-open.org/ws-sx/ws-trust/v1.3/ws-trust.html"&gt;WS-Trust&lt;/a&gt; and &lt;a href="http://www.oasis-open.org/specs/index.php#saml"&gt;SAML 2.0&lt;/a&gt;.&amp;#160; You don’t have to use “Geneva” server to use CardSpace; in fact, you might not even need an identity selector for simple scenarios.&amp;#160; Likewise, applications built using the “Geneva” Framework can process claims served from sources other than “Geneva” Server.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&amp;#160;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;References&lt;/h1&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/security/aa570351.aspx"&gt;MSDN Security Developer Center&lt;/a&gt;&lt;/p&gt;

  &lt;p&gt;&lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=9ca5c685-3172-4d8f-81cb-1a59bdc9f7e3&amp;amp;displaylang=en"&gt;“Geneva” Claims Based Access Platform (whitepapers)&lt;/a&gt;&lt;/p&gt;

  &lt;p&gt;&lt;a href="http://download.microsoft.com/download/7/d/0/7d0b5166-6a8a-418a-addd-95ee9b046994/GenevaFrameworkWhitepaperForDevelopers.pdf"&gt;“Geneva” Framework Whitepaper for Developers&lt;/a&gt;&lt;/p&gt;

  &lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/evalcenter/dd440951.aspx"&gt;“Geneva” Beta 2 Download&lt;/a&gt;&lt;/p&gt;

  &lt;p&gt;Channel 9: &lt;a href="http://channel9.msdn.com/shows/Identity/"&gt;The Id Element&lt;/a&gt;&lt;/p&gt;

  &lt;p&gt;&lt;a href="http://blogs.msdn.com/card/"&gt;“Geneva” Team Blog&lt;/a&gt;&lt;/p&gt;

  &lt;blockquote&gt;
    &lt;p&gt;&lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=9ca5c685-3172-4d8f-81cb-1a59bdc9f7e3&amp;amp;displaylang=en"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;/blockquote&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9807967" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jimoneil/archive/tags/A-to-Z/default.aspx">A-to-Z</category></item><item><title>Y is for… Yield</title><link>http://blogs.msdn.com/jimoneil/archive/2009/06/22/y-is-for-yield.aspx</link><pubDate>Mon, 22 Jun 2009 07:01:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9792685</guid><dc:creator>joneil</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jimoneil/comments/9792685.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jimoneil/commentrss.aspx?PostID=9792685</wfw:commentRss><description>&lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px 15px 0px 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="y" border="0" alt="y" align="left" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/YisforYield_10515/y_3.gif" width="61" height="69" /&gt;&lt;/p&gt;  &lt;p&gt;Before we talk specifically about the &lt;code&gt;yield&lt;/code&gt; keyword, let’s review a few constructs you probably use everyday, namely collection classes like lists and arrays.&amp;#160; We’re quite used to traversing these simply with a &lt;code&gt;foreach&lt;/code&gt; loop, and what enables us to do so is that these types implement the &lt;code&gt;System.Collections.IEnumerable&lt;/code&gt; interface.&lt;/p&gt;  &lt;p&gt;&lt;code&gt;IEnumerable&lt;/code&gt; is a rather simple interface that requires implementing a single method, &lt;code&gt;GetEnumerator&lt;/code&gt;, which returns an object that implements another interface, &lt;code&gt;IEnumerator&lt;/code&gt;. &lt;code&gt;IEnumerator&lt;/code&gt;, in turn, encapsulates two methods and a property:&lt;/p&gt;  &lt;table border="0" cellspacing="0" cellpadding="2" width="400" align="center"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="111"&gt;&lt;code&gt;MoveNext&lt;/code&gt;&lt;/td&gt;        &lt;td valign="top" width="289"&gt;called when advancing to the next element in the sequence or collection; it returns false when at the end of the collection.&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="111"&gt;&lt;code&gt;Current&lt;/code&gt;&lt;/td&gt;        &lt;td valign="top" width="289"&gt;obtains the &lt;code&gt;System.Object&lt;/code&gt; at the current spot in the collection&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="111"&gt;&lt;code&gt;Reset&lt;/code&gt;&lt;/td&gt;        &lt;td valign="top" width="289"&gt;reinitializes the list or sequence&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;&amp;#160; &lt;br /&gt;Now, let’s say we wanted to build a implementation of a class that returns the first &lt;em&gt;n&lt;/em&gt; &lt;a href="http://en.wikipedia.org/wiki/Triangular_number"&gt;triangular numbers&lt;/a&gt;.&amp;#160; One way to do so would be the following:&lt;/p&gt;  &lt;div id="codeSnippetWrapper"&gt;   &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;     &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; TriNumbers : IEnumerable&amp;#160; &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt;      {   &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt;          &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; Int32[] _nums;  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt;          &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; TriNumbers(Int32 n)   &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt;          {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum6"&gt;   6:&lt;/span&gt;              &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (n &amp;lt;= 0) &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum7"&gt;   7:&lt;/span&gt;                  &lt;span style="color: #0000ff"&gt;throw&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; System.ArgumentOutOfRangeException();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum8"&gt;   8:&lt;/span&gt;&amp;#160; &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum9"&gt;   9:&lt;/span&gt;              _nums = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Int32[n];   &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum10"&gt;  10:&lt;/span&gt;              _nums[0] = 1;   &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum11"&gt;  11:&lt;/span&gt;              &lt;span style="color: #0000ff"&gt;for&lt;/span&gt; (Int32 i = 1; i &amp;lt; n; i++)  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum12"&gt;  12:&lt;/span&gt;                  _nums[i] = _nums[i-1] + i + 1;  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum13"&gt;  13:&lt;/span&gt;          } &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum14"&gt;  14:&lt;/span&gt;         &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum15"&gt;  15:&lt;/span&gt;          &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; IEnumerator GetEnumerator() &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum16"&gt;  16:&lt;/span&gt;          {  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum17"&gt;  17:&lt;/span&gt;              &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; _nums.GetEnumerator();  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum18"&gt;  18:&lt;/span&gt;          }  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum19"&gt;  19:&lt;/span&gt;      }&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;and then iterate over it with code like this.&amp;#160; &lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Program&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Main(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;[] args)&lt;br /&gt;    {&lt;br /&gt;        TriNumbers triNums = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; TriNumbers(10);&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (var val &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; triNums)&lt;br /&gt;            Console.WriteLine(val);&lt;br /&gt;        Console.ReadLine();&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;In the implementation of &lt;code&gt;TriNumbers&lt;/code&gt;, I sort of ‘cheated’ (line 17) by just deferring the iteration work to the iterator I get for free from the underlying integer array, which is really what’s storing my sequence.&amp;#160; &lt;/p&gt;

&lt;p&gt;Not that cheating is wrong in this case, but it does require my array to be populated from the get-go, in the constructor.&amp;#160; Initializing an array of ten elements is no big deal, but what if the argument to the constructor were much larger? and there are many instances of this class in memory?&amp;#160; and what if I only end up iterating over the first couple of values?&amp;#160; Clearly I’m using up memory here that isn’t really needed, and I’m taking time to calculate each element of the sequence, when I’m not really even sure I need them all.&amp;#160; If the calculation was CPU-intensive or required a series of database or services calls to build the list, there could be a lot of wasted cycles dedicated to populating data that might never be requested in the application.&lt;/p&gt;

&lt;p&gt;To make the class a little less wasteful up front, I can implement the &lt;code&gt;IEnumerator&lt;/code&gt; interface explicitly – something like this:&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; TriNumbers : IEnumerable&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; TriNumbersEnumerator _enum;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; TriNumbers(Int32 n)&lt;br /&gt;    {&lt;br /&gt;        _enum = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; TriNumbersEnumerator(n);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; IEnumerator GetEnumerator()&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; _enum;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; TriNumbersEnumerator : IEnumerator&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; Int32 _limit = 0;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; Int32 _index = 0;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; Int32 _value = 0;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; TriNumbersEnumerator(Int32 n)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (n &amp;lt;= 0)&lt;br /&gt;                &lt;span style="color: #0000ff"&gt;throw&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; System.ArgumentOutOfRangeException();&lt;br /&gt;            _limit = n;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;object&lt;/span&gt; Current&lt;br /&gt;        {&lt;br /&gt;            get&lt;br /&gt;            {&lt;br /&gt;                &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (_index == 0)&lt;br /&gt;                    &lt;span style="color: #0000ff"&gt;throw&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; System.InvalidOperationException(&lt;br /&gt;                            &lt;span style="color: #006080"&gt;&amp;quot;Enumeration has not started. Call MoveNext.&amp;quot;&lt;/span&gt;);&lt;br /&gt;                &lt;span style="color: #0000ff"&gt;else&lt;/span&gt;&lt;br /&gt;                    &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; _value;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;bool&lt;/span&gt; MoveNext()&lt;br /&gt;        {&lt;br /&gt;            _index++;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (_index &amp;lt;= _limit)&lt;br /&gt;            {&lt;br /&gt;                _value += _index;&lt;br /&gt;                &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;;&lt;br /&gt;            }&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;else&lt;/span&gt;&lt;br /&gt;            {&lt;br /&gt;                Reset();&lt;br /&gt;                &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;false&lt;/span&gt;;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Reset()&lt;br /&gt;        {&lt;br /&gt;            _index = 0;&lt;br /&gt;            _value = 0;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;That works, but there’s an easier way!&amp;#160; Enter &lt;code&gt;yield&lt;/code&gt; – not much longer than the original implementation, and certainly less wasteful of space and CPU cycles should the entire sequence not be enumerated.&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; TriNumbers : IEnumerable&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt; {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; Int32 _limit = 0;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; TriNumbers(Int32 n)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt;     {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum6"&gt;   6:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (n &amp;lt;= 0)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum7"&gt;   7:&lt;/span&gt;             &lt;span style="color: #0000ff"&gt;throw&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; System.ArgumentOutOfRangeException();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum8"&gt;   8:&lt;/span&gt;         _limit = n;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum9"&gt;   9:&lt;/span&gt;     }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum10"&gt;  10:&lt;/span&gt;&amp;#160; &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum11"&gt;  11:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; IEnumerator GetEnumerator()&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum12"&gt;  12:&lt;/span&gt;     {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum13"&gt;  13:&lt;/span&gt;         Int32 val = 0;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum14"&gt;  14:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;for&lt;/span&gt; (Int32 i = 0; i &amp;lt; _limit; i++)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum15"&gt;  15:&lt;/span&gt;         {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum16"&gt;  16:&lt;/span&gt;             val += i + 1;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum17"&gt;  17:&lt;/span&gt;             &lt;span style="color: #0000ff"&gt;yield&lt;/span&gt; &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; val;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum18"&gt;  18:&lt;/span&gt;         }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum19"&gt;  19:&lt;/span&gt;     }&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;div id="codeSnippetWrapper"&gt;&amp;#160;&lt;/div&gt;

&lt;div&gt;The magic is on line 17, with the &lt;code&gt;yield return&lt;/code&gt; statement.&amp;#160; Each time this statement is reached, the current value of &lt;code&gt;val&lt;/code&gt; is returned as the value of the &lt;code&gt;IEnumerator&lt;/code&gt; reference (namely, what the &lt;code&gt;foreach&lt;/code&gt; in the calling program will see).&amp;#160; The current location and state of the &lt;code&gt;GetEnumerator&lt;/code&gt; method is stored, and so the next time the iterator is called, we’ll get the next value from the for loop in line 14.&lt;/div&gt;

&lt;p&gt;While reading IL is not something I do often, it’s interesting to look at what gets generated when using the &lt;code&gt;yield return&lt;/code&gt; construct.&amp;#160; Using &lt;a href="http://msdn.microsoft.com/en-us/library/f7dy01k1(VS.80).aspx"&gt;IL DASM&lt;/a&gt;, you can see a &lt;code&gt;newobj&lt;/code&gt; call to a class called &lt;code&gt;d__0&lt;/code&gt;, essentially indicating that there is a new class being constructed under the covers. &lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="IL DASM output" border="0" alt="IL DASM output" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/YisforYield_10515/image_3.png" width="525" height="96" /&gt; &lt;/p&gt;

&lt;p&gt;IL DASM shows that that class is implementing the &lt;code&gt;IEnumerator&lt;/code&gt; interface (the &lt;code&gt;Current&lt;/code&gt; property and the &lt;code&gt;Reset&lt;/code&gt; and &lt;code&gt;MoveNext&lt;/code&gt; methods), so essentially the &lt;code&gt;yield return&lt;/code&gt; provides some &amp;quot;syntactic sugar&amp;quot; (more or less) for code similar to the &lt;code&gt;TriNumbersEnumerator&lt;/code&gt; class that I wrote above.&amp;#160; &lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="IL DASM" border="0" alt="IL DASM" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/YisforYield_10515/image_6.png" width="445" height="459" /&gt; &lt;/p&gt;

&lt;p&gt;
  &lt;br /&gt;The real story is a tad deeper than that though, and Wes Dyer does a great job of looking more closely at the generated class in &lt;a href="http://blogs.msdn.com/wesdyer/archive/2007/03/23/all-about-iterators.aspx"&gt;his blog post&lt;/a&gt;.&amp;#160; And if you’d like to do all this in Visual Basic, which unfortunately doesn’t have iterators (yet?), take a look at &lt;a href="http://blog.matthewdoig.com/?p=81"&gt;Matthew Doig’s&lt;/a&gt; blog.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9792685" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jimoneil/archive/tags/A-to-Z/default.aspx">A-to-Z</category></item><item><title>X is for… XML Literal</title><link>http://blogs.msdn.com/jimoneil/archive/2009/06/15/x-is-for-xml-literal.aspx</link><pubDate>Mon, 15 Jun 2009 07:23:41 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9752042</guid><dc:creator>joneil</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jimoneil/comments/9752042.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jimoneil/commentrss.aspx?PostID=9752042</wfw:commentRss><description>&lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px 10px 0px 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="X" border="0" alt="X" align="left" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/XisforXMLLiteral_581/x_3.gif" width="56" height="66" /&gt; &lt;/p&gt;  &lt;p&gt;XML Literal(s) is a feature that Visual Basic developers can call their own! The XML Literal syntax facilitates creating XML documents and elements that support the vast majority of the &lt;a href="http://www.w3.org/TR/xml/"&gt;XML 1.0 specification&lt;/a&gt;. Complementing the literals are &lt;em&gt;axis properties&lt;/em&gt; that aid in navigating and accessing XML elements and attributes.&lt;/p&gt;  &lt;p&gt;Let’s start with an example.&amp;#160; Below is an XML literal representing a menu; as you can see it’s assigned directly to a variable – here relying on implicit typing.&lt;/p&gt;  &lt;div id="codeSnippetWrapper"&gt;   &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;Dim xml = &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;menu&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;              &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;course&lt;/span&gt; &lt;span style="color: #ff0000"&gt;name&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;appetizer&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                  &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;dish&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;Shrimp Cocktail&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;dish&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                  &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;dish&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;Escargot&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;dish&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;              &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;course&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;              &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;course&lt;/span&gt; &lt;span style="color: #ff0000"&gt;name&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;main&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                  &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;dish&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;Filet Mignon&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;dish&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                  &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;dish&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;Garlic Potatoes&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;dish&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                  &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;dish&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;Broccoli&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;dish&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;              &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;course&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;              &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;course&lt;/span&gt; &lt;span style="color: #ff0000"&gt;name&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;dessert&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                  &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;dish&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;Chocolate Cheesecake&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;dish&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;              &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;course&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;menu&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;So what is the type of the &lt;code&gt;xml&lt;/code&gt; variable?&amp;#160; It’s a &lt;code&gt;System.Xml.Linq.XElement&lt;/code&gt;, which certainly brings to the forefront the synergy with &lt;a href="http://msdn.microsoft.com/en-us/library/bb397926.aspx"&gt;LINQ&lt;/a&gt; (Language Integrated Query).&amp;#160; Underneath the covers, the Visual Basic compiler calls the relevant constructors (&lt;code&gt;XElement&lt;/code&gt;, &lt;code&gt;XAttribute&lt;/code&gt;, etc.) to create the LINQ to XML object.&lt;/p&gt;

&lt;p&gt;Note though that &lt;code&gt;xml&lt;/code&gt; in the following snippet is an &lt;code&gt;XDocument&lt;/code&gt;, because of the inclusion of the xml version tag. &lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;Dim xml = &lt;span style="color: #0000ff"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color: #800000"&gt;xml&lt;/span&gt; &lt;span style="color: #ff0000"&gt;version&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt;?&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                  &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;menu&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                      &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;course&lt;/span&gt; &lt;span style="color: #ff0000"&gt;name&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;appetizer&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                          &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;dish&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;Shrimp Cocktail&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;dish&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                          &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;dish&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;Escargot&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;dish&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                      &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;course&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                  &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;menu&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;h2&gt;
  &lt;br /&gt;&lt;/h2&gt;

&lt;h1&gt;LINQ within XML Literals&lt;/h1&gt;

&lt;p&gt;You can include LINQ expressions with your literals as well, using an embedded expression syntax reminiscent of ASP!&amp;#160; The following code yields the same XML as the first example, but populates it using LINQ to Objects from a hard-coded list; of course, that data could just as easily come from a database or some other external source.&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;Dim&lt;/span&gt; menu &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; List(Of Dish) = &lt;span style="color: #0000ff"&gt;New&lt;/span&gt; List(Of Dish)()&lt;br /&gt;&lt;br /&gt;menu.Add(&lt;span style="color: #0000ff"&gt;New&lt;/span&gt; Dish(&lt;span style="color: #006080"&gt;&amp;quot;appetizer&amp;quot;&lt;/span&gt;, &lt;span style="color: #006080"&gt;&amp;quot;Shrimp Cocktail&amp;quot;&lt;/span&gt;))&lt;br /&gt;menu.Add(&lt;span style="color: #0000ff"&gt;New&lt;/span&gt; Dish(&lt;span style="color: #006080"&gt;&amp;quot;appetizer&amp;quot;&lt;/span&gt;, &lt;span style="color: #006080"&gt;&amp;quot;Escargot&amp;quot;&lt;/span&gt;))&lt;br /&gt;menu.Add(&lt;span style="color: #0000ff"&gt;New&lt;/span&gt; Dish(&lt;span style="color: #006080"&gt;&amp;quot;main&amp;quot;&lt;/span&gt;, &lt;span style="color: #006080"&gt;&amp;quot;Filet Mignon&amp;quot;&lt;/span&gt;))&lt;br /&gt;menu.Add(&lt;span style="color: #0000ff"&gt;New&lt;/span&gt; Dish(&lt;span style="color: #006080"&gt;&amp;quot;main&amp;quot;&lt;/span&gt;, &lt;span style="color: #006080"&gt;&amp;quot;Garlic Potatoes&amp;quot;&lt;/span&gt;))&lt;br /&gt;menu.Add(&lt;span style="color: #0000ff"&gt;New&lt;/span&gt; Dish(&lt;span style="color: #006080"&gt;&amp;quot;main&amp;quot;&lt;/span&gt;, &lt;span style="color: #006080"&gt;&amp;quot;Broccoli&amp;quot;&lt;/span&gt;))&lt;br /&gt;menu.Add(&lt;span style="color: #0000ff"&gt;New&lt;/span&gt; Dish(&lt;span style="color: #006080"&gt;&amp;quot;dessert&amp;quot;&lt;/span&gt;, &lt;span style="color: #006080"&gt;&amp;quot;Chocolate Cheesecake&amp;quot;&lt;/span&gt;))&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;Dim&lt;/span&gt; xml = &amp;lt;?xml version=&lt;span style="color: #006080"&gt;&amp;quot;1.0&amp;quot;&lt;/span&gt;?&amp;gt;&lt;br /&gt;          &amp;lt;menu&amp;gt;&lt;br /&gt;              &amp;lt;course name=&lt;span style="color: #006080"&gt;&amp;quot;appetizer&amp;quot;&lt;/span&gt;&amp;gt;&lt;br /&gt;                  &amp;lt;%= From m &lt;span style="color: #0000ff"&gt;In&lt;/span&gt; menu _&lt;br /&gt;                      Where m.Course = &lt;span style="color: #006080"&gt;&amp;quot;appetizer&amp;quot;&lt;/span&gt; _&lt;br /&gt;                      &lt;span style="color: #0000ff"&gt;Select&lt;/span&gt; &amp;lt;dish&amp;gt;&amp;lt;%= m.Food %&amp;gt;&amp;lt;/dish&amp;gt; _&lt;br /&gt;                  %&amp;gt;&lt;br /&gt;              &amp;lt;/course&amp;gt;&lt;br /&gt;              &amp;lt;course name=&lt;span style="color: #006080"&gt;&amp;quot;main&amp;quot;&lt;/span&gt;&amp;gt;&lt;br /&gt;                  &amp;lt;%= From m &lt;span style="color: #0000ff"&gt;In&lt;/span&gt; menu _&lt;br /&gt;                      Where m.Course = &lt;span style="color: #006080"&gt;&amp;quot;main&amp;quot;&lt;/span&gt; _&lt;br /&gt;                      &lt;span style="color: #0000ff"&gt;Select&lt;/span&gt; &amp;lt;dish&amp;gt;&amp;lt;%= m.Food %&amp;gt;&amp;lt;/dish&amp;gt; _&lt;br /&gt;                  %&amp;gt;&lt;br /&gt;              &amp;lt;/course&amp;gt;&lt;br /&gt;              &amp;lt;course name=&lt;span style="color: #006080"&gt;&amp;quot;dessert&amp;quot;&lt;/span&gt;&amp;gt;&lt;br /&gt;                  &amp;lt;%= From m &lt;span style="color: #0000ff"&gt;In&lt;/span&gt; menu _&lt;br /&gt;                      Where m.Course = &lt;span style="color: #006080"&gt;&amp;quot;dessert&amp;quot;&lt;/span&gt; _&lt;br /&gt;                      &lt;span style="color: #0000ff"&gt;Select&lt;/span&gt; &amp;lt;dish&amp;gt;&amp;lt;%= m.Food %&amp;gt;&amp;lt;/dish&amp;gt; _&lt;br /&gt;                  %&amp;gt;&lt;br /&gt;              &amp;lt;/course&amp;gt;&lt;br /&gt;          &amp;lt;/menu&amp;gt;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;XML literals can also be traversed via LINQ to XML.&amp;#160; For instance, the following snippet will result in a list of each of the dishes, without any XML markup.&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;Dim&lt;/span&gt; dishList = From d &lt;span style="color: #0000ff"&gt;In&lt;/span&gt; xml.Descendants(&lt;span style="color: #006080"&gt;&amp;quot;dish&amp;quot;&lt;/span&gt;) _&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;Select&lt;/span&gt; d.Value&lt;br /&gt;&lt;span style="color: #0000ff"&gt;For&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Each&lt;/span&gt; item &lt;span style="color: #0000ff"&gt;In&lt;/span&gt; dishList&lt;br /&gt;    Console.WriteLine(item)&lt;br /&gt;&lt;span style="color: #0000ff"&gt;Next&lt;/span&gt;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;h1&gt;&amp;#160;&lt;/h1&gt;

&lt;h1&gt;Axis Properties&lt;/h1&gt;

&lt;p&gt;Axis properties are a series of syntax elements that enable you to access elements and attributes within the XML.&amp;#160; There are three different properties that we’ll take a look at next.&lt;/p&gt;

&lt;h3&gt;Child axis&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;The child axis property returns all child elements of the source element.&amp;#160; The property is denoted by placing angle brackets around the child elements desired.&amp;#160; For example, this code&lt;/p&gt;

  &lt;div&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;Dim&lt;/span&gt; childAxis = xml.&amp;lt;menu&amp;gt;.&amp;lt;course&amp;gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;For&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Each&lt;/span&gt; item &lt;span style="color: #0000ff"&gt;In&lt;/span&gt; childAxis&lt;br /&gt;    Console.WriteLine(item)&lt;br /&gt;&lt;span style="color: #0000ff"&gt;Next&lt;/span&gt;&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div&gt;
    &lt;br /&gt;&lt;/div&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;div&gt;results in&lt;/div&gt;
&lt;/blockquote&gt;
&lt;code&gt;
  &lt;blockquote&gt;
    &lt;p&gt;&amp;lt;course name=&amp;quot;appetizer&amp;quot;&amp;gt; 
      &lt;br /&gt;&amp;#160; &amp;lt;dish&amp;gt;Shrimp Cocktail&amp;lt;/dish&amp;gt; 

      &lt;br /&gt;&amp;#160; &amp;lt;dish&amp;gt;Escargot&amp;lt;/dish&amp;gt; 

      &lt;br /&gt;&amp;lt;/course&amp;gt; 

      &lt;br /&gt;&amp;lt;course name=&amp;quot;main&amp;quot;&amp;gt; 

      &lt;br /&gt;&amp;#160; &amp;lt;dish&amp;gt;Filet Mignon&amp;lt;/dish&amp;gt; 

      &lt;br /&gt;&amp;#160; &amp;lt;dish&amp;gt;Garlic Potatoes&amp;lt;/dish&amp;gt; 

      &lt;br /&gt;&amp;#160; &amp;lt;dish&amp;gt;Broccoli&amp;lt;/dish&amp;gt; 

      &lt;br /&gt;&amp;lt;/course&amp;gt; 

      &lt;br /&gt;&amp;lt;course name=&amp;quot;dessert&amp;quot;&amp;gt; 

      &lt;br /&gt;&amp;#160; &amp;lt;dish&amp;gt;Chocolate Cheesecake&amp;lt;/dish&amp;gt; 

      &lt;br /&gt;&amp;lt;/course&amp;gt;&lt;/p&gt;
  &lt;/blockquote&gt;
&lt;/code&gt;

&lt;h3&gt;Descendant axis&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;The descendant axis property returns all elements under the source element, regardless of how deeply they are nested.&amp;#160; This property is denoted by placing angle brackets around the element desired and separating it from the source element with three periods.&amp;#160; For example, the following code produces the same XML as shown for the child axis example above.&lt;/p&gt;

  &lt;div id="codeSnippetWrapper"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;Dim&lt;/span&gt; childAxis = xml...&amp;lt;course&amp;gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;For&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Each&lt;/span&gt; item &lt;span style="color: #0000ff"&gt;In&lt;/span&gt; childAxis&lt;br /&gt;    Console.WriteLine(item)&lt;br /&gt;&lt;span style="color: #0000ff"&gt;Next&lt;/span&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;You can use indexing as well, for instance, to get only the last course, dessert, via the following line&lt;/p&gt;

  &lt;div id="codeSnippetWrapper"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;Dim&lt;/span&gt; course3 = xml...&amp;lt;course&amp;gt;(2)&lt;/pre&gt;
  &lt;/div&gt;
&lt;/blockquote&gt;

&lt;h3&gt;Attribute axis&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;The attribute axis returns the specified attribute (denoted by the @ symbol) of the element on which it operates.&amp;#160; For instance, the following code will print out the three courses: appetizer, main, and dessert.&lt;/p&gt;

  &lt;div id="codeSnippetWrapper"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;Dim&lt;/span&gt; childAxis = xml...&amp;lt;course&amp;gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;For&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Each&lt;/span&gt; item &lt;span style="color: #0000ff"&gt;In&lt;/span&gt; childAxis&lt;br /&gt;    Console.WriteLine(item.@name)&lt;br /&gt;&lt;span style="color: #0000ff"&gt;Next&lt;/span&gt;&lt;/pre&gt;

    &lt;br /&gt;&lt;/div&gt;
&lt;/blockquote&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;For more information on using XML Literals with Visual Basic, check out the MSDN topic, &lt;a href="http://msdn.microsoft.com/en-us/library/bb384833.aspx"&gt;XML in Visual Basic&lt;/a&gt;.&amp;#160; And for those of you using C#, there’s a neat little add-in that will paste the code necessary to create the XML document that you have stored on the clipboard.&amp;#160; Here’s how:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Extract the &lt;code&gt;LinqSamples\PasteXmlAsLinq&lt;/code&gt; directory from 

    &lt;br /&gt;&lt;code&gt;C:\Program Files\Microsoft Visual Studio 9.0\Samples\1033\CSharpSamples.zip&lt;/code&gt; &lt;/li&gt;

  &lt;li&gt;Build the PasteXmlAsLinq solution &lt;/li&gt;

  &lt;li&gt;Copy the &lt;code&gt;PasteXmlAsLinq.dll&lt;/code&gt; and &lt;code&gt;PasteXmlAsLinq.Addin&lt;/code&gt; files into the Addins folder.&amp;#160; This folder is located at &lt;code&gt;My Documents/Visual Studio/Addins&lt;/code&gt;, but you may need to create it explicitly. &lt;/li&gt;

  &lt;li&gt;Restart Visual Studio. &lt;/li&gt;

  &lt;li&gt;If you have XML in the clipboard (perhaps copied from some other source), you’ll see a Paste XML as XElement menu option in the Edit menu: &lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/XisforXMLLiteral_581/image_2.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px" title="Paste XML as XElement" border="0" alt="Paste XML as XElement" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/XisforXMLLiteral_581/image_thumb.png" width="244" height="215" /&gt;&lt;/a&gt; &lt;/p&gt;

  &lt;p&gt;Copying the XML literal syntax from the first Visual Basic example in this post into the clipboard and then pasting into a C# script using the menu item above yields the following:&lt;/p&gt;

  &lt;div id="codeSnippetWrapper"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;XElement xml = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; XElement(&lt;span style="color: #006080"&gt;&amp;quot;menu&amp;quot;&lt;/span&gt;,&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; XElement(&lt;span style="color: #006080"&gt;&amp;quot;course&amp;quot;&lt;/span&gt;,&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; XAttribute(&lt;span style="color: #006080"&gt;&amp;quot;name&amp;quot;&lt;/span&gt;, &lt;span style="color: #006080"&gt;&amp;quot;appetizer&amp;quot;&lt;/span&gt;),&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; XElement(&lt;span style="color: #006080"&gt;&amp;quot;dish&amp;quot;&lt;/span&gt;, &lt;span style="color: #006080"&gt;&amp;quot;Shrimp Cocktail&amp;quot;&lt;/span&gt;),&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; XElement(&lt;span style="color: #006080"&gt;&amp;quot;dish&amp;quot;&lt;/span&gt;, &lt;span style="color: #006080"&gt;&amp;quot;Escargot&amp;quot;&lt;/span&gt;)&lt;br /&gt;    ),&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; XElement(&lt;span style="color: #006080"&gt;&amp;quot;course&amp;quot;&lt;/span&gt;,&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; XAttribute(&lt;span style="color: #006080"&gt;&amp;quot;name&amp;quot;&lt;/span&gt;, &lt;span style="color: #006080"&gt;&amp;quot;main&amp;quot;&lt;/span&gt;),&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; XElement(&lt;span style="color: #006080"&gt;&amp;quot;dish&amp;quot;&lt;/span&gt;, &lt;span style="color: #006080"&gt;&amp;quot;Filet Mignon&amp;quot;&lt;/span&gt;),&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; XElement(&lt;span style="color: #006080"&gt;&amp;quot;dish&amp;quot;&lt;/span&gt;, &lt;span style="color: #006080"&gt;&amp;quot;Garlic Potatoes&amp;quot;&lt;/span&gt;),&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; XElement(&lt;span style="color: #006080"&gt;&amp;quot;dish&amp;quot;&lt;/span&gt;, &lt;span style="color: #006080"&gt;&amp;quot;Broccoli&amp;quot;&lt;/span&gt;)&lt;br /&gt;    ),&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; XElement(&lt;span style="color: #006080"&gt;&amp;quot;course&amp;quot;&lt;/span&gt;,&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; XAttribute(&lt;span style="color: #006080"&gt;&amp;quot;name&amp;quot;&lt;/span&gt;, &lt;span style="color: #006080"&gt;&amp;quot;dessert&amp;quot;&lt;/span&gt;),&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; XElement(&lt;span style="color: #006080"&gt;&amp;quot;dish&amp;quot;&lt;/span&gt;, &lt;span style="color: #006080"&gt;&amp;quot;Chocolate Cheesecake&amp;quot;&lt;/span&gt;)&lt;br /&gt;    )&lt;br /&gt;);&lt;/pre&gt;

    &lt;br /&gt;&lt;/div&gt;

  &lt;p&gt;It’s not exactly an XML literal, but certainly saves a lot of typing!&lt;/p&gt;&lt;/blockquote&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9752042" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jimoneil/archive/tags/A-to-Z/default.aspx">A-to-Z</category></item><item><title>W is for… WeakReference</title><link>http://blogs.msdn.com/jimoneil/archive/2009/06/08/w-is-for-weakreference.aspx</link><pubDate>Mon, 08 Jun 2009 07:33:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9703521</guid><dc:creator>joneil</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jimoneil/comments/9703521.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jimoneil/commentrss.aspx?PostID=9703521</wfw:commentRss><description>&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/WisforWeakReference_103BD/w_2.gif"&gt;&lt;img style="border-right-width: 0px; margin: 0px 10px 0px 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="W" border="0" alt="W" align="left" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/WisforWeakReference_103BD/w_thumb.gif" width="60" height="66" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;With most of the major development technologies from Microsoft beginning with a “W” – &lt;a href="http://windowsclient.net"&gt;Windows Presentation Foundation&lt;/a&gt;, &lt;a href="http://www.microsoft.com/net/WindowsCommunicationFoundation.aspx"&gt;Windows Communication Foundation&lt;/a&gt;, &lt;a href="http://www.microsoft.com/net/wfdetails.aspx"&gt;Windows Workflow Foundation&lt;/a&gt; – there was no shortage of choices for today’s post.&amp;#160; But somehow those just seemed too easy, and well, a bit large to tackle in one post.&lt;/p&gt;  &lt;p&gt;So, from the depths of the .NET Framework, I settled on a class called &lt;a href="http://msdn.microsoft.com/en-us/library/system.weakreference.aspx"&gt;&lt;code&gt;WeakReference&lt;/code&gt;&lt;/a&gt;.&amp;#160; A &lt;em&gt;weak reference&lt;/em&gt; is essentially a reference to an object that you can access but that is also a candidate for garbage collection.&amp;#160; It’s essentially a shade of grey between the absolutes of having an object in memory that has a &lt;em&gt;strong&lt;/em&gt; reference (e.g., you’ve got a variable referencing the object) and an object that’s been marked for garbage collection but hasn’t yet been reclaimed.&lt;/p&gt;  &lt;p&gt;By creating a weak reference to an object, you’re saying that you’d like to still be able to use it in the future, but it’s not *that* important that it can’t be reclaimed by the garbage collector – in which case, you’ll take the hit for recreating it.&lt;/p&gt;  &lt;p&gt;Let’s take a look an example here. It’s a simplified version of the sample from the &lt;a href="http://msdn.microsoft.com/en-us/library/system.weakreference.aspx"&gt;MSDN reference page&lt;/a&gt;, and dare I say, a bit more interesting, since it forces a garbage collection so that you can actually see the weak reference scenario kick in.&lt;/p&gt;  &lt;p&gt;The context here is a cache, specifically a wrapper for a &lt;code&gt;Dictionary&amp;lt;int, String&amp;gt;&lt;/code&gt;; of course, the &lt;code&gt;String&lt;/code&gt; could be any object here.&amp;#160; The strings are initialized to just the word “Item” followed by their index in the dictionary.&amp;#160; The indexer (line 15) casts the weak reference to a strong one in line 19 to determine whether the object is still available.&amp;#160; If the null test (line 22) fails, we know the object has been garbage collected, and we have to create a new one (line 24). &lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Note, there is an &lt;code&gt;IsAlive&lt;/code&gt; property on &lt;code&gt;WeakReference&lt;/code&gt; which will return true if the object reference has not yet been garbage collected; however, be aware that in the instant between the positive test for &lt;code&gt;IsAlive&lt;/code&gt; and the actual reference of the object, that object could be garbage-collected.&amp;#160; It’s recommended that you do the cast and then check for null as I did below.&lt;/p&gt; &lt;/blockquote&gt;  &lt;div&gt;   &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;     &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Cache&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt; {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt;     Dictionary&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;, WeakReference&amp;gt; Items;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; Count { get { &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; Items.Count;} }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt;&amp;#160; &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum6"&gt;   6:&lt;/span&gt;     String ItemFactory(&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; i) { &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #006080"&gt;&amp;quot;Item &amp;quot;&lt;/span&gt; + i.ToString(); }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum7"&gt;   7:&lt;/span&gt;&amp;#160; &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum8"&gt;   8:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; Cache(&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; count)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum9"&gt;   9:&lt;/span&gt;     {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum10"&gt;  10:&lt;/span&gt;         Items = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Dictionary&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;, WeakReference&amp;gt;(count);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum11"&gt;  11:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;for&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; i = 0; i &amp;lt; count; i++)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum12"&gt;  12:&lt;/span&gt;             Items[i] = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; WeakReference(ItemFactory(i), &lt;span style="color: #0000ff"&gt;false&lt;/span&gt;);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum13"&gt;  13:&lt;/span&gt;     }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum14"&gt;  14:&lt;/span&gt;&amp;#160; &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum15"&gt;  15:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; String &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;[&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; index]&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum16"&gt;  16:&lt;/span&gt;     {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum17"&gt;  17:&lt;/span&gt;         get&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum18"&gt;  18:&lt;/span&gt;         {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum19"&gt;  19:&lt;/span&gt;             String d = (String) Items[index].Target;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum20"&gt;  20:&lt;/span&gt;             Console.WriteLine(&lt;span style="color: #006080"&gt;&amp;quot;Object at {0,2}: {1}&amp;quot;&lt;/span&gt;, index.ToString(), &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum21"&gt;  21:&lt;/span&gt;                 d == &lt;span style="color: #0000ff"&gt;null&lt;/span&gt; ? &lt;span style="color: #006080"&gt;&amp;quot;Regenerated&amp;quot;&lt;/span&gt; : &lt;span style="color: #006080"&gt;&amp;quot;Original&amp;quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum22"&gt;  22:&lt;/span&gt;             &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (d == &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum23"&gt;  23:&lt;/span&gt;             {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum24"&gt;  24:&lt;/span&gt;                 d = ItemFactory(index);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum25"&gt;  25:&lt;/span&gt;                 Items[index] = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; WeakReference(d, &lt;span style="color: #0000ff"&gt;false&lt;/span&gt;);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum26"&gt;  26:&lt;/span&gt;             }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum27"&gt;  27:&lt;/span&gt;             &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; d;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum28"&gt;  28:&lt;/span&gt;         }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum29"&gt;  29:&lt;/span&gt;     }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum30"&gt;  30:&lt;/span&gt; }&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;Now, let’s take a look at a simple console application to show the weak references in action.&amp;#160; I create a Cache object with ten items, and then launch a loop that references items in the cache ‘randomly’ (using the random number generator reference created in line 3).&amp;#160; The twist here is that on line 13, a garbage collection is forced on every fifth reference to the cache.&amp;#160; That will result in all of the current weak references in the cache being garbage collected.&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Main()&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt; {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt;     Random r = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Random();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt;     Cache c = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Cache(10);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt;&amp;#160; &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum6"&gt;   6:&lt;/span&gt;     &lt;span style="color: #008000"&gt;// Randomly access objects in the cache.&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum7"&gt;   7:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;for&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; i = 0; i &amp;lt; 100; i++)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum8"&gt;   8:&lt;/span&gt;     {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum9"&gt;   9:&lt;/span&gt;         Console.Write(&lt;span style="color: #006080"&gt;&amp;quot;{0,2}: &amp;quot;&lt;/span&gt;, i);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum10"&gt;  10:&lt;/span&gt;&amp;#160; &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum11"&gt;  11:&lt;/span&gt;         String s = c[r.Next(c.Count)];&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum12"&gt;  12:&lt;/span&gt;&amp;#160; &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum13"&gt;  13:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; ((i + 1) % 5 == 0) GC.Collect();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum14"&gt;  14:&lt;/span&gt;     }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum15"&gt;  15:&lt;/span&gt;     Console.ReadLine();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum16"&gt;  16:&lt;/span&gt; }&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;So, lets take a look at the output, below.&amp;#160; The first five lines demonstrate accessing the weak references set up in the cache constructor; no garbage collection has yet occurred.&amp;#160; The &lt;img style="border-right-width: 0px; margin: 15px 15px 0px 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="console output" border="0" alt="console output" align="left" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/WisforWeakReference_103BD/image_3.png" width="262" height="192" /&gt;garbage collection occurs at the end of the fifth iteration, and all the weak references in the cache are reclaimed.&amp;#160; As a result, when the 3rd item is referenced at iteration 5, the strongly cast reference to &lt;/p&gt;

&lt;p align="center"&gt;&lt;code&gt;Items[index].Target&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;(line 19 in the first code snippet above) results in null, and a new weak reference is subsequently re-created.&amp;#160; On iteration 8, we get lucky, because the object at index 0 was just recreated in iteration 6.&lt;/p&gt;

&lt;p&gt;One thing this example drives home is that the effectiveness of this approach in terms of implementing a cache pattern depends on the frequency of the garbage collection.&amp;#160; Here, I forced garbage collection for the sake of example, but it could well be the case that the frequency and cost of re-creating objects outweigh the benefits that the cache was to provide.&amp;#160; Jeffrey Richter in &lt;a href="http://www.wintellect.com/BookInformation.aspx?ASIN=0735621632"&gt;CLR via C#&lt;/a&gt; cautions:&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;img style="margin: 0px 10px 0px 0px" title="" border="0" alt="" align="left" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/VBSamplesForEntityFramework_72DE/320px-Left_pointing_double_angle_quotation_mark_sh1_svg_3.png" width="25" height="27" /&gt;The problem with this technique is the following: Garbage collections do not occur when memory is full or close to full.&amp;#160; Instead, garbage collections occur whenever generation 0 is full, which occurs approximately after every 256 KB of memory is allocated.&amp;#160; So objects are being tossed out of memory much more frequently than desired, and your application’s performance suffers greatly.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;… Basically you want your cache to keep strong references to all of your objects and then, when you see that memory is getting tight, you start turning strong references into weak references…some people have had much success by periodically calling the Win32 GlobalMemoryStatusEx function and checking the returned MEMORYSTATUSEX structure’s dwMemoryLoad member.&amp;#160; If this member reports a value above 80, memory is getting tight, and you can start converting strong references to weak references&lt;img style="margin: 5px 0px 0px" title="" border="0" alt="" align="right" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/VBSamplesForEntityFramework_72DE/320px-Right_pointing_double_angle_quotation_mark_sh1_svg_3.png" width="25" height="27" /&gt; based on whether you want a least-recently used algorithm, a most-frequently used algorithm, a time-base algorithm, or whatever.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you look closely at the documentation for &lt;code&gt;WeakReference&lt;/code&gt;, you’ll note there is a property called &lt;code&gt;TrackResurrection&lt;/code&gt; as well as an overloaded constructor to set this value when creating the weak reference.&amp;#160; The default value, which I relied on above, is false, indicating a &lt;em&gt;short&lt;/em&gt; weak reference.&amp;#160; The alternative, a &lt;em&gt;long&lt;/em&gt; weak reference, allows the object to be reclaimed even after &lt;a href="http://www.devx.com/dotnet/Article/33167"&gt;finalization&lt;/a&gt;.&amp;#160; &lt;/p&gt;

&lt;p&gt;If your object has a finalizer (or destructor), the garbage collection process actually has to ‘resurrect’ the object to allow the finalizer to run.&amp;#160; If you have a &lt;em&gt;long&lt;/em&gt; weak reference, you’re allowed to reclaim the object at this stage; with a &lt;em&gt;short&lt;/em&gt; weak reference, you can’t.&amp;#160; Note that if you did reclaim a long reference at this point, the object has been finalized, so you may end up with an object that has an indeterminate state in terms of the unmanaged resources that the finalizer was cleaning up.&amp;#160; To cut to the chase, safe use of long weak references is not for the meek!&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9703521" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jimoneil/archive/tags/A-to-Z/default.aspx">A-to-Z</category></item><item><title>V is for… Velocity</title><link>http://blogs.msdn.com/jimoneil/archive/2009/06/01/v-is-for-velocity.aspx</link><pubDate>Mon, 01 Jun 2009 07:39:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9667207</guid><dc:creator>joneil</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jimoneil/comments/9667207.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jimoneil/commentrss.aspx?PostID=9667207</wfw:commentRss><description>&lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px 15px 5px 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="V" border="0" alt="V" align="left" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/VisforVelocity_850C/v_3.gif" width="61" height="72" /&gt; &lt;/p&gt;  &lt;p&gt;Velocity is the code name (and a cool code name at that) for a highly-scalable, in-memory cache currently in a Community Technology Preview (CTP) stage.&amp;#160; The objective of Velocity is to increase performance by enabling your applications to grab data from the cache versus needing to make expensive calls back to the data source, whether that be a traditional relational database management system or an on-premises or cloud-hosted service.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h1&gt;Velocity Physical and Logical Models&lt;/h1&gt;  &lt;p&gt;The physical configuration of Velocity is as a service (the &lt;em&gt;cache host service&lt;/em&gt;) running on one or more servers, known as &lt;em&gt;cache servers&lt;/em&gt;.&amp;#160; Multiple cache servers are arranged in a ring, the &lt;em&gt;cache cluster&lt;/em&gt;, communicating among each other to handle requests for retrieving, storing, and distributing data.&amp;#160; The cluster configuration itself may be stored in SQL Server or in a file share.&lt;/p&gt;  &lt;p&gt;To the consumer of the cache, the cache cluster is the single entry point, and both the number of servers involved and the actual distribution of data among those servers is immaterial.&amp;#160; The image below shows the relationship of the client to the cache cluster and servers; note there are also a PowerShell administration tool and cmdlets provided to configure the cluster.&lt;/p&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="Velocity architecture" border="0" alt="Velocity architecture" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/VisforVelocity_850C/image_3.png" width="527" height="318" /&gt; &lt;/p&gt;  &lt;p&gt;The logical model of a cache cluster comprises three parts&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;named cache &lt;/li&gt;    &lt;li&gt;region &lt;/li&gt;    &lt;li&gt;cache item &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;A &lt;em&gt;named cache&lt;/em&gt; is a logical grouping of data, somewhat like a database, and is defined by a name, expiration and eviction policy (defining when items expire and how are they removed from the cache).&amp;#160; You can also enable high-availability, which automatically engages secondary copies of your objects.&amp;#160; The secondary objects are kept in sync (strong consistency) and can assume a primary role if the cache host on which the primary object is located fails.&lt;/p&gt;  &lt;p&gt;A &lt;em&gt;region&lt;/em&gt; is a logical subset of a named cache, something like a table within a database.&amp;#160; Regions, which are optional, enable richer retrieval functionality by allowing data to be &lt;em&gt;tagged&lt;/em&gt; with one or more descriptive strings (in addition to the unique key for that data).&amp;#160; Regions, however, are stored on a single node in the cluster and not load-balanced, so there is a tradeoff between functionality and scalability.&lt;/p&gt;  &lt;p&gt;A &lt;em&gt;cache item&lt;/em&gt; is any serializable descendant of &lt;code&gt;System.Object&lt;/code&gt;.&amp;#160; Associated with each cache item is a key and other metadata, including version, creation timestamp, time to live (TTL), etc.&lt;/p&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="Logical architecture" border="0" alt="Logical architecture" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/VisforVelocity_850C/image_11.png" width="515" height="373" /&gt; &lt;/p&gt;  &lt;h1&gt;&amp;#160;&lt;/h1&gt;  &lt;h1&gt;Data Taxonomy&lt;/h1&gt;  &lt;p&gt;When considering caching technology, it’s important to understand the nature of the data that you are caching.&amp;#160; One possible classification is three bins&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Reference data &lt;/li&gt;    &lt;li&gt;Activity data &lt;/li&gt;    &lt;li&gt;Resource data &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;em&gt;Reference data&lt;/em&gt; are things like tax tables or a product catalog.&amp;#160; The data is shared among multiple clients, but it’s essentially immutable, and when it does change the change affects everyone – an updated product list, for instance.&amp;#160; Such data lends itself quite well to partitioning, and in Velocity, you can use local cache as well (cache managed within the application process) to get even better performance for frequently accessed items.&lt;/p&gt;  &lt;p&gt;&lt;em&gt;Activity data&lt;/em&gt; are items that result from the processes that your application initiates.&amp;#160; If you’re running an e-commerce site, for instance, the activity data would be things like a shopping cart or customer order.&amp;#160; The data here is mutable, but it’s only accessible by a single user.&amp;#160; The single access point still allows for partitioning of the data; however, here high-availability is generally required since you don’t want to risk losing information about an activity while that activity is in progress.&lt;/p&gt;  &lt;p&gt;&lt;em&gt;Resource data&lt;/em&gt; is data that is shared in both a read and write fashion by multiple clients.&amp;#160; Inventory data is a classic example – you’re booking a flight via an on-line service, for instance.&amp;#160; As you view the flight options, someone else may be booking the last seat on that flight, so in these scenarios options for resolving concurrency issues are paramount.&amp;#160; Velocity does support both optimistic concurrency (via version metadata) and pessimistic concurrency (via a locking mechanism).&lt;/p&gt;  &lt;p&gt;The nature of the data to be cached will lead you toward specific configuration options for your caches.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h1&gt;Client API&lt;/h1&gt;  &lt;p&gt;Using Velocity within your own applications is fairly straightforward.&amp;#160; There are four assemblies that contain the various classes required by the &lt;code&gt;Microsoft.Data.Caching&lt;/code&gt; namespace:&lt;/p&gt; &lt;code&gt;   &lt;ul&gt;     &lt;li&gt;CASBase.dll &lt;/li&gt;      &lt;li&gt;FabricCommon.dll &lt;/li&gt;      &lt;li&gt;CacheBaseLibrary.dll &lt;/li&gt;      &lt;li&gt;ClientLibrary.dll &lt;/li&gt;   &lt;/ul&gt; &lt;/code&gt;  &lt;p&gt;When building client applications, you’ll need to add references to the last two of these assemblies within your Visual Studio project.&amp;#160; You also need to add some configuration information to the client config file (or programmatically) to specify the location of a cache host (any one in the cluster will do) as well as whether you want the client to act as a simple client or a routing client.&amp;#160; &lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;A &lt;em&gt;routing client&lt;/em&gt; maintains a lookup table mapping data items to the specific cache host where the object is physically located, so the data is directly retrieved from that server.&lt;/p&gt;    &lt;p&gt;A &lt;em&gt;simple client&lt;/em&gt; connects to only a single cache host and makes all requests through that host.&amp;#160; As a result, the host needs to make additional requests to get to the actual cache host physically storing the data.&amp;#160; Simple clients should be use only if there are topology issues such that a client does not have visibility to all the cache hosts in the cluster – generally a rare scenario.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Within the client configuration file, you can also indicate whether a local cache should be maintained.&amp;#160; The local cache is stored in the address space of the client application and should be used only to store rarely (if ever) changed data - to reduce the occurrence of stale data.&lt;/p&gt;  &lt;p&gt;Within your client code, the access pattern looks something like this:&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="code sample" border="0" alt="code sample" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/VisforVelocity_850C/image_8.png" width="475" height="315" /&gt; &lt;/p&gt;  &lt;p&gt;The code above does not use regions; however, there are overloads of &lt;code&gt;Get&lt;/code&gt; and &lt;code&gt;Put&lt;/code&gt; that address regions as well.&amp;#160; There are also ‘locking’ versions of &lt;code&gt;Get&lt;/code&gt; and &lt;code&gt;Put&lt;/code&gt; to provide for pessimistic concurrency, where the object is locked from access by other clients. &lt;/p&gt;  &lt;p&gt;Also available are callback actions to which a client can subscribe to be informed of additions, modifications, and deletions of items and regions as well as logging capabilities with levels of error, warning, information and verbose.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h1&gt;References&lt;/h1&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/data/cc655792.aspx"&gt;MSDN Data Platform Developer Center&lt;/a&gt;&lt;/p&gt;    &lt;p&gt;&lt;a href="http://blogs.msdn.com/velocity/"&gt;Velocity Team Blog&lt;/a&gt;&lt;/p&gt;    &lt;p&gt;&lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=B24C3708-EEFF-4055-A867-19B5851E7CD2&amp;amp;displaylang=en"&gt;Community Technology Preview (CTP) 3 download&lt;/a&gt;&lt;/p&gt;    &lt;p&gt;&lt;a href="http://code.msdn.microsoft.com/velocity"&gt;Sample Code&lt;/a&gt; (CTP 2 – needs modification to work with CTP 3)&lt;/p&gt;&lt;/blockquote&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9667207" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jimoneil/archive/tags/A-to-Z/default.aspx">A-to-Z</category></item><item><title>U is for… unchecked</title><link>http://blogs.msdn.com/jimoneil/archive/2009/05/25/u-is-for-unchecked.aspx</link><pubDate>Mon, 25 May 2009 07:01:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9637943</guid><dc:creator>joneil</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jimoneil/comments/9637943.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jimoneil/commentrss.aspx?PostID=9637943</wfw:commentRss><description>&lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px 10px 0px 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="u" border="0" alt="u" align="left" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/Uisforunchecked_BA7D/u_3.gif" width="56" height="66" /&gt; &lt;/p&gt;  &lt;p&gt;Welcome to the Memorial Day edition of the A-Z series!&amp;#160; As I flipped through the index of &lt;a href="http://apress.com/book/view/1590598849"&gt;Pro C# 2008 and the .NET 3.5 Platform&lt;/a&gt;, I happened upon the &lt;a href="http://msdn.microsoft.com/en-us/library/a569z7k8.aspx"&gt;&lt;code&gt;unchecked&lt;/code&gt;&lt;/a&gt; keyword - one of those things I remember seeing once or twice, but couldn’t really recall what it did or how it worked.&amp;#160; As&amp;#160; you might expect, there’s a &lt;a href="http://msdn.microsoft.com/en-us/library/74b4xzyw.aspx"&gt;&lt;code&gt;checked&lt;/code&gt;&lt;/a&gt; keyword too.&lt;/p&gt;  &lt;p&gt;&lt;code&gt;unchecked&lt;/code&gt; and its positive counterpart come into play when dealing with integer arithmetic and overflow conditions.&amp;#160; Recall that the range for an &lt;code&gt;Int16&lt;/code&gt; variable is ‑32,768 to 32,767, so what do you suppose happens with the following code?&lt;/p&gt;  &lt;pre class="csharpcode"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="" border="0" alt="" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/Uisforunchecked_BA7D/image_3.png" width="415" height="76" /&gt; &lt;/pre&gt;

&lt;p&gt;Here’s the result, courtesy of &lt;a href="http://en.wikipedia.org/wiki/Two's_complement"&gt;2’s complement&lt;/a&gt;; essentially, we’ve ‘wrapped around’ and are now in negative number territory.&amp;#160; There were no errors, reported though, and that’s because this is an unchecked operation by default.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="" border="0" alt="" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/Uisforunchecked_BA7D/image7.png" width="154" height="152" /&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;The compiler is smart enough to catch a lot of cases of potential conversion issues, like the following:&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="" border="0" alt="" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/Uisforunchecked_BA7D/image_43.png" width="529" height="123" /&gt; &lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Subtle difference, huh?&amp;#160; It’s essentially the same statement; however, the type promotion &lt;a href="http://msdn.microsoft.com/en-us/library/aa691330(VS.71).aspx"&gt;rules for the binary operator +&lt;/a&gt; differ just enough from the &lt;a href="http://msdn.microsoft.com/en-us/library/aa691316(VS.71).aspx"&gt;rules for the compound assigment +=&lt;/a&gt; to result in this behavior. The good news here is that it makes it harder to unwittingly introduce these types of errors: most of the time the compiler will warn you that an explicit cast is required, even if you try to do something to ‘trick’ it, like this:&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px auto; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="" border="0" alt="" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/Uisforunchecked_BA7D/image_13.png" width="518" height="257" /&gt;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;You do have the option though to use an explicit cast (and take responsibility for the consequences), and so the following code results in the same erroneous output that I started the post off with:&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="" border="0" alt="" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/Uisforunchecked_BA7D/image_46.png" width="508" height="279" /&gt; &lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;If you’re really concerned about these types of errors impacting your application, you can use the &lt;code&gt;checked&lt;/code&gt; keyword/method, in which case an &lt;a href="http://msdn.microsoft.com/en-us/library/system.overflowexception.aspx"&gt;OverflowException&lt;/a&gt; is thrown if there is numeric overflow (which, of course, you’d then be catching to handle a bit more gracefully than this):&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="" border="0" alt="" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/Uisforunchecked_BA7D/image_28.png" width="530" height="214" /&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;By the way, another way to write the checked logic is as follows:&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="" border="0" alt="" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/Uisforunchecked_BA7D/image_22.png" width="366" height="32" /&gt;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;It should be clear by now that by default your code is executing in an unchecked mode.&amp;#160; That may seem more dangerous, but given the warnings you get about casting when writing your code, there shouldn’t be too much of an opportunity for surprises.&amp;#160; It’s also expensive to make these checks at runtime, so rather than make everyone pay a performance penalty, the default is to let the overflow go undetected at runtime.&lt;/p&gt;

&lt;p&gt;But what if you’d still prefer to err on the side of caution, or perhaps you’re working with less-than-robust 3rd party code that you can’t fix at the moment?&amp;#160; If you click the Advanced button on the Build tab of your project, you can turn on the automatic detection of overflow and underflow:&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="" border="0" alt="" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/Uisforunchecked_BA7D/image_31.png" width="531" height="396" /&gt; &lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;… and now the original example throws an exception too.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="" border="0" alt="" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/Uisforunchecked_BA7D/image_34.png" width="536" height="190" /&gt; &lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;We’re now erring on the side of caution, but what if there’s now code that we know will cause an overflow/underflow, but we don’t care?&amp;#160;&amp;#160; Now that I’ve turned it on everywhere, how do I turn it off?&amp;#160;&amp;#160; As you might expect, that’s where &lt;code&gt;unchecked&lt;/code&gt; comes in.&amp;#160; To let my original code succeed in spite of the Build property being changed, I can change my code to the following, and it ‘works’ again.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="" border="0" alt="" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/Uisforunchecked_BA7D/image_37.png" width="527" height="213" /&gt; &lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;It turns out you can nest the checked and unchecked sections too, which promises to add up to a maintenance nightmare, but it’s nice to know you can do it.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9637943" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jimoneil/archive/tags/A-to-Z/default.aspx">A-to-Z</category></item><item><title>T is for… Tracepoint</title><link>http://blogs.msdn.com/jimoneil/archive/2009/05/18/t-is-for-tracepoint.aspx</link><pubDate>Mon, 18 May 2009 07:01:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9624470</guid><dc:creator>joneil</dc:creator><slash:comments>6</slash:comments><comments>http://blogs.msdn.com/jimoneil/comments/9624470.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jimoneil/commentrss.aspx?PostID=9624470</wfw:commentRss><description>&lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px 15px 0px 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="t" border="0" alt="t" align="left" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/TisforTracepoint_8D2E/t_3.gif" width="51" height="66" /&gt; &lt;/p&gt;  &lt;p&gt;I’ve presented various sessions on &lt;a href="http://code.msdn.microsoft.com/northeast/Release/ProjectReleases.aspx?ReleaseId=2503" target="_blank"&gt;Debugging Tips and Tricks&lt;/a&gt; as part of the &lt;a href="http://blogs.msdn.com/jimoneil/archive/2009/05/02/2009-summer-northeast-roadshow.aspx" target="_blank"&gt;Northeast Roadshow&lt;/a&gt; and MSDN Events series, and one of the undiscovered gems in that presentation is that of tracepoints.&amp;#160; Tracepoints have actually been around since Visual Studio 2005, but weren’t all that discoverable until Visual Studio 2008.&lt;/p&gt;  &lt;p&gt;What exactly is a tracepoint?&amp;#160; It’s essentially a breakpoint that doesn’t halt the execution of your application in the debugger.&amp;#160; Instead, it provides the opportunity to write information to the output window, with much the same effect that &lt;a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.trace.writeline.aspx" target="_blank"&gt;&lt;code&gt;Trace.WriteLine&lt;/code&gt;&lt;/a&gt; has, but without requiring code to do it.&amp;#160; You can also use tracepoints to run macros for more advanced debugging scenarios.&lt;/p&gt;  &lt;p&gt;To get an idea of how to employ tracepoints, here’s a YACE (yet-another-contrived-example) that builds a list of the prime numbers between two input numbers.&amp;#160; There are &lt;a href="http://mathworld.wolfram.com/PrimeFactorizationAlgorithms.html" target="_blank"&gt;far better algorithms&lt;/a&gt; than this for performing this task, so don’t focus too much on the logic, lack of bounds checking, coding style, performance issues, etc.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;div class="csharpcode"&gt;   &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;List&amp;lt;Int32&amp;gt; primeList = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;Int32&amp;gt;();&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;Boolean isPrime;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;&lt;span class="kwrd"&gt;if&lt;/span&gt; ((startVal &amp;lt;= 2) &amp;amp;&amp;amp; (endVal &amp;gt;= 2)) primeList.Add(2);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;&lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; candidate = Math.Max(3, startVal); candidate &amp;lt;= endVal; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;         candidate++)&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;{&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;    isPrime = &lt;span class="kwrd"&gt;true&lt;/span&gt;;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;    &lt;span class="kwrd"&gt;int&lt;/span&gt; maxTest = (&lt;span class="kwrd"&gt;int&lt;/span&gt;) Math.Sqrt(candidate) + 1;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;    &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; testVal = 2; (testVal &amp;lt; maxTest) &amp;amp;&amp;amp; isPrime; testVal++)&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;        &lt;span class="kwrd"&gt;if&lt;/span&gt; (candidate % testVal == 0)&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;            isPrime = &lt;span class="kwrd"&gt;false&lt;/span&gt;;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;    &lt;span class="kwrd"&gt;if&lt;/span&gt; (isPrime) primeList.Add(candidate);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;

























.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;style type="text/css"&gt;




























.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;If I employ this logic in a simple console application, and set &lt;code&gt;startVal&lt;/code&gt; to say 700 and &lt;code&gt;endVal&lt;/code&gt; to 800, I’ll get output such as the following:&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; margin-left: 0px; border-left-width: 0px; margin-right: 0px" title="Prime console output" border="0" alt="Prime console output" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/TisforTracepoint_8D2E/image_3.png" width="530" height="307" /&gt;&lt;/p&gt;

&lt;p&gt;Now, lets say I’m looking at the output and really wondering why 703 and 713 didn’t make the list.&amp;#160; They “look” prime – at least they don’t pass those handful of quick tests we learned in junior high – so I’m curious as to what factor kicked them out of the list.&amp;#160; It’s the code at lines 13 and 14 where a divisor is detected for a candidate prime number, and that divisor is &lt;code&gt;testVal&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I could put some code in there using &lt;code&gt;System.Diagnostics.Trace&lt;/code&gt; to output &lt;code&gt;testVal&lt;/code&gt; at that point, but it’s going to require me to introduce more code, as well as some logic, perhaps, to print out only when it’s dealing with the values 703 and 713.&amp;#160; And, maybe it’s not even my code to be tinkering with.&lt;/p&gt;

&lt;p&gt;One option here is to introduce a tracepoint on line 14 to capture just the information you want but without causing execution to stop as a breakpoint would.&amp;#160; You can insert a tracepoint by right-clicking on a line of code and selecting the option from the context menu as you see here:&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Tracepoint context menu" border="0" alt="Tracepoint context menu" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/TisforTracepoint_8D2E/image_6.png" width="535" height="303" /&gt; &lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Alternatively, you can click on the gutter to add a breakpoint, and then use the When hit… menu item to convert it from a breakpoint into a tracepoint (note the diamond shape versus the circle in the gutter):&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="When hit..." border="0" alt="When hit..." src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/TisforTracepoint_8D2E/image_11.png" width="411" height="193" /&gt; &lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Both of these mechanisms bring up a dialog with two main options&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Print a message, or &lt;/li&gt;

  &lt;li&gt;Run a macro &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Regardless of which option you choose, you can elect to continue execution or break (as a normal breakpoint would).&amp;#160; By default, when you select either the Print a message or Run a macro option, the Continue execution box will be checked.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h1&gt;Print a message&lt;/h1&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="When Breakpoint Is Hit... dialog" border="0" alt="When Breakpoint Is Hit... dialog" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/TisforTracepoint_8D2E/image_14.png" width="490" height="411" /&gt; &lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;When you choose to Print a message, the verbiage no longer has the disabled look, and you can use the textbox to indicate what you want to display in the output window.&amp;#160; The Function and Thread information are there as a default and demonstrate that you have access to a number of debugging environment values (which are described on the dialog).&amp;#160; You can also include your own variables and expressions enclosed in curly braces.&amp;#160; For our scenario here, I’ve set up the tracepoint as follows:&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="Tracepoint settings" border="0" alt="Tracepoint settings" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/TisforTracepoint_8D2E/image_17.png" width="490" height="410" /&gt; &lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;When I execute the application now, it runs to completion, but within the Output window, I get the information I was looking for, and I can see that 703 is divisible by 19 and 713 by 23.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="Output window" border="0" alt="Output window" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/TisforTracepoint_8D2E/image_37.png" width="438" height="263" /&gt; &lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Keep in mind it’s still a breakpoint too, so the other options on a breakpoint apply to activating the tracepoint as well.&amp;#160; For instance, if I really want the trace information written for only 703 and 713, then I can set up a breakpoint condition such as the following:&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="Breakpoint Condition dialog" border="0" alt="Breakpoint Condition dialog" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/TisforTracepoint_8D2E/image_22.png" width="425" height="202" /&gt; &lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px 0px 0px 15px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Output window" border="0" alt="Output window" align="right" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/TisforTracepoint_8D2E/image_25.png" width="312" height="163" /&gt;The output will show as on the right, and execution will continue.&amp;#160; In such a case, the tracepoint glyph will include a white cross &lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="Tracepoint glyph" border="0" alt="Tracepoint glyph" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/TisforTracepoint_8D2E/image_28.png" width="32" height="31" /&gt;&lt;/p&gt;

&lt;p&gt;indicating that there are advanced options set.&amp;#160;&amp;#160; &lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h1&gt;Run a macro&lt;/h1&gt;

&lt;p&gt;The Run a macro option gives you even more power for handling a tracepoint, but presumes you’re willing to work for it by writing a bit of Visual Basic for Applications script to implement a macro.&amp;#160; The dropdown list already provides a number of options; these are macros that come with Visual Studio, and the ones in the &lt;code&gt;Macros.Samples.VSDebugger&lt;/code&gt; namespace are the most germane here.&amp;#160; &lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Available macros" border="0" alt="Available macros" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/TisforTracepoint_8D2E/pic_3.gif" width="515" height="575" /&gt; &lt;/p&gt;

&lt;p&gt;Now, the &lt;code&gt;ShowCurrentProcess&lt;/code&gt; macro here isn’t all that interesting, but when engaged will display the path to the current process in the output window.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="ShowCurrentProcess output" border="0" alt="ShowCurrentProcess output" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/TisforTracepoint_8D2E/image_33.png" width="515" height="88" /&gt;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;You can examine the implementation of this macro and the other others listed in the dropdown list by opening the Macros IDE (Alt+F11).&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Macros IDE" border="0" alt="Macros IDE" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/TisforTracepoint_8D2E/image_36.png" width="520" height="362" /&gt; &lt;/p&gt;

&lt;p&gt;A closer look at the implementation below reveals the use of the &lt;a href="http://msdn.microsoft.com/en-us/library/envdte.aspx" target="_blank"&gt;&lt;code&gt;EnvDTE&lt;/code&gt; namespace&lt;/a&gt; and the &lt;code&gt;OutputWindowPane&lt;/code&gt;, &lt;code&gt;Process&lt;/code&gt;, and &lt;code&gt;DTE&lt;/code&gt; objects within that namespace.&amp;#160; &lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="rem"&gt;' This function displays the current debugger mode in the output window.&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; ShowCurrentProcess()&lt;/pre&gt;

  &lt;pre class="alt"&gt;     &lt;span class="kwrd"&gt;Dim&lt;/span&gt; outputWinPane &lt;span class="kwrd"&gt;As&lt;/span&gt; EnvDTE.OutputWindowPane&lt;/pre&gt;

  &lt;pre&gt;     &lt;span class="kwrd"&gt;Dim&lt;/span&gt; proc &lt;span class="kwrd"&gt;As&lt;/span&gt; EnvDTE.Process&lt;/pre&gt;

  &lt;pre class="alt"&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre&gt;     outputWinPane = Utilities.GetOutputWindowPane(&lt;span class="str"&gt;&amp;quot;Debugger&amp;quot;&lt;/span&gt;)&lt;/pre&gt;

  &lt;pre class="alt"&gt;     proc = DTE.Debugger.CurrentProcess&lt;/pre&gt;

  &lt;pre&gt;     &lt;span class="kwrd"&gt;If&lt;/span&gt; (proc &lt;span class="kwrd"&gt;Is&lt;/span&gt; &lt;span class="kwrd"&gt;Nothing&lt;/span&gt;) &lt;span class="kwrd"&gt;Then&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;         outputWinPane.OutputString(&lt;span class="str"&gt;&amp;quot;No process is being debugged&amp;quot;&lt;/span&gt;)     &lt;/pre&gt;

  &lt;pre&gt;     &lt;span class="kwrd"&gt;Else&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;         outputWinPane.OutputString(&lt;span class="str"&gt;&amp;quot;&amp;quot;&lt;/span&gt; + Str(proc.ProcessID) + &lt;span class="str"&gt;&amp;quot;: &amp;quot;&lt;/span&gt; &lt;/pre&gt;

  &lt;pre&gt;             + proc.Name + vbCrLf)&lt;/pre&gt;

  &lt;pre class="alt"&gt;     &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;p&gt; &lt;span class="kwrd"&gt;End&lt;/span&gt; Sub&lt;/p&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/envdte._dte_members.aspx" target="_blank"&gt;&lt;code&gt;DTE&lt;/code&gt;&lt;/a&gt; is the top most object in the Visual Studio automation model and provides access to the debugger as well as the IDE including toolbars, commands, the active document, and more.&amp;#160; With programmatic control of these objects, you can create more advanced breakpoints and tracepoints, like, for instance, one that would analyze the stack trace and break only if method &lt;code&gt;bar&lt;/code&gt; was called by method &lt;code&gt;foo&lt;/code&gt;.&amp;#160; &lt;/p&gt;

&lt;p&gt;Check out the &lt;a href="http://msdn.microsoft.com/en-us/library/1xt0ezx9.aspx" target="_blank"&gt;MSDN documentation on Visual Studio Extensibility&lt;/a&gt; for more information on building your own macros.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9624470" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jimoneil/archive/tags/A-to-Z/default.aspx">A-to-Z</category></item><item><title>S is for… sqlmetal</title><link>http://blogs.msdn.com/jimoneil/archive/2009/05/11/s-is-for-sqlmetal.aspx</link><pubDate>Mon, 11 May 2009 07:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9598324</guid><dc:creator>joneil</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jimoneil/comments/9598324.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jimoneil/commentrss.aspx?PostID=9598324</wfw:commentRss><description>&lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px 10px 0px 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="S" border="0" alt="S" align="left" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/Sisforsqlmetal_134D6/s_3.gif" width="57" height="66" /&gt; &lt;/p&gt;  &lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/bb386987.aspx"&gt;sqlmetal&lt;/a&gt; is one of the rare cases where a Microsoft utility or application ends up seeing the light of day with a cool name!&amp;#160; If you’re working with LINQ to SQL you may know what sqlmetal is, but since much of what it does is part of the Visual Studio designer experience there’s a fair chance you’ve been doing LINQ to SQL work and had no idea it existed.&lt;/p&gt;  &lt;p&gt;sqlmetal is a command-line utility that you can use in lieu of the built-in Visual Studio designer to create a dbml (database markup language) file that defines the entities and &lt;a href="http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.aspx"&gt;DataContext&lt;/a&gt; for LINQ to SQL models.&amp;#160; The utility is installed with the SDK that accompanies Visual Studio; on my machine I found it in &lt;/p&gt;  &lt;p align="center"&gt;&lt;code&gt;C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin&lt;/code&gt;&lt;/p&gt;  &lt;p&gt;For sake of examples in the post, I’ll use the timeless Northwind database as the targeted database.&amp;#160; &lt;/p&gt;  &lt;h2&gt;LINQ To SQL within Visual Studio&lt;/h2&gt;  &lt;p&gt;Within the Visual Studio environment, creating a LINQ to SQL model is pretty straightforward:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;You add a new item in the Data category &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="Add New Item dialog" border="0" alt="Add New Item dialog" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/Sisforsqlmetal_134D6/image_3.png" width="368" height="201" /&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;and up comes the visual designer where you can drag and drop tables and procedures to the surface &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="LINQ to SQL designer" border="0" alt="LINQ to SQL designer" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/Sisforsqlmetal_134D6/image_6.png" width="303" height="249" /&gt; &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;save, and you get a couple of files created for you &lt;/li&gt; &lt;/ul&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/Sisforsqlmetal_134D6/image_8.png"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="Generated files" border="0" alt="Generated files" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/Sisforsqlmetal_134D6/image_thumb_2.png" width="215" height="58" /&gt;&lt;/a&gt;&lt;/p&gt;    &lt;ul&gt;     &lt;li&gt;the &lt;code&gt;dbml&lt;/code&gt; file, a file that captures the schema from the database in XML format, &lt;/li&gt;      &lt;li&gt;the &lt;code&gt;layout&lt;/code&gt; file, an XML file used by the visual designer, and &lt;/li&gt;      &lt;li&gt;the &lt;code&gt;designer.cs&lt;/code&gt; file, which contains generated classes for the tables (entities) in the database.         &lt;br /&gt;&lt;/li&gt;   &lt;/ul&gt; &lt;/blockquote&gt;  &lt;h2&gt;LINQ to SQL with sqlmetal&lt;/h2&gt;  &lt;p&gt;With sqlmetal you can accomplish essentially the same task in two steps&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;1.&amp;#160; Generate the dbml file, and &lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; margin-left: 0px; border-left-width: 0px; margin-right: 0px" title="SQLMetal command line" border="0" alt="SQLMetal command line" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/Sisforsqlmetal_134D6/image_14.png" width="440" height="71" /&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;2.&amp;#160; use the dbml file to generate the classes&lt;/p&gt;    &lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/Sisforsqlmetal_134D6/image_17.png" width="436" height="73" /&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;There isn’t a layout file in this case, of course, since we’re doing everything outside of the Visual Studio IDE.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Output Comparison&lt;/h2&gt;  &lt;p&gt;I thought I might try to compare the files generated from within Visual Studio to those output by sqlmetal, expecting them to be nearly identical, but I stumbled upon some differences:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;The designer default is to use smart pluralization, so from the &lt;code&gt;Categories&lt;/code&gt; table, for instance, I’d get a class named &lt;code&gt;Category&lt;/code&gt;.&amp;#160; In sqlmetal, the default is to not do this, but you can use the &lt;code&gt;/pluralize&lt;/code&gt; switch to engage that feature. &lt;/li&gt;    &lt;li&gt;Associations between tables generated by sqlmetal have a naming convention of &lt;code&gt;FK_&lt;em&gt;foreignkeytable&lt;/em&gt;_&lt;em&gt;primarykeytable&lt;/em&gt;&lt;/code&gt;, whereas the designer produces the name &lt;code&gt;&lt;em&gt;primarykeyentity&lt;/em&gt;_&lt;em&gt;foreignkeyentity&lt;/em&gt; &lt;/code&gt;&lt;/li&gt;    &lt;li&gt;sqlmetal adds a &lt;code&gt;DeleteRule=&amp;quot;NO ACTION&amp;quot;&lt;/code&gt; to primary key associations, but since that’s the default, the lack of the attribute in the designer-generated version is inconsequential. &lt;/li&gt;    &lt;li&gt;Similarly, the sqlmetal generated code file explicitly includes &lt;code&gt;CanBeNull=&amp;quot;true&amp;quot;&lt;/code&gt; for some columns (I saw it specifically on the Image type), but true is the default for that attribute as well, so leaving it off is not an issue. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Which Method to Choose?&lt;/h2&gt;  &lt;p&gt;While I was a little surprised by the discrepancies in the files, none of them really affect the functionality.&amp;#160; So, given that, why bother with the effort of bringing up the command-line, executing two commands, and importing files into your project, when you can get the same result right within the Visual Studio IDE?&amp;#160; Here’s a couple of reasons to consider.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;1.&amp;#160; Performance&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;With large databases, the Visual Studio designer can be a bit slow.&amp;#160; After all, it’s handling not only the dbml and code file generation, but also the visual layout of the classes and relationships.&amp;#160; The point at which this becomes noticeable, as many other such variables, &lt;em&gt;depends&lt;/em&gt;.&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;2.&amp;#160; Scriptability&lt;/p&gt;    &lt;p&gt;If you’re using msbuild scripts, for instance, then command line operations are right up your alley.&amp;#160; If your schema is in a state of flux, using sqlmetal within a batch script is a reasonable method to refresh the dbml and code files.&amp;#160; Within the Visual Studio IDE there isn’t a 'synchronize to database’ option, so if your schema changes, you have no recourse but to recreate the model from scratch in Visual Studio (or hack into the IDE-generated files).&amp;#160; &lt;/p&gt;    &lt;p&gt;Of course, if your schema is too volatile you’ll have issues in that developers may have written code addressing various entities and properties (tables and columns) that no longer compiles if you’ve renamed them, changed types, etc.&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;3.&amp;#160; Flexibility&lt;/p&gt;    &lt;p&gt;One of the criticisms of the default LINQ to SQL generation is the heavy use of attributes on the generated classes.&amp;#160; Each entity has an attribute relating it to the table schema, and each property has an attribute relating it to the associated column’s properties. For instance, here’s a snippet of code for the &lt;code&gt;TerritoryID&lt;/code&gt; property.&lt;/p&gt;    &lt;pre class="csharpcode"&gt;[Column(Storage=&lt;span class="str"&gt;&amp;quot;_TerritoryID&amp;quot;&lt;/span&gt;, 
   DbType=&lt;span class="str"&gt;&amp;quot;NVarChar(20) NOT NULL&amp;quot;&lt;/span&gt;, 
   CanBeNull=&lt;span class="kwrd"&gt;false&lt;/span&gt;, IsPrimaryKey=&lt;span class="kwrd"&gt;true&lt;/span&gt;)]
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; TerritoryID
{
    get
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;this&lt;/span&gt;._TerritoryID;
    }
    set
    {
        &lt;span class="kwrd"&gt;if&lt;/span&gt; ((&lt;span class="kwrd"&gt;this&lt;/span&gt;._TerritoryID != &lt;span class="kwrd"&gt;value&lt;/span&gt;))
        {
            &lt;span class="kwrd"&gt;this&lt;/span&gt;.OnTerritoryIDChanging(&lt;span class="kwrd"&gt;value&lt;/span&gt;);
            &lt;span class="kwrd"&gt;this&lt;/span&gt;.SendPropertyChanging();
            &lt;span class="kwrd"&gt;this&lt;/span&gt;._TerritoryID = &lt;span class="kwrd"&gt;value&lt;/span&gt;;
            &lt;span class="kwrd"&gt;this&lt;/span&gt;.SendPropertyChanged(&lt;span class="str"&gt;&amp;quot;TerritoryID&amp;quot;&lt;/span&gt;);
            &lt;span class="kwrd"&gt;this&lt;/span&gt;.OnTerritoryIDChanged();
        }
    }
}&lt;/pre&gt;
  &lt;style type="text/css"&gt;






















.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;Many developers are strong &lt;a href="http://www.sidarok.com/web/blog/content/2008/10/14/achieving-poco-s-in-linq-to-sql.html"&gt;proponents of POCO&lt;/a&gt; (Plain-old CLR classes) and would prefer to use classes that are not marked by the various attributes tying the object to the underlying data store.&lt;/p&gt;

  &lt;p&gt;With sqlmetal, you have the option to generate an external XML mapping and nearly-POCO classes for your entities (below). By the way, I say &lt;em&gt;nearly-&lt;/em&gt;POCO based on the fact that the classes generated still incorporate &lt;code&gt;EntityRef&lt;/code&gt; and &lt;code&gt;EntityState&lt;/code&gt; classes, thus pulling in the LINQ infrastructure.&amp;#160; Classes and properties are, however, unadorned by attributes.&lt;/p&gt;

  &lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="SQLMetal generation" border="0" alt="SQLMetal generation" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/Sisforsqlmetal_134D6/image_20.png" width="448" height="69" /&gt; &lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;The resulting map XML file looks quite a bit like the dbml file, however, it includes a &lt;code&gt;member&lt;/code&gt; and &lt;code&gt;storage&lt;/code&gt; attribute to tie the abstracted model back to specific properties and storage fields in the POCO class, a class you might even have coded from scratch yourself.&amp;#160; &lt;/p&gt;

  &lt;p&gt;In fact, this mapping file can help insulate you from backend changes in the database as well.&amp;#160; For example, if the name of the field in the database changes, you need only update the mapping file.&amp;#160; No modification of class files is needed – something impossible to do with the default attribute-based approach.&lt;/p&gt;

  &lt;p&gt;Instantiating your LINQ to SQL &lt;code&gt;DataContext&lt;/code&gt; to incorporate a mapping file merely involves using an overloaded constructor that accepts a &lt;code&gt;MappingSource&lt;/code&gt;, like this:&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;pre class="csharpcode"&gt;String connString = ConfigurationSettings.
     AppSettings[&lt;span class="str"&gt;&amp;quot;NorthwindConnectionString&amp;quot;&lt;/span&gt;];
XmlMappingSource map = XmlMappingSource.FromStream(
     File.Open(&lt;span class="str"&gt;&amp;quot;northwind.xml&amp;quot;&lt;/span&gt;, FileMode.Open));
Northwind ctx = &lt;span class="kwrd"&gt;new&lt;/span&gt; Northwind(connString, map);&lt;/pre&gt;
  &lt;style type="text/css"&gt;


















.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;/blockquote&gt;

&lt;h2&gt;Additional References&lt;/h2&gt;

&lt;p&gt;If you want to look a bit more under the covers of sqlmetal, here’s a couple of reference links on MSDN:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/bb386987.aspx"&gt;Code Generation Tool (SqlMetal.exe)&lt;/a&gt;&lt;/p&gt;

  &lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/bb399400.aspx"&gt;Code Generation in LINQ to SQL&lt;/a&gt;&lt;/p&gt;

  &lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/bb386907.aspx"&gt;External Mapping Reference (LINQ to SQL)&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There is no shortage of posts on techniques for using POCO with LINQ To SQL; a &lt;a href="http://search.live.com/search?q=sqlmetal+poco"&gt;Live Search&lt;/a&gt; will bring you to plenty of reading on the subject!&amp;#160; One of the hits even led me to an open source project on CodePlex called &lt;a href="http://www.codeplex.com/Close2Poco"&gt;Close2Poco&lt;/a&gt;.&amp;#160; I haven’t had a chance to check it out, but it takes the trivial setup I proposed here much further toward reality.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9598324" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jimoneil/archive/tags/A-to-Z/default.aspx">A-to-Z</category></item><item><title>R is for... REST</title><link>http://blogs.msdn.com/jimoneil/archive/2009/05/04/r-is-for-rest.aspx</link><pubDate>Mon, 04 May 2009 07:08:05 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9585286</guid><dc:creator>joneil</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jimoneil/comments/9585286.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jimoneil/commentrss.aspx?PostID=9585286</wfw:commentRss><description>&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/Risfor.RoutedEvent_7D66/r_2.gif"&gt;&lt;img style="border-right-width: 0px; margin: 0px 15px 0px 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="R" border="0" alt="R" align="left" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/Risfor.RoutedEvent_7D66/r_thumb.gif" width="62" height="73" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;REST stands for Representational State Transfer, an acronym coined by &lt;a href="http://roy.gbiv.com/"&gt;Dr. Roy Fielding&lt;/a&gt; in 2000 as part of &lt;a href="http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm"&gt;his doctoral dissertation&lt;/a&gt; at the &lt;a href="http://www.uci.edu/"&gt;University of California, Irvine&lt;/a&gt;.&amp;#160; &lt;/p&gt;  &lt;p&gt;As Dr. Fielding states in Chapter 6, &lt;/p&gt;  &lt;p align="center"&gt;&lt;em&gt;Since 1994, the REST architectural style has been used to guide the design and development of the architecture for the modern Web.&lt;/em&gt;&lt;/p&gt;  &lt;p align="left"&gt;   &lt;br /&gt;REST is technically a formalization of his approach to building distributed network architectures, with the World Wide Web being the canonical example of its application.&amp;#160; Today, however, REST is often positioned as an alternative technology to &lt;a href="http://www.w3.org/TR/soap/"&gt;SOAP&lt;/a&gt; (Simple Object Access Protocol) for the implementation of web services.&amp;#160; To that end:&lt;/p&gt;  &lt;p align="left"&gt;&amp;#160;&lt;/p&gt;  &lt;p align="left"&gt;&lt;img style="border-right-width: 0px; margin: 0px 10px 0px 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="320px-Left_pointing_double_angle_quotation_mark_sh1_svg" border="0" alt="320px-Left_pointing_double_angle_quotation_mark_sh1_svg" align="left" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/Risfor.RoutedEvent_7D66/320px-Left_pointing_double_angle_quotation_mark_sh1_svg_3.png" width="55" height="60" /&gt; REST is defined by four interface constraints: identification of resources; manipulation of resources through representations; self-descriptive messages; and, hypermedia as the engine of application state. &lt;/p&gt;  &lt;p align="right"&gt;Section 5.1.5&lt;/p&gt;  &lt;p align="right"&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Identification of Resources&lt;/h2&gt;  &lt;p&gt;A REST approach to services treats everything on the web as a resource, and every resource has a name – its Uniform Resource Identifier (or URI).&amp;#160; A SOAP-based approach, in contrast, is operation-centric where the focus is on using the URI essentially as a method call, with parameters provided on the query string, as form variables, or perhaps in an encoded message body.&lt;/p&gt;  &lt;p&gt;For sake of example, let’s look at Amazon’s &lt;a href="http://aws.amazon.com/s3/"&gt;Simple Storage Service (S3)&lt;/a&gt;.&amp;#160; The service provides functionality to store arbitrary objects in the cloud, by assigning them to ‘buckets’ created within the storage account (similar to a one level directory/file structure).&amp;#160; So, your resource in this case can easily be expressed as a URI, such as     &lt;br /&gt;&lt;/p&gt;  &lt;p align="center"&gt;&lt;font size="3"&gt;http://s3.amazonaws.com/mybucket/myobject      &lt;br /&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p align="center"&gt;&lt;font color="#ff9900" size="3"&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p align="left"&gt;This URI, if provided in browser client for instance, will result in retrieving that particular resource (by virtue of the default GET action, which we’ll get to shortly).&amp;#160; &lt;/p&gt;  &lt;p align="left"&gt;Contrast that with a SOAP approach, which would be routed to a common URL:    &lt;br /&gt;&lt;/p&gt;  &lt;p align="center"&gt;&lt;code&gt;&lt;font size="3" face="Verdana"&gt;http://s3.amazonaws.com/soap        &lt;br /&gt;&lt;/font&gt;&lt;/code&gt;&lt;/p&gt;  &lt;p align="left"&gt;and require a payload SOAP message similar to&lt;/p&gt;  &lt;pre class="csharpcode"&gt;&amp;lt;GetObject xmlns=&lt;span class="str"&gt;&amp;quot;http://doc.s3.amazonaws.com/2006-03-01&amp;quot;&lt;/span&gt;&amp;gt;
  &amp;lt;Bucket&amp;gt;mybucket&amp;lt;/Bucket&amp;gt;
  &amp;lt;Key&amp;gt;myobject&amp;lt;/Key&amp;gt;
  &amp;lt;GetMetadata&amp;gt;&lt;span class="kwrd"&gt;true&lt;/span&gt;&amp;lt;/GetMetadata&amp;gt;
  &amp;lt;GetData&amp;gt;&lt;span class="kwrd"&gt;true&lt;/span&gt;&amp;lt;/GetData&amp;gt;
  &amp;lt;InlineData&amp;gt;&lt;span class="kwrd"&gt;true&lt;/span&gt;&amp;lt;/InlineData&amp;gt;
  &amp;lt;AWSAccessKeyId&amp;gt;1D9FVRAYCP1VJEXAMPLE=&amp;lt;/AWSAccessKeyId&amp;gt;
  &amp;lt;Timestamp&amp;gt;2009-05-01T12:00:00.183Z&amp;lt;/Timestamp&amp;gt;
  &amp;lt;Signature&amp;gt;Iuyz3d3P0aTou39dzbqaEXAMPLE=&amp;lt;/Signature&amp;gt;
&amp;lt;/GetObject&amp;gt;&lt;/pre&gt;
&lt;style type="text/css"&gt;




















.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;The contrast in approaches here is rather apparent.&amp;#160; In REST, the object or resource you’re addressing is immediately discoverable, whereas in a SOAP approach, the URI tells you next to nothing, and you have to parse the XML message payload to determine both what you want to do (&lt;code&gt;GetObject)&lt;/code&gt; and to what you want to do it (&lt;code&gt;&amp;lt;Bucket&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;Key&amp;gt;)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Manipulation of Resources through Representations&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The representation of a resource indicates the specific sequence of bytes that describes that resource.&amp;#160; A resource's representation isn’t absolute and can be driven by information sent in the message that acts upon the resource.&amp;#160; The ability to provide alternative representations provides a nimbleness that allows the resource to be treated in a format most conducive to the application at hand.&lt;/p&gt;

&lt;p&gt;For instance, in the implementation of &lt;a href="http://msdn.microsoft.com/en-us/data/bb931106.aspx"&gt;ADO.NET Data Services&lt;/a&gt;, a REST-based technology in .NET 3.5 SP1 for exposing data models over the web, two representations can be returned for a given resource depending on the &lt;code&gt;Accept:&lt;/code&gt; HTTP header passed in the request:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The default format is &lt;a href="http://bitworking.org/projects/atom/draft-ietf-atompub-protocol-04.html"&gt;Atom Publishing Protocol (AtomPub)&lt;/a&gt; &lt;/li&gt;

  &lt;li&gt;The alternative format, &lt;a href="http://www.json.org"&gt;JavaScript Object Notation (JSON)&lt;/a&gt;, can be engaged by setting the &lt;code&gt;Accept&lt;/code&gt; header’s value to &lt;code&gt;application/json&lt;/code&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The default format provides integration with other applications that support the AtomPub format and also with rich client applications, such as Silverlight, that interface with Data Services.&amp;#160; That format, however, isn’t as efficient when accessing services from within an AJAX-based client, and that’s where JSON comes in, providing a more compact format that can be quickly serialized for manipulation in the browser’s document object model (DOM).&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Self-descriptive messages&lt;/h2&gt;

&lt;p&gt;In terms of RESTful services, the message transport is generally HTTP, a protocol that includes a number of operations that can be applied to the resource specified in the URI.&amp;#160; Most implementations support the use of the following HTTP verbs to provide manipulation of the resource:&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;table border="1" cellspacing="0" cellpadding="2" width="400" align="center"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign="top" width="104"&gt;GET&lt;/td&gt;

      &lt;td valign="top" width="296"&gt;Retrieve a resource representation&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="104"&gt;POST&lt;/td&gt;

      &lt;td valign="top" width="296"&gt;Create a new resource based on representation provided in the HTTP message&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="104"&gt;PUT&lt;/td&gt;

      &lt;td valign="top" width="296"&gt;Update a resource given the representation provided in the HTTP message&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="104"&gt;DELETE&lt;/td&gt;

      &lt;td valign="top" width="296"&gt;Delete the resource&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Conveniently, these operations correspond to the CRUD (Create-Read-Update-Delete) operations that are part of most data-centric applications.&amp;#160; It’s this simple HTTP verb + URI approach for data access and manipulation that has propelled REST based services to become the mechanism of choice for simple data access in distributed applications.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Hypermedia as the engine of application state&lt;/h2&gt;

&lt;p&gt;The protocol of the web, HTTP, is at its core stateless, which has made life difficult for developers trying to build applications.&amp;#160; Concepts such as cookies, &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.ui.control.viewstate.aspx"&gt;ViewState&lt;/a&gt; in ASP.NET, and storing information in databases have all been used to overlay a sense of statefulness on the web.&lt;/p&gt;

&lt;p&gt;REST has a goal “to improve server scalability by eliminating any need for the server to maintain an awareness of the client state beyond the current request.” A direct result of this is that the representation returned from a RESTful request must contain the entire state of the application at that point.&amp;#160; That includes not only data, but also the enumeration of what you can do next in the application.&lt;/p&gt;

&lt;p&gt;For example, consider a shopping cart application on which you’ve just done a search for an item.&amp;#160; Such applications often make use of a cookie to tie the session to the stateful shopping cart for that user maintained on the server.&amp;#160; Strictly speaking, this breaks the RESTful paradigm, since the state of the application is not wholly described by the current page – it takes the cookie value to provide the connection between this set of search results and the current user.&amp;#160; If the page included a hidden field, for instance, that provides an ID for the user’s cart (which itself would be considered a resource in the REST paradigm), then the RESTful constraint would be met.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Read more about it&lt;/h2&gt;

&lt;p&gt;There are numerous on-line resources and books that cover REST topics, including&amp;#160; a few relatively recent ones that address REST in the specific domain of .NET technologies.&amp;#160;&amp;#160; Here’s a sampling that you may want to add to your library&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;a href="http://oreilly.com/catalog/9780596529260/"&gt;RESTful Web Services&lt;/a&gt;&lt;/p&gt;

  &lt;p&gt;&lt;a href="http://oreilly.com/catalog/9780596519209/?CMP=AFC-ak_book&amp;amp;ATT=RESTful+.NET%2c"&gt;RESTful .NET&lt;/a&gt;&lt;/p&gt;

  &lt;p&gt;&lt;a href="http://oreilly.com/catalog/9780596523091/"&gt;Data-Driven Services With Silverlight 2&lt;/a&gt;&lt;/p&gt;

  &lt;p&gt;&lt;a href="http://www.apress.com/book/view/9781430216148"&gt;Pro ADO.NET Data Services: Working with RESTful Data&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In terms of technology implementations, here’s a few that make heavy use of RESTful approaches&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/data/bb931106.aspx"&gt;ADO.NET Data Services&lt;/a&gt;&lt;/p&gt;

  &lt;p&gt;&lt;a href="http://www.asp.net/downloads/starter-kits/wcf-rest/"&gt;WCF REST Starter Kit&lt;/a&gt;&lt;/p&gt;

  &lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/dd179425.aspx"&gt;Azure Storage Services&lt;/a&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9585286" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jimoneil/archive/tags/REST/default.aspx">REST</category><category domain="http://blogs.msdn.com/jimoneil/archive/tags/A-to-Z/default.aspx">A-to-Z</category></item><item><title>Q is for... Quirks</title><link>http://blogs.msdn.com/jimoneil/archive/2009/04/27/q-is-for-quirks.aspx</link><pubDate>Mon, 27 Apr 2009 07:05:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9570290</guid><dc:creator>joneil</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jimoneil/comments/9570290.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jimoneil/commentrss.aspx?PostID=9570290</wfw:commentRss><description>&lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px 10px 0px 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Q" border="0" alt="Q" align="left" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/Qisfor.Quirks_13B1B/q_3.gif" width="58" height="77" /&gt; &lt;/p&gt;  &lt;p&gt;Quirks… we all have them, but rather than get too personal, we’ll constrain the domain for this post to web browsers.&amp;#160; &lt;/p&gt;  &lt;p&gt;In the early days of the web – pre-HTML and CSS (cascading style sheet) standards – browsers varied considerably over what features they implemented and how.&amp;#160; Over time standards matured, and browser vendors have sought to come into full compliance with developing HTML and CSS standards (which is a good thing); however, it leaves a number of old and often broken pages out there which demand backward compatibility.&lt;/p&gt;  &lt;p&gt;That’s where quirks mode comes in.&amp;#160; &lt;a href="http://www.quirksmode.org/css/quirksmode.html"&gt;It’s not an IE-specific concept&lt;/a&gt;, and essentially defines a baseline, pre-standards behavior for web page rendering.&amp;#160; In any recent version of IE, for example, quirks mode means a page will display as it would in IE 5.5.&amp;#160; The alternative to quirks mode is known as standards (or strict mode), and different browsers have different rules for determining which rendering mode to use.&amp;#160; Those rules are founded in a concept known as &lt;em&gt;doctype sniffing&lt;/em&gt; or &lt;em&gt;doctype switching&lt;/em&gt; introduced with IE 5.0 for the Macintosh.&lt;/p&gt;  &lt;p&gt;In IE8 (and IE7 for that matter), the presence of a &lt;code&gt;&amp;lt;!DOCTYPE&amp;gt;&lt;/code&gt; declaration will &lt;em&gt;often&lt;/em&gt; trigger standards (strict) mode.&amp;#160; For instance, the &lt;a href="http://microsoft.com"&gt;Microsoft home page&lt;/a&gt; includes this declaration indicating standards mode:&lt;/p&gt;  &lt;pre class="csharpcode"&gt;&lt;font size="1"&gt;&amp;lt;!-- DOCTYPE html PUBLIC 
   &lt;span class="str"&gt;&amp;quot;-//W3C//DTD XHTML 1.0 Transitional//EN&amp;quot;&lt;/span&gt; 
   &lt;span class="str"&gt;&amp;quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&amp;quot;&lt;/span&gt; --&amp;gt;&lt;/font&gt;&lt;/pre&gt;
&lt;style type="text/css"&gt;














.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;The absence of such a declaration will &lt;em&gt;always&lt;/em&gt; trigger quirks mode, which makes sense since old-school pages, the ones relying on pre-standards behavior, were written before &lt;code&gt;&amp;lt;!DOCTYPE&amp;gt;&lt;/code&gt; was a twinkle in Microsoft’s eye.&amp;#160; Knowing which &lt;code&gt;&amp;lt;!DOCTYPE&amp;gt;&lt;/code&gt; declarations indicate standards mode versus quirks mode in IE can be a bit of a challenge, so &lt;a href="http://msdn.microsoft.com/en-us/library/ms535242(VS.85).aspx"&gt;consult this table&lt;/a&gt; on MSDN for specifics.&amp;#160; (For Mozilla-based browsers, like Firefox, &lt;a href="https://developer.mozilla.org/en/Mozilla_Quirks_Mode_Behavior"&gt;check here&lt;/a&gt;, and for Opera, &lt;a href="http://www.opera.com/docs/specs/doctype/"&gt;see here&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;If you want to know what mode a given page is displayed in, you can use the nifty new Developer Tools in IE8 (accessible via Tools&amp;gt;Developer Tools or F12), which displays the Document Mode in its menu bar.&amp;#160;&amp;#160; For sake of example, the Amazon site does not have a &lt;code&gt;&amp;lt;!DOCTYPE&amp;gt;&lt;/code&gt; tag and therefore renders in quirks mode, as the toolbar shows below:&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Amazon in Quirks mode" border="0" alt="Amazon in Quirks mode" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/Qisfor.Quirks_13B1B/image_5.png" width="514" height="405" /&gt; &lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;You can also determine the mode of a page by executing a little JavaScript right from the URL text box in the browser.&amp;#160; Below, the response of 5 indicates quirks mode.&amp;#160; Other options are 7 and 8, indicating the standards mode operations of IE 7 and 8.&amp;#160; For more detail on these modes, as well as how they work hand-in-hand with IE8’s compatibility view, &lt;a href="http://blogs.msdn.com/jimoneil/archive/2009/04/10/it-s-for-ie-day-week-2.aspx"&gt;check out my IE blog post&lt;/a&gt; of a couple of weeks ago.&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="Accessing documentMode via JavaScript" border="0" alt="Accessing documentMode via JavaScript" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/Qisfor.Quirks_13B1B/image_8.png" width="517" height="258" /&gt;&lt;/p&gt;

&lt;p&gt;Now as I mentioned before, quirks mode is not an IE-specific thing.&amp;#160; For instance, it turns out that &lt;a href="http://www.google.com/firefox"&gt;default home page for Firefox&lt;/a&gt; displays in quirks mode as well.&amp;#160; In Firefox, you can use the View Page Info context menu option on the page to determine the mode:&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="Quirks mode in Firefox" border="0" alt="Quirks mode in Firefox" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/Qisfor.Quirks_13B1B/image_11.png" width="443" height="370" /&gt; &lt;/p&gt;

&lt;p&gt;What about other browsers?&amp;#160; &lt;a href="http://www.opera.com/"&gt;Opera&lt;/a&gt;?&amp;#160; &lt;a href="http://www.apple.com/safari"&gt;Safari&lt;/a&gt;?&amp;#160; Use the &lt;code&gt;&lt;a href="http://www.w3.org/TR/html5/dom.html#dom-document-compatmode"&gt;document.compatMode&lt;/a&gt;&lt;/code&gt; property, which returns one of two values&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code&gt;BackCompat&lt;/code&gt; indicating quirks mode, or &lt;/li&gt;

  &lt;li&gt;&lt;code&gt;CSS1Compat&lt;/code&gt; indicating standard mode (or almost-standards mode, &lt;a href="http://en.wikipedia.org/wiki/Quirks_mode#Almost_standards_mode"&gt;more on that here&lt;/a&gt;) &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For instance, Opera 9.64 reports the mode of &lt;a href="http://amazon.com"&gt;amazon.com&lt;/a&gt; as follows&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="Amazon.com in Opera" border="0" alt="Amazon.com in Opera" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/Qisfor.Quirks_13B1B/image_14.png" width="490" height="239" /&gt; &lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;while Safari 4 shows &lt;a href="http://microsoft.com"&gt;microsoft.com&lt;/a&gt; as:&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="Microsoft.com in Safari" border="0" alt="Microsoft.com in Safari" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/Qisfor.Quirks_13B1B/image_17.png" width="502" height="226" /&gt; &lt;/p&gt;

&lt;p&gt;Now, you might be wondering why we used &lt;code&gt;document.documentMode&lt;/code&gt; in the IE sample earlier, but &lt;code&gt;document.compatMode&lt;/code&gt; here.&amp;#160; With IE8, &lt;code&gt;compatMode&lt;/code&gt; is deprecated in favor of &lt;code&gt;documentMode&lt;/code&gt;, which has more granularity to differentiate between IE7 standards mode and IE8 standards mode.&amp;#160; Of course, since this is an IE-specific DOM extension, it’s not going to work cross-browser.&lt;/p&gt;

&lt;p&gt;Now, don’t let all this discussion distract you from what should be a goal of creating standards-compliant sites from now on.&amp;#160; Quirks mode is there to support existing sites, kind of like the &lt;a href="http://en.wikipedia.org/wiki/VHS"&gt;VHS player&lt;/a&gt; I keep around at home.&amp;#160; If you haven’t graduated to updated standards (like DVDs) for your mainstream requirements, then yeah, you are a bit quirky yourself!&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9570290" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jimoneil/archive/tags/A-to-Z/default.aspx">A-to-Z</category></item><item><title>P is for… Partial Method</title><link>http://blogs.msdn.com/jimoneil/archive/2009/04/20/p-is-for-partial-method.aspx</link><pubDate>Mon, 20 Apr 2009 07:01:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9554743</guid><dc:creator>joneil</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/jimoneil/comments/9554743.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jimoneil/commentrss.aspx?PostID=9554743</wfw:commentRss><description>&lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px 10px 0px 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="P" border="0" alt="P" align="left" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/PisforPartialMethod_117E8/p_3.gif" width="47" height="70" /&gt;&lt;/p&gt;  &lt;p&gt;By now, most of you are likely familiar with the concept of &lt;a href="http://msdn.microsoft.com/en-us/library/wa80x488(VS.80).aspx"&gt;partial classes&lt;/a&gt;, introduced in .NET 2.0 to both C# and &lt;a href="http://msdn.microsoft.com/en-us/library/yfzd5350.aspx"&gt;Visual Basic&lt;/a&gt;.&amp;#160; Simply put, classes can be marked with the &lt;code&gt;partial&lt;/code&gt; keyword indicating that their implementation is split over multiple physical source code files.&amp;#160; &lt;/p&gt;  &lt;p&gt;This technique is quite apparent when you look at Windows Forms code files, where the &lt;code&gt;designer.cs&lt;/code&gt; (or &lt;code&gt;.vb&lt;/code&gt;) file contains a partial class representing the form.&amp;#160; The designer class file contains a lot of glue code to create and initialize form controls and properties, so mucking around in it to add your own code and customization has always been an invitation for disaster: one errant Ctrl-X and you could be in for some rework.&lt;/p&gt;  &lt;p&gt;With partial classes though, you can create another code file and just re-declare the class, again with the &lt;code&gt;partial&lt;/code&gt; keyword, and via compiler magic the two separate code files will be combined into a single binary image – just as if you’d coded it in one place.&amp;#160; It’s essentially syntactic sugar that helps promote separation of concerns and maintainability within your applications.&lt;/p&gt;  &lt;p&gt;So that’s a partial class, but I though we were talking about &lt;strong&gt;&lt;em&gt;partial methods&lt;/em&gt;&lt;/strong&gt;?!&lt;/p&gt;  &lt;p&gt;Indeed!&amp;#160; Partial methods are less well known, but offer an interesting construct for extensibility at a lower level.&amp;#160; You can think of a partial method as a place holder for an implementation that may (or may not) be provided.&amp;#160; &lt;/p&gt;  &lt;p&gt;One technology that demonstrates the value and power of partial methods is the &lt;a href="http://msdn.microsoft.com/en-us/data/aa937723.aspx"&gt;ADO.NET Entity Framework&lt;/a&gt; (as does its cousin, &lt;a href="http://msdn.microsoft.com/en-us/library/bb386976.aspx"&gt;LINQ to SQL&lt;/a&gt;).&amp;#160; The Entity Framework is an object-relational mapping (ORM) framework that exposes an underlying data model as a set of .NET classes, essentially ‘objectifying’ and abstracting the data model, so that you can program to objects and let the framework worry about persisting modification to the ultimate data store, often a relational database.&lt;/p&gt;  &lt;p&gt;With the Entity Framework you can generate a data model that maps objects directly to database tables (although the technology as a whole is far more powerful than that).&amp;#160; So, for instance, when I use the Entity Framework against the venerable Northwind database, I’ll get a series of classes representing entities (here, tables) in the model, for example.&lt;/p&gt;  &lt;div class="csharpcode"&gt;   &lt;pre class="alt"&gt;[global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(&lt;/pre&gt;

  &lt;pre&gt;    NamespaceName = &lt;span class="str"&gt;&amp;quot;NorthwindModel&amp;quot;&lt;/span&gt;, Name = &lt;span class="str"&gt;&amp;quot;Categories&amp;quot;&lt;/span&gt;)]&lt;/pre&gt;

  &lt;pre class="alt"&gt;[global::System.Runtime.Serialization.DataContractAttribute(&lt;/pre&gt;

  &lt;pre&gt;    IsReference = &lt;span class="kwrd"&gt;true&lt;/span&gt;)]&lt;/pre&gt;

  &lt;pre class="alt"&gt;[global::System.Serializable()]&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;partial&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Categories : &lt;/pre&gt;

  &lt;pre class="alt"&gt;    global::System.Data.Objects.DataClasses.EntityObject&lt;/pre&gt;

  &lt;pre&gt;{&lt;/pre&gt;

  &lt;pre class="alt"&gt;...&lt;/pre&gt;

  &lt;pre&gt;}&lt;/pre&gt;

  &lt;pre class="alt"&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre&gt;[global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(&lt;/pre&gt;

  &lt;pre class="alt"&gt;    NamespaceName = &lt;span class="str"&gt;&amp;quot;NorthwindModel&amp;quot;&lt;/span&gt;, Name = &lt;span class="str"&gt;&amp;quot;CustomerDemographics&amp;quot;&lt;/span&gt;)]&lt;/pre&gt;

  &lt;pre&gt;[global::System.Runtime.Serialization.DataContractAttribute(&lt;/pre&gt;

  &lt;pre class="alt"&gt;    IsReference = &lt;span class="kwrd"&gt;true&lt;/span&gt;)]&lt;/pre&gt;

  &lt;pre&gt;[global::System.Serializable()]&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;partial&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; CustomerDemographics : &lt;/pre&gt;

  &lt;pre&gt;    global::System.Data.Objects.DataClasses.EntityObject&lt;/pre&gt;

  &lt;pre class="alt"&gt;{&lt;/pre&gt;

  &lt;pre&gt;...&lt;/pre&gt;

  &lt;pre class="alt"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;






















.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;Note, the classes here, &lt;code&gt;Categories&lt;/code&gt; and &lt;code&gt;CustomerDemographics,&lt;/code&gt; are partial classes as well.&amp;#160; Like a Windows Forms designer file, this model file is not really meant to be edited, since it will be refreshed whenever the model is modified through the visual design surface.&amp;#160; Extensions should be made in a separate file where these classes are ‘repeated’ and additional code added to customize the behavior according to the needs of the application.&lt;/p&gt;

&lt;p&gt;As you might suspect, if each table is represented as a class, each column in the table is represented as a property.&amp;#160; So just as the Northwind table has a &lt;code&gt;CategoryID&lt;/code&gt;, &lt;code&gt;CategoryName&lt;/code&gt;, &lt;code&gt;Description&lt;/code&gt;, and &lt;code&gt;Picture&lt;/code&gt; column, so will the Entity Framework class have properties with the same names and types.&amp;#160; The class actually exposes additional properties as well, namely collections that reflect relationships with other tables.&amp;#160; So for instance, the &lt;code&gt;Categories&lt;/code&gt; class has a property called &lt;code&gt;Products&lt;/code&gt;, which contains all of the products in the specific category.&amp;#160; This particular property exist because of the primary/foreign key relationship between the two tables, thereby making manifest the power of ORM technologies!&lt;/p&gt;

&lt;p&gt;Let’s take a look at the implementation of one of those properties, &lt;code&gt;CategoryName&lt;/code&gt;:&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;[global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;      (IsNullable = &lt;span class="kwrd"&gt;false&lt;/span&gt;)]&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;[global::System.Runtime.Serialization.DataMemberAttribute()]&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; CategoryName&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;{&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    get&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;    {&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;    &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;this&lt;/span&gt;._CategoryName;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;    }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;    set&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;    {&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;    &lt;span class="kwrd"&gt;this&lt;/span&gt;.OnCategoryNameChanging(&lt;span class="kwrd"&gt;value&lt;/span&gt;);&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;    &lt;span class="kwrd"&gt;this&lt;/span&gt;.ReportPropertyChanging(&lt;span class="str"&gt;&amp;quot;CategoryName&amp;quot;&lt;/span&gt;);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;    &lt;span class="kwrd"&gt;this&lt;/span&gt;._CategoryName = global::System.Data.Objects.DataClasses.&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;                      StructuralObject.SetValidValue(&lt;span class="kwrd"&gt;value&lt;/span&gt;, &lt;span class="kwrd"&gt;false&lt;/span&gt;);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;    &lt;span class="kwrd"&gt;this&lt;/span&gt;.ReportPropertyChanged(&lt;span class="str"&gt;&amp;quot;CategoryName&amp;quot;&lt;/span&gt;);&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;    &lt;span class="kwrd"&gt;this&lt;/span&gt;.OnCategoryNameChanged();&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;    }&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;}&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; _CategoryName;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;&lt;span class="kwrd"&gt;partial&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; OnCategoryNameChanging(&lt;span class="kwrd"&gt;string&lt;/span&gt; &lt;span class="kwrd"&gt;value&lt;/span&gt;);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;&lt;span class="kwrd"&gt;partial&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; OnCategoryNameChanged();&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Note lines 21 and 22 where two of the methods are defined with the partial keyword.&amp;#160; These methods are invoked by the property setter at lines 12 and 17, and can be thought of as &lt;em&gt;lightweight events&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Currently, there is no implementation for those methods, so when this class is compiled the calls are essentially removed and there will be no trace of the method in the resulting IL (Intermediate Language).&amp;#160; Since the implementation is optional, it’s a cleaner alternative than conditional compilation (#IF DEFINED) or dummy virtual method implementations.&lt;/p&gt;

&lt;p&gt;Let’s say though that I want to extend the class definition here and ensure that the &lt;code&gt;CategoryName&lt;/code&gt; always starts with a letter and is at least 5 characters long.&amp;#160; To enforce that constraint, I can provide an implementation of &lt;code&gt;OnCategoryNameChanging&lt;/code&gt; to enforce the rule and throw an exception if the value to be assigned does not meet the requirements.&amp;#160; So, I’ll create a partial class in another file and implement the partial method there:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;
&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Text.RegularExpressions;

&lt;span class="kwrd"&gt;namespace&lt;/span&gt; PartialMethodTest
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;partial&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Categories
    {
        &lt;span class="kwrd"&gt;partial&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; OnCategoryNameChanging(&lt;span class="kwrd"&gt;string&lt;/span&gt; &lt;span class="kwrd"&gt;value&lt;/span&gt;)
        {
            &lt;span class="kwrd"&gt;if&lt;/span&gt; (!Regex.IsMatch(&lt;span class="kwrd"&gt;value&lt;/span&gt;, &lt;span class="str"&gt;&amp;quot;^[A-Z].{4}&amp;quot;&lt;/span&gt;) )
                &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; ArgumentException(&lt;span class="str"&gt;&amp;quot;Category Name is not valid&amp;quot;&lt;/span&gt;);
        }
    }
}&lt;/pre&gt;

&lt;p&gt;Now we need to write a bit of code to test this out.&amp;#160; I’m just using a simple console application to instantiate the model and grab the first category in the list.&amp;#160; Then, I hardcode an invalid name to trigger the exception:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;
&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Linq;

&lt;span class="kwrd"&gt;namespace&lt;/span&gt; PartialMethodTest
{
    &lt;span class="kwrd"&gt;class&lt;/span&gt; Program
    {
        &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main(&lt;span class="kwrd"&gt;string&lt;/span&gt;[] args)
        {
            NorthwindEntities ctx = &lt;span class="kwrd"&gt;new&lt;/span&gt; NorthwindEntities();
            Categories firstCategory;

            firstCategory = ctx.Categories.FirstOrDefault(c =&amp;gt; &lt;span class="kwrd"&gt;true&lt;/span&gt;);
            Console.WriteLine(&lt;span class="str"&gt;&amp;quot;The original category is: &amp;quot;&lt;/span&gt; +
               firstCategory.CategoryName);

            &lt;span class="kwrd"&gt;try&lt;/span&gt;
            {
                firstCategory.CategoryName = &lt;span class="str"&gt;&amp;quot;1NET&amp;quot;&lt;/span&gt;;
            }
            &lt;span class="kwrd"&gt;catch&lt;/span&gt; (ArgumentException exArg)
            {
                Console.WriteLine(exArg.Message);
            }

            Console.WriteLine(&lt;span class="str"&gt;&amp;quot;The modified category is: &amp;quot;&lt;/span&gt; + 
               firstCategory.CategoryName);
            Console.ReadLine();
        }
    }
}&lt;/pre&gt;

&lt;p&gt;The result, as you might expect, is something like:&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Console" border="0" alt="Console" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/PisforPartialMethod_117E8/image_3.png" width="525" height="155" /&gt; &lt;/p&gt;

&lt;p&gt;So, with partial methods, I can pretty handily build a validation layer that is separated from my actual model.&amp;#160; If I need to swap in a different set of validation logic to customize an application for one customer versus another, it’s a piece of cake.&lt;/p&gt;

&lt;p&gt;The last thing I want to do here is drive home the point that a partial method invocation is not compiled into IL unless there is an implementation provided for it.&amp;#160; To show that, let’s point &lt;a href="http://msdn.microsoft.com/en-us/library/aa309387(VS.71).aspx"&gt;ILDASM&lt;/a&gt; at the generated assembly, and look at the implementation for the setter of the &lt;code&gt;CategoryName&lt;/code&gt; property. You can see a call to &lt;code&gt;OnCategoryNameChanging&lt;/code&gt; but no call to &lt;code&gt;OnCategoryNameChanged&lt;/code&gt;, which if you take a look at the C# code earlier in this post, is the last method that would be invoked in the setter.&amp;#160; It’s not in the IL, because it’s a partial method, and we did not provide an implementation for it – no execution overhead and no code bloat for functionality you opt out of.&amp;#160; Pretty cool, huh!&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/PisforPartialMethod_117E8/image_7.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/PisforPartialMethod_117E8/image_thumb_2.png" width="528" height="264" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9554743" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jimoneil/archive/tags/A-to-Z/default.aspx">A-to-Z</category></item><item><title>O is for… Oomph</title><link>http://blogs.msdn.com/jimoneil/archive/2009/04/13/o-is-for-oomph.aspx</link><pubDate>Mon, 13 Apr 2009 07:01:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9545544</guid><dc:creator>joneil</dc:creator><slash:comments>5</slash:comments><comments>http://blogs.msdn.com/jimoneil/comments/9545544.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jimoneil/commentrss.aspx?PostID=9545544</wfw:commentRss><description>&lt;script type="text/javascript" src="https://visitmix.com/labs/oomph/1.0/Client/jquery-1.2.6.min.js"&gt;&lt;/script&gt;&lt;script type="text/javascript" src="https://visitmix.com/labs/oomph/1.0/Client/oomph.min.js"&gt;&lt;/script&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px 10px 0px 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="O" border="0" alt="O" align="left" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/OisforOomph_13B/o_3.gif" width="55" height="65" /&gt; &lt;/p&gt;  &lt;p&gt;Oomph?!&amp;#160; no, I’m not making it up.&amp;#160; &lt;a href="http://visitmix.com/Lab/oomph"&gt;Oomph&lt;/a&gt; is a toolkit for developers facilitating the incorporation of &lt;a href="http://en.wikipedia.org/wiki/Microformats"&gt;microformats&lt;/a&gt; into web applications.&amp;#160; &lt;/p&gt;  &lt;p&gt;Ok, so what are microformats, you ask?&amp;#160; Here’s a definition from the go-to site on the subject, &lt;a href="http://microformats.org"&gt;microformats.org&lt;/a&gt;:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;em&gt;Designed for humans first and machines second, microformats are a set of simple, open data formats built upon existing and widely adopted standards. Instead of throwing away what works today, microformats intend to solve simpler problems first by adapting to current behaviors and usage patterns (e.g. &lt;abbr&gt;XHTML&lt;/abbr&gt;, blogging).&lt;/em&gt;&lt;/p&gt;    &lt;p&gt;&lt;em&gt;&lt;/em&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;h1&gt;A Microformat Primer&lt;/h1&gt;  &lt;p&gt;What microformats do is apply existing XHTML tags to provide semantic meaning to a set of data.&amp;#160; For instance, the &lt;a href="http://en.wikipedia.org/wiki/VCard"&gt;vCard&lt;/a&gt; format (&lt;a href="http://www.ietf.org/rfc/rfc2426.txt"&gt;RFC2426&lt;/a&gt;) was introduced in the mid-90s as a file format to disseminate business contacts, but as you can see it’s a format meant for machines first:&lt;/p&gt;  &lt;blockquote&gt;&lt;code&gt;     &lt;p&gt;BEGIN:VCARD        &lt;br /&gt;VERSION:2.1         &lt;br /&gt;N;LANGUAGE=en-us:O'Neil;Jim         &lt;br /&gt;FN:Jim O'Neil         &lt;br /&gt;ORG:Microsoft         &lt;br /&gt;TITLE:Developer Evangelist         &lt;br /&gt;ADR;WORK;PREF:;;201 Jones Road;Waltham;MA;02541;United States of America         &lt;br /&gt;LABEL;WORK;PREF;ENCODING=QUOTED-PRINTABLE:201 Jones Road=0D=0A=         &lt;br /&gt;Waltham MA 02451         &lt;br /&gt;URL;WORK:&lt;a href="http://blogs.msdn.com/jimoneil"&gt;http://blogs.msdn.com/jimoneil&lt;/a&gt;         &lt;br /&gt;EMAIL;PREF;INTERNET:jim.oneil@microsoft.com         &lt;br /&gt;REV:20090411T015215Z         &lt;br /&gt;END:VCARD&lt;/p&gt;   &lt;/code&gt;&lt;/blockquote&gt;  &lt;p&gt;The associated microformat is known as &lt;a href="http://microformats.org/wiki/hcard"&gt;hCard&lt;/a&gt; and uses existing XHTML markup to contain the same information as part of a web page.&amp;#160;&amp;#160; Here’s the equivalent hCard information, along with the markup:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Display&lt;/strong&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;div class="vcard"&gt;&lt;a class="fn url" href="http://blogs.msdn.com/jimoneil"&gt;Jim O'Neil&lt;/a&gt;       &lt;div class="org"&gt;Microsoft&lt;/div&gt;     &lt;a class="email" href="mailto:jim.oneil@microsoft.com"&gt;jim.oneil@microsoft.com&lt;/a&gt;       &lt;div class="adr"&gt;       &lt;div class="street-address"&gt;201 Jones Road&lt;/div&gt;       &lt;span class="locality"&gt;Waltham&lt;/span&gt;, &lt;span class="region"&gt;MA&lt;/span&gt;, &lt;span class="postal-code"&gt;02451&lt;/span&gt; &lt;span class="country-name"&gt;United States&lt;/span&gt; &lt;/div&gt;   &lt;/div&gt; &lt;/blockquote&gt;  &lt;div&gt;&lt;strong&gt;Markup&lt;/strong&gt;&lt;/div&gt;  &lt;blockquote&gt;   &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt; &lt;span class="attr"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;vcard&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt; &lt;span class="attr"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;fn url&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;http://blogs.msdn.com/jimoneil&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;br /&gt;&lt;/span&gt;      Jim O'Neil&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt; &lt;span class="attr"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;org&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Microsoft&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt; &lt;span class="attr"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;email&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;mailto:jim.oneil@microsoft.com&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
      jim.oneil@microsoft.com&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; 
   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt; &lt;span class="attr"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;adr&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt; &lt;span class="attr"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;street-address&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;201 Jones Road&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;span&lt;/span&gt; &lt;span class="attr"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;locality&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Waltham&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;span&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;, 
      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;span&lt;/span&gt; &lt;span class="attr"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;region&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;MA&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;span&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;, 
      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;span&lt;/span&gt; &lt;span class="attr"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;postal-code&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;02451&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;span&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; 
      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;span&lt;/span&gt; &lt;span class="attr"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;country-name&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;United States&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;span&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; 
   &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;style type="text/css"&gt;




















.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;style type="text/css"&gt;























.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;To the web page viewer the display looks no different than if had it been included with traditional markup; however, now that the markup exposes some semantics, it can be treated as a collection of data with specific meaning, and therein lies the value.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h1&gt;&lt;a href="http://www.codeplex.com/Oomph"&gt;Oomph Microformat Toolkit&lt;/a&gt;&lt;/h1&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://oomph.codeplex.com/"&gt;&lt;img style="border-right-width: 0px; margin: 10px auto; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Oomph" border="0" alt="Oomph" src="http://download.codeplex.com/Project/Download/FileDownload.aspx?ProjectName=Oomph&amp;amp;DownloadId=47046&amp;amp;Build=14954" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;The Oomph Microformat Toolkit, which you can &lt;a href="http://www.codeplex.com/Oomph"&gt;download from CodePlex&lt;/a&gt;, facilitates the use of microformats with three primary offerings:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;an aggregator that detects microformats on the page and provides an overlay allowing users to quickly store calendar appointments (&lt;a href="http://microformats.org/wiki/hcalendar"&gt;hCalendar&lt;/a&gt;) and contact information (hCard) to their local machine &lt;/li&gt;

  &lt;li&gt;a library of 14 CSS styles to be applied to hCard and hCalendar microformats &lt;/li&gt;

  &lt;li&gt;a Live Writer plug-in to ease the effort in producing microformats &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Aggregator&lt;/h2&gt;

&lt;p&gt;An IE add-in included with the toolkit enables end-users to have web pages light up with the gleam (the icon you should see at the upper left of this page) when microformats are detected.&amp;#160; The gleam can also be enabled by developers&amp;#160; simply referencing two JavaScript files (one for jQuery and one for Oomph) to their pages.&amp;#160; &lt;em&gt;[Note: the IE add-in injects the needed JavaScript if not explicitly included on the page.&amp;#160; If you’re developing your own pages with microformats, including the JavaScript yourself will ensure a consistent cross-browser experience for your users].&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;When hovering over the gleam, an overlay appears providing an interface to add the detected hCalendar and hCard items to your local calendar and contact list. Since I included a sample hCard in my post above, this page itself should be exhibiting the gleam at the upper left corner and when clicked result in the following overlay: &lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="Oomph overlay" border="0" alt="Oomph overlay" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/OisforOomph_13B/image_8.png" width="524" height="190" /&gt; &lt;/p&gt;

&lt;p&gt;There are no event (hCalendar) entries, but my contact information does appear on the right, along with icons below it to add to my Outlook, Yahoo, or Mac contacts, and since my contact includes a location, I can select the map (or globe) icon at the upper left, and it will plot the locations on Virtual Earth!&amp;#160; Cool, huh!&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Virtual Earth" border="0" alt="Virtual Earth" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/OisforOomph_13B/image_11.png" width="515" height="159" /&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;CSS Styles&lt;/h2&gt;

&lt;p&gt;If you don’t like the default overlay presentation, you can change it by modifying the &lt;code&gt;oomph.js&lt;/code&gt; file that is part of the toolkit (and referenced on the target page).&amp;#160; Or, if you simply want to reskin the display, there are multiple CSS files accompanying the toolkit that you can install on your server and reference within your microformat-outfitted pages.&amp;#160;&amp;#160; Here’s a couple of static samples (this page isn’t outfitted with the CSS file to see them ‘live’).&lt;/p&gt;

&lt;table border="0" cellspacing="0" cellpadding="2" width="526"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign="top" width="262"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; margin-left: 0px; border-left-width: 0px; margin-right: 0px" title="Notebook theme" border="0" alt="Notebook theme" align="left" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/OisforOomph_13B/image_16.png" width="184" height="121" /&gt; &lt;/td&gt;

      &lt;td valign="top" width="262"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; margin-left: 0px; border-left-width: 0px; margin-right: 0px" title="Rounded corner theme" border="0" alt="Rounded corner theme" align="right" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/OisforOomph_13B/image_17.png" width="240" height="129" /&gt; &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Live Writer plug-in&lt;/h2&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px 10px 0px 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" align="left" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/OisforOomph_13B/image_20.png" width="231" height="253" /&gt;The &lt;a href="http://oomph.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=19840#DownloadId=50202"&gt;Microsoft installer image&lt;/a&gt; includes the IE add-in, the CSS files referred to above, and a Live Writer plug-in to aid in authoring hCard microformats for your site.&amp;#160; The plug-in collects the contact information and generates the requisite markup.&lt;/p&gt;

&lt;p&gt;There are a few more Live Writer plug-ins available outside of Oomph as well:&lt;/p&gt;

&lt;ul&gt;
  &lt;ul&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href="http://gallery.live.com/liveItemDetail.aspx?li=9751e563-1408-4fc3-8028-bd4351edb1fb&amp;amp;bt=9&amp;amp;pl=8"&gt;Event Plugin (hCalendar)&lt;/a&gt; &lt;/li&gt;

      &lt;li&gt;&lt;/li&gt;

      &lt;li&gt;&lt;a href="http://gallery.live.com/liveItemDetail.aspx?li=6e57ab6f-aab9-439b-b578-0ea3b1d95d36&amp;amp;bt=9"&gt;Geo Microformat&lt;/a&gt; &lt;/li&gt;

      &lt;li&gt;&lt;/li&gt;

      &lt;li&gt;&lt;a href="http://www.codeplex.com/WLWPlugins"&gt;XFN Link&lt;/a&gt; (XFN stands for &lt;a href="http://gmpg.org/xfn/"&gt;XHTML Friends Network&lt;/a&gt; and is a microformat to capture human relationships via the &lt;code&gt;rel&lt;/code&gt; attribute) &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/ul&gt;
&lt;/ul&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h1&gt;More Information&lt;/h1&gt;

&lt;p&gt;Here’s a number of additional Microsoft resources on Oomph and several links of note on the subject of microformats in general.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;a href="http://oomph.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=19840"&gt;Oomph CodePlex project&lt;/a&gt; (&lt;a href="http://oomph.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=19840#DownloadId=50204"&gt;documentation direct link&lt;/a&gt;)&lt;/p&gt;

  &lt;p&gt;&lt;a href="http://visitmix.com/Lab/oomph"&gt;Oomph on MIX On-Line&lt;/a&gt;&lt;/p&gt;

  &lt;p&gt;&lt;a href="http://channel9.msdn.com/pdc2008/PC55/"&gt;Oomph PDC Session&lt;/a&gt; (on Channel9)&lt;/p&gt;

  &lt;p&gt;&lt;a href="http://visitmix.com/Articles/Prototype-Oomph-A-Microformats-Toolkit"&gt;Designing with Microformats&lt;/a&gt; (article by Tim Aidlin)&lt;/p&gt;

  &lt;p&gt;&lt;a href="http://microformats.org"&gt;microformats.org&lt;/a&gt;&lt;/p&gt;

  &lt;p&gt;&lt;a href="http://www.amazon.com/Microformats-Empowering-Your-Markup-Web/dp/1590598148"&gt;&lt;em&gt;Microformats: Empowering Your Markup for Web 2.0&lt;/em&gt;&lt;/a&gt; (on Amazon)&lt;/p&gt;&lt;/blockquote&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9545544" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jimoneil/archive/tags/A-to-Z/default.aspx">A-to-Z</category></item><item><title>N is for… Nullable&lt;T&gt;</title><link>http://blogs.msdn.com/jimoneil/archive/2009/04/06/n-is-for-nullable-t.aspx</link><pubDate>Mon, 06 Apr 2009 07:10:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9531960</guid><dc:creator>joneil</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jimoneil/comments/9531960.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jimoneil/commentrss.aspx?PostID=9531960</wfw:commentRss><description>&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/NisforNullableT_F9B7/n_2.gif"&gt;&lt;img style="border-right-width: 0px; margin: 0px 15px 0px 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="n" border="0" alt="n" align="left" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/NisforNullableT_F9B7/n_thumb.gif" width="55" height="65" /&gt;&lt;/a&gt; &lt;code&gt;Nullable&amp;lt;T&amp;gt;&lt;/code&gt; is a generic structure introduced with the .NET Framework 2.0 to support the concept of an undefined value.&amp;#160; The &lt;code&gt;T&lt;/code&gt; in &lt;code&gt;Nullable&amp;lt;T&amp;gt;&lt;/code&gt; is a value type; reference types, like strings, support nullability by design.&amp;#160; For instance, in C#:&lt;/p&gt;  &lt;pre class="csharpcode"&gt;String s = &lt;span class="kwrd"&gt;null&lt;/span&gt;; &lt;span class="rem"&gt;// works fine!&lt;/span&gt;
Int32 i = &lt;span class="kwrd"&gt;null&lt;/span&gt;;  &lt;span class="rem"&gt;// compilation error: cannot convert null to 'int'&lt;/span&gt;
                 &lt;span class="rem"&gt;// because it is a non-nullable value type&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&lt;style type="text/css"&gt;




































.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00;}
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;/p&gt;

&lt;p&gt;Note that since &lt;code&gt;Nullable&amp;lt;T&amp;gt;&lt;/code&gt; is a structure, it’s a value type itself and therefore fairly lightweight.&amp;#160; It’s the same size as the underlying type with the additional overhead of a boolean field, and instances are still placed on the stack versus the heap.&lt;/p&gt;

&lt;p&gt;Fundamental to the type are the two properties &lt;code&gt;Value&lt;/code&gt; and &lt;code&gt;HasValue&lt;/code&gt;.&amp;#160; &lt;code&gt;HasValue&lt;/code&gt; is a boolean property that indicates whether or not the &lt;code&gt;Value&lt;/code&gt; property is assigned.&amp;#160; The code snippet below, for instance, yields the output that follows &lt;strong&gt;&lt;em&gt;and&lt;/em&gt;&lt;/strong&gt; throws an &lt;code&gt;InvalidOperationException&lt;/code&gt; on Line 10 because of the attempt to access the &lt;code&gt;Value&lt;/code&gt; property of the null-valued &lt;code&gt;j&lt;/code&gt;.&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;Nullable&amp;lt;Int32&amp;gt; i = 5;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;Nullable&amp;lt;Int32&amp;gt; j = &lt;span class="kwrd"&gt;null&lt;/span&gt;;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;Console.WriteLine(i.HasValue);&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;Console.WriteLine(i == &lt;span class="kwrd"&gt;null&lt;/span&gt;);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;Console.WriteLine(i.Value);&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;Console.WriteLine(j.HasValue);&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;Console.WriteLine(j == &lt;span class="kwrd"&gt;null&lt;/span&gt;);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;Console.WriteLine(j.Value);&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px 10px 0px 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Console output" border="0" alt="Console output" align="left" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/NisforNullableT_F9B7/image_3.png" width="267" height="136" /&gt; &lt;/p&gt;

&lt;p&gt;As you might expect, there’s some shortcuts available too.&amp;#160; For instance, you can access &lt;code&gt;i.Value&lt;/code&gt; as just &lt;code&gt;i&lt;/code&gt;, and &lt;code&gt;i.HasValue == false&lt;/code&gt; is equivalent to the condition &lt;code&gt;i == null&lt;/code&gt;.&amp;#160; There’s also a &lt;code&gt;GetValueOrDefault&lt;/code&gt; method available, which will perform the null test and return the default value of the underlying type if the current value is actually null.&lt;/p&gt;

&lt;p&gt;And there’s even a bit more syntactic sugar, as both C# and Visual Basic support the use of the ? as part of a synonym notation, so the following declarations are equivalent:&lt;/p&gt;

&lt;table border="0" cellspacing="0" cellpadding="2" width="451" align="center"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign="top" width="200"&gt;
        &lt;p align="center"&gt;&lt;strong&gt;C# declarations&lt;/strong&gt;&lt;/p&gt;
      &lt;/td&gt;

      &lt;td valign="top" width="21"&gt;&amp;#160;&lt;/td&gt;

      &lt;td valign="top" width="228"&gt;
        &lt;p align="center"&gt;&lt;strong&gt;Visual Basic declarations&lt;/strong&gt;&lt;/p&gt;
      &lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="200"&gt;
        &lt;pre class="csharpcode"&gt;Nullable&amp;lt;Int32&amp;gt; i;
Int32? i;&lt;/pre&gt;
        &lt;style type="text/css"&gt;




























.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;/td&gt;

      &lt;td valign="top" width="21"&gt;&amp;#160;&lt;/td&gt;

      &lt;td valign="top" width="228"&gt;
        &lt;div class="csharpcode"&gt;
          &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;Dim&lt;/span&gt; i &lt;span class="kwrd"&gt;As&lt;/span&gt; Nullable(Of Int32)&lt;/pre&gt;

          &lt;pre&gt;&lt;span class="kwrd"&gt;Dim&lt;/span&gt; i &lt;span class="kwrd"&gt;As&lt;/span&gt; Int32?&lt;/pre&gt;

          &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;Dim&lt;/span&gt; i? &lt;span class="kwrd"&gt;As&lt;/span&gt; Int32&lt;/pre&gt;
        &lt;/div&gt;
        &lt;style type="text/css"&gt;




























.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;Nullable types support both explicit and implicit conversions to their non-nullable counterparts, so consider the following C# snippet:&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;Int32  i;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;Int32? j;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;i = j.Value;    &lt;span class="rem"&gt;// ok, but throws exception if j is null&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;i = (&lt;span class="kwrd"&gt;int&lt;/span&gt;) j;    &lt;span class="rem"&gt;// ok, but throws exception if j is null&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;i = j;          &lt;span class="rem"&gt;// compilation fails, explicit cast required&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;j = &lt;span class="kwrd"&gt;null&lt;/span&gt;;       &lt;span class="rem"&gt;// null assignment&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;j = 10;         &lt;span class="rem"&gt;// implicit cast to nullable type&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;j = i;          &lt;span class="rem"&gt;// implicit cast to nullable type&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;
























.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;Note, in Visual Basic, Option Strict is off by default, so in Visual Basic Line 6 would not cause a compilation error in the default scenario.&lt;/p&gt;

&lt;p&gt;To make the scenarios in Lines 4 and 5 above bulletproof, you can test for the null in multiple ways as shown below:&lt;/p&gt;

&lt;table border="0" cellspacing="0" cellpadding="2" width="400" align="center"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign="top"&gt;
        &lt;p align="center"&gt;&lt;strong&gt;C#&lt;/strong&gt;&lt;/p&gt;
      &lt;/td&gt;

      &lt;td valign="top"&gt;&amp;#160;&lt;/td&gt;

      &lt;td valign="top"&gt;
        &lt;p align="center"&gt;&lt;strong&gt;Visual Basic&lt;/strong&gt;&lt;/p&gt;
      &lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top"&gt;
        &lt;div class="csharpcode"&gt;
          &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;Int32  i;&lt;/pre&gt;
        &lt;/div&gt;

        &lt;div class="csharpcode"&gt;
          &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;Int32? j = &lt;span class="kwrd"&gt;null&lt;/span&gt;;&lt;/pre&gt;
        &lt;/div&gt;

        &lt;div class="csharpcode"&gt;
          &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;
        &lt;/div&gt;

        &lt;div class="csharpcode"&gt;
          &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;&lt;span class="kwrd"&gt;if&lt;/span&gt; (j.HasValue)&lt;/pre&gt;
        &lt;/div&gt;

        &lt;div class="csharpcode"&gt;
          &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    i = j.Value;&lt;/pre&gt;
        &lt;/div&gt;

        &lt;div class="csharpcode"&gt;
          &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;&lt;span class="kwrd"&gt;else&lt;/span&gt;&lt;/pre&gt;
        &lt;/div&gt;

        &lt;div class="csharpcode"&gt;
          &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;    i = 0;&lt;/pre&gt;
        &lt;/div&gt;

        &lt;div class="csharpcode"&gt;
          &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;
        &lt;/div&gt;

        &lt;div class="csharpcode"&gt;
          &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;
        &lt;/div&gt;

        &lt;div class="csharpcode"&gt;
          &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;&lt;span class="kwrd"&gt;if&lt;/span&gt; (j != &lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;/pre&gt;
        &lt;/div&gt;

        &lt;div class="csharpcode"&gt;
          &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;    i = (&lt;span class="kwrd"&gt;int&lt;/span&gt;)j;&lt;/pre&gt;
        &lt;/div&gt;

        &lt;div class="csharpcode"&gt;
          &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;&lt;span class="kwrd"&gt;else&lt;/span&gt;&lt;/pre&gt;
        &lt;/div&gt;

        &lt;div class="csharpcode"&gt;
          &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;    i = 0;&lt;/pre&gt;
        &lt;/div&gt;

        &lt;div class="csharpcode"&gt;
          &lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;
        &lt;/div&gt;

        &lt;div class="csharpcode"&gt;
          &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;
        &lt;/div&gt;

        &lt;div class="csharpcode"&gt;
          &lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;i = j.GetValueOrDefault();&lt;/pre&gt;
        &lt;/div&gt;

        &lt;div class="csharpcode"&gt;
          &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;
        &lt;/div&gt;

        &lt;div class="csharpcode"&gt;
          &lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;i = j ?? 0;&lt;/pre&gt;
        &lt;/div&gt;
      &lt;/td&gt;

      &lt;td valign="top"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/td&gt;

      &lt;td valign="top"&gt;
        &lt;div class="csharpcode"&gt;
          &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;Dim&lt;/span&gt; i &lt;span class="kwrd"&gt;As&lt;/span&gt; Int32&lt;/pre&gt;

          &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;&lt;span class="kwrd"&gt;Dim&lt;/span&gt; j &lt;span class="kwrd"&gt;As&lt;/span&gt; Int32? = &lt;span class="kwrd"&gt;Nothing&lt;/span&gt;&lt;/pre&gt;

          &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

          &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;&lt;span class="kwrd"&gt;If&lt;/span&gt; (j.HasValue) &lt;span class="kwrd"&gt;Then&lt;/span&gt;&lt;/pre&gt;

          &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;   i = j.Value&lt;/pre&gt;

          &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;&lt;span class="kwrd"&gt;Else&lt;/span&gt;&lt;/pre&gt;

          &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;   i = 0&lt;/pre&gt;

          &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;&lt;/pre&gt;

          &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

          &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;&lt;span class="kwrd"&gt;If&lt;/span&gt; (j &amp;lt;&amp;gt; &lt;span class="kwrd"&gt;Nothing&lt;/span&gt;) &lt;span class="kwrd"&gt;Then&lt;/span&gt;&lt;/pre&gt;

          &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;   i = &lt;span class="kwrd"&gt;CType&lt;/span&gt;(j, Int32)&lt;/pre&gt;

          &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;&lt;span class="kwrd"&gt;Else&lt;/span&gt;&lt;/pre&gt;

          &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;   i = 0&lt;/pre&gt;

          &lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;&lt;/pre&gt;

          &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

          &lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;i = j.GetValueOrDefault()&lt;/pre&gt;

          &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

          &lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;i = &lt;span class="kwrd"&gt;If&lt;/span&gt;(j, 0)&lt;/pre&gt;
        &lt;/div&gt;
        &lt;style type="text/css"&gt;






















.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Lines 4 through 16 are variations on constructs we’ve already discussed, and while they are all safe, the &lt;code&gt;if&lt;/code&gt; constructs are clunky, and the &lt;code&gt;GetValueOrDefault&lt;/code&gt; doesn’t offer much choice when the underlying value is indeed null.&amp;#160; That’s where the coalesce operators on Line 16 (&lt;code&gt;??&lt;/code&gt; in C#, and &lt;code&gt;If(x,y)&lt;/code&gt; in Visual Basic) come in.&amp;#160;&amp;#160; Each returns the value of the first argument (&lt;code&gt;j&lt;/code&gt;) if the underlying value is non-null, and the second value (zero, here) if not.&lt;/p&gt;

&lt;p&gt;This can come in handy when dealing with nullable boolean types for instance, since they cannot be used in C# conditional statements.&amp;#160;&amp;#160; Presuming you want to interpret a null as a false condition, the following snippet would work well; however, you might want to strongly consider &lt;a href="http://blogs.msdn.com/kcwalina/archive/2008/07/16/Nullable.aspx"&gt;whether use of three-valued boolean logic is a good idea&lt;/a&gt; in general.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;Boolean? b = &lt;span class="kwrd"&gt;null&lt;/span&gt;;
&lt;span class="kwrd"&gt;if&lt;/span&gt; (b ?? &lt;span class="kwrd"&gt;false&lt;/span&gt;) 
    Console.WriteLine(&lt;span class="str"&gt;&amp;quot;Condition is true&amp;quot;&lt;/span&gt;);
&lt;span class="kwrd"&gt;else&lt;/span&gt;
    Console.WriteLine(&lt;span class="str"&gt;&amp;quot;Condition is false or null&amp;quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;style type="text/css"&gt;












.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;Visual Basic actually handles nullable booleans differently, and the following compiles just fine (even with Option Strict on) – bringing to the surface a &lt;a href="http://blogs.msdn.com/timng/archive/2008/03/24/vb-expression-trees-coalesce-operator.aspx"&gt;subtlety in the implementation&lt;/a&gt; across the languages.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Dim&lt;/span&gt; b &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Boolean&lt;/span&gt;? = &lt;span class="kwrd"&gt;Nothing&lt;/span&gt;
&lt;span class="kwrd"&gt;If&lt;/span&gt; (b) &lt;span class="kwrd"&gt;Then&lt;/span&gt;
   Console.WriteLine(&lt;span class="str"&gt;&amp;quot;Condition is true&amp;quot;&lt;/span&gt;)
&lt;span class="kwrd"&gt;Else&lt;/span&gt;
   Console.WriteLine(&lt;span class="str"&gt;&amp;quot;Condition is false or null&amp;quot;&lt;/span&gt;)
&lt;span class="kwrd"&gt;End&lt;/span&gt; If&lt;/pre&gt;
&lt;style type="text/css"&gt;











.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;For more in depth information on the treatment of null types, check out the following MSDN articles:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms235245.aspx"&gt;Visual Basic Programming Guide: Nullable Value Types&lt;/a&gt; &lt;/li&gt;

  &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/1t3y8s4s.aspx"&gt;C# Programming Guide: Nullable Types&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9531960" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jimoneil/archive/tags/A-to-Z/default.aspx">A-to-Z</category></item><item><title>M is for… M</title><link>http://blogs.msdn.com/jimoneil/archive/2009/03/30/m-is-for-m.aspx</link><pubDate>Mon, 30 Mar 2009 07:02:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9517782</guid><dc:creator>joneil</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jimoneil/comments/9517782.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jimoneil/commentrss.aspx?PostID=9517782</wfw:commentRss><description>&lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px 20px 0px 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="M" border="0" alt="M" align="left" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/MisforM_DED8/m_3.gif" width="55" height="65" /&gt;&lt;em&gt;M is for M:&lt;/em&gt;&amp;#160; How cool is that?&lt;/p&gt;  &lt;p&gt;You really can’t start talking about M without first mentioning &lt;a href="http://msdn.microsoft.com/en-us/library/dd129514(VS.85).aspx"&gt;Oslo&lt;/a&gt;, the code-name for Microsoft’s model-driven development infrastructure.&amp;#160; Oslo was formally introduced at &lt;a href="http://microsoftpdc.com"&gt;Microsoft PDC&lt;/a&gt; back in October 2008, and its overarching goal is to reduce the disconnect between developer intent and implementation.&amp;#160;&amp;#160;&amp;#160; &lt;/p&gt;  &lt;p&gt;In model-driven development, the model &lt;strong&gt;&lt;em&gt;is&lt;/em&gt;&lt;/strong&gt; the application, versus just a design artifact.&amp;#160; You build a model via some tools, and you execute that model via a runtime environment, thus enhancing the transparency, flexibility, and productivity of the development process.&amp;#160; Consider &lt;a href="http://msdn.microsoft.com/en-us/library/ms752059.aspx"&gt;XAML&lt;/a&gt; (eXtensible Application Markup Language) for instance.&amp;#160; XAML provides a model – or more precisely a &lt;em&gt;domain specific language&lt;/em&gt; or DSL – for describing user interfaces.&amp;#160; And this XAML can be consumed by the &lt;a href="http://msdn.microsoft.com/en-us/netframework/aa663326.aspx"&gt;Windows Presentation Foundation (WPF)&lt;/a&gt; or &lt;a href="http://silverlight.net"&gt;Silverlight&lt;/a&gt; runtime to ‘execute the model.’&lt;/p&gt;  &lt;p&gt;Oslo itself comprises three main components:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/dd129586(VS.85).aspx"&gt;Repository&lt;/a&gt; – a SQL Server database to store models and metadata, &lt;/p&gt;    &lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/oslo/dd576274.aspx"&gt;Quadrant&lt;/a&gt; – a visual design tool, and&lt;/p&gt;    &lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/dd129519(VS.85).aspx"&gt;M&lt;/a&gt; – a declarative language for modeling.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;M, the topic of interest for this post, is a family of declarative, textual languages for building and working with models.&amp;#160; Within this family are&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;MSchema – for describing constraints and types,&lt;/p&gt;    &lt;p&gt;MGraph – for defining instances of structured data, and&lt;/p&gt;    &lt;p&gt;MGrammar – for defining DSLs.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;To get started, you’ll want to download the &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=f2f4544c-626c-44a3-8866-b2a9fe078956&amp;amp;displaylang=en"&gt;Oslo SDK&lt;/a&gt;; the current version is the January 2009 CTP.&amp;#160; In addition to providing assemblies, samples, and documentation, the SDK sets up the SQL Server 2008 repository and provides a tool called Intellipad for authoring M documents.&amp;#160; You can also work with M and Oslo in Visual Studio, via the “M” Project template installed by the SDK.&lt;/p&gt;  &lt;p&gt;To make this all a bit more real, I’ll work with a simple domain (here, universities I’ve attended) to create a model, add some data, and develop a simple domain-specific language.&amp;#160; In the process, I’ll touch on M’s three variants.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h1&gt;MSchema&lt;/h1&gt;  &lt;p&gt;Let’s get started by defining the model with MSchema.&amp;#160; Below I’m using Intellipad , which ships with the Oslo CTP, to define my model.&lt;/p&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="Intellipad" border="0" alt="Intellipad" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/MisforM_DED8/image_3.png" width="516" height="395" /&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;The model includes two types, one representing the school and one representing a state.&amp;#160; On lines 8 and 19, collections of these types (referred to as &lt;em&gt;extents&lt;/em&gt;) are defined; the * indicates one or more occurrences.&amp;#160; In line 19, the list of schools is further constrained to those universities with &lt;code&gt;State&lt;/code&gt; values in the &lt;code&gt;States&lt;/code&gt; collection, thus creating a relationship not unlike a primary-key/foreign-key relationship in a database.&amp;#160; In fact, it actually does correlate to just such a relationship as you can see when bringing up the T-SQL preview:     &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="Intellipad" border="0" alt="Intellipad" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/MisforM_DED8/image12.png" width="520" height="297" /&gt; &lt;/p&gt;  &lt;p&gt;The preview shows how the model will be instantiated within the SQL Server repository.&amp;#160; When using Intellipad you can see the SQL dynamically updated in this view as the M model is modified, thus giving you some insight into how the model is translated into relational database concepts.&lt;/p&gt;  &lt;p&gt;To load the model into the repository, I first compile the model into a binary M &lt;em&gt;image&lt;/em&gt; file using the M compiler, which ships with the SDK.&amp;#160; The &lt;code&gt;m&lt;/code&gt; command has a number of additional switches as well which can be used to verify syntax, generate T-SQL scripts, and reference metadata from other image files.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="M command line" border="0" alt="M command line" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/MisforM_DED8/image31.png" width="528" height="169" /&gt; &lt;/p&gt;  &lt;p&gt;   &lt;br /&gt;Now that I have an image file, &lt;code&gt;univ.mx&lt;/code&gt;, I can use the &lt;code&gt;mx&lt;/code&gt; command to load the model into the repository.&amp;#160; The command below loads the metadata and model into the default server and the tempdb database, but there are &lt;a href="http://msdn.microsoft.com/en-us/library/dd129654(VS.85).aspx"&gt;additional command-line parameters&lt;/a&gt; available to specify the destination.&amp;#160; &lt;code&gt;mx&lt;/code&gt; can also be used to export an image file from an existing schema in the database. &lt;/p&gt;  &lt;p&gt;&amp;#160; &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="MX command line" border="0" alt="MX command line" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/MisforM_DED8/image25.png" width="526" height="169" /&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px 0px 0px 20px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="View of Repository" border="0" alt="View of Repository" align="right" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/MisforM_DED8/image_10.png" width="249" height="162" /&gt;Using SQL Server Management Studio, I can check out the schema that was added to the the database.&amp;#160; In this case, I used the basic &lt;code&gt;TSQL10&lt;/code&gt; transformation option (specified via the target parameter of the M compiler).&amp;#160; If I had used the &lt;code&gt;/target:Repository&lt;/code&gt; option to create the image file, additional views and triggers would have been created to support the Oslo repository design pattern.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h1&gt;MGraph&lt;/h1&gt;  &lt;p&gt;So, now that I have a model in the repository, I need to add some data.&amp;#160; To do so I’ll use MGraph inside of Intellipad to populate a couple of states as well as the universities I’ve attended.&amp;#160; Here you can see the SQL statements, namely INSERTs, that are generated from the model.    &lt;br /&gt;&amp;#160; &lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="Intellipad" border="0" alt="Intellipad" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/MisforM_DED8/image_14.png" width="519" height="348" /&gt; &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;To update the repository, I’ll again use the &lt;code&gt;m&lt;/code&gt; and &lt;code&gt;mx&lt;/code&gt; commands:&lt;/p&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="m and mx command line" border="0" alt="m and mx command line" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/MisforM_DED8/image_20.png" width="515" height="219" /&gt; &lt;/p&gt;  &lt;p&gt;As a result, I now have data populated in my repository, as you can see from the results of the select query in SQL Server Management Studio below.&lt;/p&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="" border="0" alt="" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/MisforM_DED8/image_23.png" width="518" height="287" /&gt; &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h1&gt;MGrammar&lt;/h1&gt;  &lt;p&gt;While I was able to populate my model with data, the format of that data in MGraph was a bit obtuse.&amp;#160; Actually, it looks a lot like JavaScript Object Notation (JSON), and not something that really connotes the &lt;em&gt;domain&lt;/em&gt; of the data.&amp;#160; What I’d like to be able to do is define a grammar that’s a bit more user friendly (read: no curly braces and commas!) to people that have domain knowledge but not necessarily implementation knowledge (e.g., analysts versus developers).&lt;/p&gt;  &lt;p&gt;In the past, many of us have written grammars in &lt;a href="http://en.wikipedia.org/wiki/Backus-Naur_form"&gt;Backus-Naur Form (BNF)&lt;/a&gt; and used tools like &lt;a href="http://en.wikipedia.org/wiki/Lex_programming_tool"&gt;lex&lt;/a&gt; and &lt;a href="http://en.wikipedia.org/wiki/Yacc"&gt;yacc&lt;/a&gt; to define grammars and parsers to accomplish a similar goal.&amp;#160; Here I’ll use MGrammar.&lt;/p&gt;  &lt;p&gt;First, though, I’ll create a file that provides examples of my DSL, for instance, something like the following.&amp;#160; Note, the data really isn’t important at this stage.&lt;/p&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="DSL sample" border="0" alt="DSL sample" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/MisforM_DED8/image_26.png" width="524" height="96" /&gt; &lt;/p&gt;  &lt;p&gt;In Intellipad, I next create the start of my grammar by creating a module definition and saving it as a file with the &lt;code&gt;mg&lt;/code&gt; extension.&amp;#160; That extension indicates my intent to Intellipad to create a grammar, allows me to select the MGrammar Mode&amp;gt;Tree Preview to browse to my example file, and splits my Intellipad view into four panels&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;my example, &lt;/li&gt;    &lt;li&gt;the MGrammar specification, &lt;/li&gt;    &lt;li&gt;a preview of the syntax tree for my grammar, and &lt;/li&gt;    &lt;li&gt;an errors view. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Intellipad in MGrammar mode" border="0" alt="Intellipad in MGrammar mode" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/MisforM_DED8/image_29.png" width="526" height="197" /&gt; &lt;/p&gt;  &lt;p&gt;With the example and grammar specification side-by-side, you can tweak either to arrive at the desired results.&amp;#160; The third pane of the right provides a representation of how your grammar will parse the DSL into your domain model.&amp;#160; This particular grammar could use a bit more tweaking, for instance, note the extraneous quotation marks.&amp;#160; That’s a bit beyond the scope of a blog post though, so for more information, I refer you to the links at the end of the article, and in particular to Shawn Wildermuth’s series on &lt;a href="http://msdn.microsoft.com/en-us/library/dd441702.aspx"&gt;&lt;em&gt;Textual Domain Specific Languages for Developers&lt;/em&gt;&lt;/a&gt;.&amp;#160; &lt;/p&gt;  &lt;p&gt;Now that I have a grammar (the &lt;code&gt;.mg&lt;/code&gt; file), I can create an &lt;code&gt;mgx&lt;/code&gt; file from it and use that image file to convert other instances of my DSL into MGraph files.&amp;#160; In other words, I’m using the grammar file to translate files written in my DSL to the underlying MGraph format, which then allows them to be added into the repository just as I did before.&amp;#160; The difference now though is that the source document is in my domain-specific language, not one with curly braces and commas.&lt;/p&gt;  &lt;p&gt;So assume I have a document (&lt;code&gt;unc.txt&lt;/code&gt;) with the following line:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;code&gt;&amp;quot;University of North Carolina&amp;quot; in &amp;quot;Chapel Hill&amp;quot;,        &lt;br /&gt;&lt;/code&gt;&lt;code&gt;&amp;quot;North Carolina&amp;quot; has 28136 students.&lt;/code&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Using the &lt;code&gt;mg&lt;/code&gt; and &lt;code&gt;mgx&lt;/code&gt; command line utilities, I can transform my file in the DSL to an MGraph format:&lt;/p&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="m and mgx command line" border="0" alt="m and mgx command line" src="http://blogs.msdn.com/blogfiles/jimoneil/WindowsLiveWriter/MisforM_DED8/image_32.png" width="526" height="151" /&gt;&lt;/p&gt;  &lt;p&gt;At this point, I’m at the same place I was in the MGraph section above where I used the &lt;code&gt;m&lt;/code&gt; and &lt;code&gt;mx&lt;/code&gt; commands to generate the requisite SQL statements to update my model in the repository.&amp;#160; Note, here I will have some consistency problems though, because of the extraneous quotation marks delimiting my state values.&amp;#160; In the new record for the University of North Carolina, the state will be interpreted as &amp;quot;North Carolina&amp;quot;, with quotes, whereas the current database has the quote-less version.&amp;#160; The remedy here, of course, is to not gloss over the need for tweaking the grammar as I did a few paragraphs ago ;)&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h1&gt;Wrap-up&lt;/h1&gt;  &lt;p&gt;This posting turned out to be much more of a tome than I expected, and we haven’t even talked about how to ‘execute’ the model!&amp;#160; If you’re left feeling a bit confused, and wondering how and why all these steps are a good thing, don’t despair.&amp;#160; The “Oslo” concept is really in its nascent stages, and we can expect the tooling and methodologies to be refined and streamlined as the technology matures.&amp;#160; If you like to be on the bleeding edge, then you may already have tinkered around with Oslo and its related technologies, if not, and you have ‘real work’ to do, then just put it on your radar and check back from time to time to keep your finger on the pulse of where Microsoft is going with this model-driven development paradigm.&lt;/p&gt;  &lt;p&gt;To further whet your appetite, here’s some more information on Oslo and M:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=f2f4544c-626c-44a3-8866-b2a9fe078956&amp;amp;displaylang=en"&gt;Oslo SDK&lt;/a&gt; (January 2009 CTP) &lt;/li&gt;    &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/oslo/default.aspx"&gt;Oslo Developer Center&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/dd129870.aspx"&gt;MGrammar in a Nutshell&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/dd219521.aspx"&gt;Oslo Technical Articles&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;Blog: &lt;a href="http://blogs.msdn.com/mlanguage"&gt;Oslo Modeling Language Team&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;MSDN Magazine:&amp;#160; &lt;a href="http://msdn.microsoft.com/en-us/magazine/dd419662.aspx"&gt;Build Metadata-Based Applications with the “Oslo” Platform&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;Blog posting: &lt;a href="http://dvanderboom.wordpress.com/2009/01/17/why-oslo-is-important/"&gt;Why Oslo is Important&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9517782" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jimoneil/archive/tags/A-to-Z/default.aspx">A-to-Z</category></item></channel></rss>