Wednesday, March 26, 2008

If you've been following my whirlwind tour of C# on Channel 9, with episode 4 posted I've now covered the features that I consider to be the major ones — the ones that have the biggest impact on the way we write code. There are other features that were also introduced in C# 2.0, and it is only fair to point out what I left out.

The definitive reference for 2.0 features is the article What's New in the C# 2.0 Language and Compiler, in the Visual C# Getting Started, in the MSDN Library. The topics I opted to not cover are:

Okay, your curiosity should have gotten the better of you by now. Go look up covariance and contravariance in delegates. Then you'll be able to write better code and impress your friends with sesquipedalian verbiage.

Wednesday, March 26, 2008 4:48:58 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 

My fourth screencast on modern C# language features is posted on Microsoft's Channel 9 — Whirlwind 4: What's new is C# 2 - Accessors, Static Classes, Nullable Types (9:40). This installment wraps up the major C# 2.0 features, so next time we'll be jumping into the C# 3.0 goodness.

In addition to a few resources on today's topics, I've also provided some code showing a sample of using accessor visibility and a static class. And for the careful listener, I  have some small corrections to the screencast.

Resources

Asymmetric Accessor Accessibility, C# Programming Guide, MSDN Library

Access Modifiers, C# Programming Guide, MSDN Library

Static Classes and Static Class Members, C# Programming Guide, MSDN Library

static, C# Language Reference, MSDN Library

Nullable Types, C# Programming Guide, MSDN Library

?? Operator, C# Language Reference, MSDN Library

Nullable<T> Generic Structure, .NET Framework Class Library, MSDN Library

C# Whidbey Featurette #3: Static classes, blog post by Eric Gunnerson, C# program manager. Eric provides some justification for the static class in C# 2.0.

Get a Charge From Statics with Seven Essential Programming Tips, K. Scott Allen, MSDN Magazine, June 2005.

Nullable types in C#, blog post by Eric Gunnerson, C# program manager.

Create Elegant Code With Anonymous Methods, Iterators, And Partial Classes, Juval Löwy, MSDN Magazine, Visual Studio 2005 Guided Tour issue, 2006 (Vol. 21, No. 3)

Code sample

Here is a sample that illustrates use of accessor visibility and static classes. 

using System;
using System.Collections.Generic;

namespace CSharpWhirlwind4
{
 
public class Animal
  {
   
private string name;
   
public string Name
    {
     
get { return name; }
     
private set { name = value; }  // restrict accessor visibility
    }

   
// private constructor
    private Animal( string name ) { Name = name; }

   
// nested static class, manages Animal instances
    public static class ClassFactory
    {
     
private static Dictionary<string, Animal> animals
        =
new Dictionary<string, Animal>();

     
public static Animal Create( string name )
      {
       
if ( !animals.ContainsKey( name ) )
          animals[ name ] =
new Animal( name );
       
return animals[ name ];
      }
    }
  }

 
// ...
}

The Animal class has a Name property which is marked with public visibility. Within the Name property, the set accessor is declared with private visibility, so that only members of the Animal class can set the Name property.

The Animal constructor is also declared with private visibility, so Animal cannot be instantiated except by members of the Animal class. The constructor uses the private set accessor on the Name property.

Inside of the Animal class, there is a nested class named ClassFactory which is responsible for managing Animal instances. The developer's intention is that Animal.ClassFactory should only contain static members, and therefore never be instantiated. That is indicated by declaring the class to be static.

imageTake a look at the resulting assembly in ildasm. The ClassFactory class (.class) is marked as abstract   and sealed. As an abstract class, it cannot be instantiated. And because it is sealed, it cannot be inherited by another class. Also note that the static ClassFactory has a default class constructor (.cctor), which is in fact permitted on static classes since that is a static member.

Since ClassFactory is a nested class in the Animal class, it is a member of the Animal class, and so has access to Animal's private constructor.

In this example, the ClassFactory ensures that no more than one instance of an Animal of a given name is created, as illustrated here.

Animal w1 = Animal.ClassFactory.Create( "wombat" );
Animal w2 = Animal.ClassFactory.Create( "wombat" );
Debug.Assert( w1 == w2 );   // two references to the same object

Errata

The devil is always in the details.

At 1:31, in a bout of overly excessive exuberance, on accessor visibility I said  you can change the visibility of "one, the other, or both" accessors.  Every word of that is true... except for the "or both" part.

At the end of the section on static classes (4:17) I misspoke when I said the static class "exists only because it has private members." Pretend you really heard me say it "exists only because it has static members."

Previous episodes

Wednesday, March 26, 2008 3:12:59 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 
 Monday, March 24, 2008

Last year we put together a team of folks from Corillian, Arcot, Wachovia, and the Microsoft CardSpace team and jointly created a proof-of-concept demo for a user logging into an online banking application using Microsoft CardSpace. I delivered the demo in the Microsoft booth at the RSA 2007 Conference for a whole week, and we had a lot of traffic since the technology was featured in Bill Gates's keynote address at the start of the conference.

Thanks to CardSpace team member Nigel Watling, you can view the demo on Microsoft's Channel 9. We designed the demo so we could use it to tell many different stories. This is one of them.

Monday, March 24, 2008 2:31:57 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 

I am looking forward to reading Bob Uva's new technical blog, http://bobdotnet.wordpress.com. Bob is a friend and colleague at CheckFree, and he's particularly keen on sharing his impressions of WCF.

Monday, March 24, 2008 11:14:40 AM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 
 Sunday, March 23, 2008

image My whirlwind tour of C# features continues with Whirlwind 3: What's new in C# 2 - Partial types, anonymous methods on Microsoft's Channel 9.

Partial types are quick and easy: split class, struct, and interface definitions across multiple files. I don't cover them in the screencast, but in addition to partial types there are also partial methods, see the resources for details.

Anonymous methods are more involved: pass a block of code inline anywhere a delegate is expected. It's a good idea to understand this concept, since lambda expressions in C# 3.0 build on anonymous methods. You'll see that in a future whirlwind episode.

Resources

Partial Classes and Methods, C# Programming Guide, MSDN Library

partial (Type), C# Reference, MSDN Library

partial (Method), C# Reference, MSDN Library

Anonymous Methods, C# Programming Guide, MSDN Library

Delegates, C# Programming Guide, MSDN Library

delegate, C# Reference, MSDN Library

Create Elegant Code With Anonymous Methods, Iterators, And Partial Classes, Juval Löwy, MSDN Magazine, Visual Studio 2005 Guided Tour issue, 2006 (Vol. 21, No. 3)

Introduction to C# Anonymous Methods, Patrick Smacchia, TheServerSide.NET

Fun with Anonymous Methods, blog post by Brad Adams. The mischievous kind of fun.

Closures and Continuations, blog post by Don Box.

Previous episodes

Sunday, March 23, 2008 9:45:02 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 
 Friday, March 21, 2008

Don Box and Charles Torre interviewed She-Who-Watches-Microsoft in All About Mary Jo on Channel 9. The informal and slightly irreverent video was filmed backstage at the Lang.NET 2008 conference.

If you're not familiar with her prodigious body of work, Mary Jo Foley has been writing All About Microsoft for ZDNet since September 2006. And prior to that gig she wrote Microsoft Watch for eWeek for eleven years.

In the interview, Don's unrelenting pursuit of the truth and subtle sleuthing uncovers that Mary Jo has written a new book, Microsoft 2.0, due out later this spring. It purports to predict what Microsoft AB (After Bill) will look like.

Note to Don about your new career interests: you really do seem better suited for the sensitive architect role than investigative journalism. I am not saying don't follow your dream, but in the meantime keep your day job.

Friday, March 21, 2008 6:54:07 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 

PDC 2008 I keep getting asked this question, so I bet others are asking as well.

Yes, the dates for PDC08 have been announced: 27 – 30 October 2008 in Los Angeles, California. Save the date, and clear the time with your boss, spouse, entourage coordinator, indoor Ultimate Frisbee league, and anyone else who normally has a lock on your coordinates.

No, no other details have been announced yet. I think that Microsoft focused on  TechEd 2008 at the moment.

But you can subscribe to the PDC event RSS feed so that updates are delivered straight to you.

Friday, March 21, 2008 10:37:49 AM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 
 Wednesday, March 19, 2008

The talks from the 2008 Lang.NET Symposium (28 – 30 January 2008) have been published for your viewing pleasure. The Talks page lets you choose between viewing in the browser using Silverlight, or a viewing .wmv file. If you choose the Silverlight option, hover your mouse near the bottom of the video for playback controls.

I sing the praises of Jimmy Schementi who put them up earlier without realizing the demand for them, and incurred the wrath of his ISP for bogarting the bandwidth. Now the talks are back, and some initial link boo-boos appear to be all better now.

Ted Neward already posted his copious highlights of the symposium (day one, day two, and day three), as well as list of his favorite videos. Ted spoke at Lang.NET on Scala.

Wednesday, March 19, 2008 9:48:55 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 

Whirlwind 2: What's new in C# 2 - Iterators My next whirlwind C# tour screencast is up. Whirlwind 2: What's new in C# 2 - Iterators is now on Channel 9 for your viewing pleasure. In just nine minutes you can learn what iterators are and what problem they address. Get down. Get funky.

Resources

Iterators, C# Programming Guide, MSDN Library

yield, C# Reference, MSDN Library

Create Elegant Code With Anonymous Methods, Iterators, And Partial Classes, Juval Löwy, MSDN Magazine, Visual Studio 2005 Guided Tour issue, 2006 (Vol. 21, No. 3)

Fun with Iterators and state machines, Under The Hood - Matt Pietrek (blog). Focus on the cool compiler and runtime magic that makes iterators possible. Based on prereleased Whidbey bits, but you get the idea.

Iterators with C#2, Patrick Smacchia, TheServerSide.net.

Using C# 2.0 iterators to simplify writing asynchronous code part 1 and part 2, Michael Entin's notebook (blog). Stretch your mind: using iterators to implement the .NET asynch pattern.

Previous episodes

Whirlwind 1: What's new in C# 2 - Generics (notes)

Wednesday, March 19, 2008 5:00:00 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 

image Erik and Monica Mork are rocking and rolling on their new Silverlight podcast, SparklingClient.com. The challenge, of course, is to relate a rich graphical user experience (UX), and developer and design experiences, in an audio podcast. Erik and Monica are Portland area developers, and enthusiastic about their topic.

Erik went to MIX08 for the Silverlight 2 Beta 1 release. Download the bits, and the tool, and get started. Then let Sparkling Client be your guide.

Wednesday, March 19, 2008 3:51:06 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 
 Tuesday, March 18, 2008

Get your hands on slides, code samples, and whatnots from the Boise Code Camp 2008 sessions. My session just points back to posts on this blog: no surprises there. That is all.

Tuesday, March 18, 2008 4:01:38 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 

The Portland Adobe Developer User Group is hosting Mike Culver, Amazon Web Services Evangelist, on Thursday, 20 March 2008 at 6:00 PM, speaking on "What's Possible in a Post-Web 2.0 World?" at PCC Sylvania, Library Room 112. Networking begins at 5:30 PM.

Innovation continues at a mind-bending pace, and this presentation will showcase some thought-provoking new ideas built on Web Services. You will also learn how others, empowered by technology advances—known as “Web Scale Computing”—created businesses that weren’t practical until recently... (More...)

Amazon has done phenomenal things with Amazon Web Services (AWS), so this ought to be great.

See the Upcoming Meetings for the Portland Adobe Developer User Group for additional details.

Tuesday, March 18, 2008 2:26:07 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  |