Saturday, November 15, 2008

Well that's good news: Server Core changes in Windows Server 2008 R2

Server Core will soon allow ASP.NET and PowerShell to be installed as options. They've partitioned the .NET framework and only install those parts that are needed (presumably to remove those parts that depend on the UI). I haven't seen any details, but I expect that things in the System.Drawing space may not be there either.

This is good news for people with a farm of web servers, and also for those that want better management of their server core installs.

Kirk

posted on Saturday, November 15, 2008 8:45:06 AM (New Zealand Standard Time, UTC+12:00)  #    Comments [0]
 Thursday, November 06, 2008

Hi,

Many thanks to those who came along to our PDC Fireworks session last night. I hope you had an interesting time hearing about what's coming on the horizon, and that your families enjoyed meeting your geek friends!
A huge thank you to our speakers who presented on such a wide range of topics in a limited time. If you want to find out more or watch any sessions from the PDC, you could visit the Channel 9 PDC site, or visit the blogs of the presenters:

Also, a big thanks to those who helped set up and tidy up the room. Great job!

The .NET user group meets once or twice monthly, usually on the first and third Wednesday of the month. If you'd like to go on my mailing list for upcoming user group presentations, please email me.

If you're interested in the Wellington Sharepoint User Group, or the Wellington SQL User Group, make sure you follow the links to sign up to them.

Upcoming Sessions:

There's a few events coming up in Wellington that are free to attend:

Wed 19 Nov, 6pm - Microsoft .NET Services with Chaks Chandran [.NET User Group]

Microsoft .Net Services is a part of the recently announced Microsoft Azure Platform. What does Microsoft .Net Services provide ?

Microsoft .Net Services consists of three main components:

  1. Service Bus
  2. Access Control
  3. Workflow Services

They provide a hosting platform where you can develop connected, peer to peer applications that can speak to each other without considering the other complexities such as firewall rules and NAT etc,.

Venue: Xero, Level 1, 98 Customhouse Quay, Wellington

Please RSVP to kirkj@paradise.net.nz

Thurs 13 Nov, 1:10-5pm - MSDN Unplugged [Microsoft]

This free half day event, organised by Microsoft, sees JD Trask and myself talking on WPF, Silverlight and Visual Studio. More info here.

Sat 6 & Sun 7 Dec - SQL Pass Community Connection [NZ User Groups]

A free, all day event focussed on SQL and related technologies. Overseas and NZ speakers. Find out more at the event website.

Wed 3 Dec, 6pm - Overcoming your web insecurity [.NET User Group]

In this hour-long session, I'll be introducing you to some of the threats your ASP.NET website will face, and give you ideas on how to protect your users, company and yourself. This applies to public facing and internal (intranet) applications.

RSVP details will be sent out to the announcement list closer to the date.

 

Thanks for reading this far!

Kirk

posted on Thursday, November 06, 2008 9:47:02 AM (New Zealand Standard Time, UTC+12:00)  #    Comments [0]
 Friday, October 31, 2008

Reading about how dynamic is implemented in C# 4.0 over on Sam Ng's blog: Dynamic in C#

How did they implement the new dynamic 'keyword' in C# 4.0? Using the DLR, of course!

Very cool to see that the C# version of dynamic dispatch is implemented over the same mechanisms for call actions, dynamic objects and binders to generate expression trees, with some C# specifics in the binder to implement C#'s rules for dispatch etc.

It's interesting to see the flow-on effects of using a dynamic type in an expression and how the 'dynamism' then flows on to subsequent expressions or invocations involving the resulting object.

Quite a nice bit of work, and well explained by Sam. He's going to post more in the series, so worth subscribing to his blog.

Kirk

posted on Friday, October 31, 2008 12:06:10 AM (New Zealand Standard Time, UTC+12:00)  #    Comments [0]
 Tuesday, October 28, 2008

As linked to by the C# Futures site, here are the four main groups of C# 4.0 proposed features:

Dynamic lookup

Dynamic lookup allows you to write method, operator and indexer calls, property and field accesses, and even object invocations which bypass the C# static type checking and instead gets resolved at runtime.

Named and optional parameters

Parameters in C# can now be specified as optional by providing a default value for them in a member declaration. When the member is invoked, optional arguments can be omitted. Furthermore, any argument can be passed by parameter name instead of position.

COM specific interop features

Dynamic lookup as well as named and optional parameters both help making programming against COM less painful than today. On top of that, however, we are adding a number of other small features that further improve the interop experience.

Variance

It used to be that an IEnumerable<string> wasn’t an IEnumerable<object>. Now it is – C# embraces type safe “co-and contravariance” and common BCL types are updated to take advantage of that.

Most of these features are similar in spirit to what was shown at the Lang.NET symposium, but with a few details worked out.

The Dynamic features are going to be great when dealing with objects from DLR languages or COM. Named parameters sounds kind-of nice, but not world-changing. Variance support when assigning collections is going to be very handy, so is probably my favourite of the four feature groups.

Read more at the C# Futures site.

Kirk

posted on Tuesday, October 28, 2008 2:41:46 PM (New Zealand Standard Time, UTC+12:00)  #    Comments [0]
 Sunday, October 26, 2008

This post shows how to create an Automatic Property, by turning an existing private field into a property.

 

You've got a private field that you would like to expose to users of your class, but you only want them to be able to get, not set the value.

You could expose the field itself to callers, but it's better to expose a property so that you can set different permissions for get and set, add behaviour when the value is set, and make it easy to version your class so that callers won't notice when you change your internal features.

For this walk-through, I'll assume you've already got a private field on your class that you want to expose publicly.

Encapsulating Fields

So starting with your private field, that has no public access:

class TestClass

{

    private int _Property1;

}

You can use the refactoring support in Visual Studio's right-click menu to wrap the field with a property:

Right-click field, and choose encapsulate field

Go ahead and select the "All" option to update all usages of the _Property1 to use the new property we are creating:

EncapsulateField2

Now all code will reference the Property1 property, rather than the private field:

class TestClass

{

    private int _Property1;

 

    public int Property1

    {

        get { return _Property1; }

        set { _Property1 = value; }

    }

}

Our final requirement was that Getting the value should be public, but the setter should be private, so external callers can't update our fields value.

You can do this by overriding the access of the setter, to make it more restricted:

class TestClass

{

    private int _Property1;

 

    public int Property1

    {

        get { return _Property1; }

        private set { _Property1 = value; }

    }

}

Note the addition of the private keyword to the property setter, while the public keyword now will just apply to the getter.

Automatic Properties

C# 3.0 added a handy new feature called Automatic Properties.

If all your accesses to a field (e.g. _Property1) are going through a property (Property1), then it seems like a waste to have to create a private field by hand.

Automatic Properties in C# will let the compiler do the work of creating the private field for you. All you have to do is create the property.

Change the code to:

class TestClass

{

    public int Property1

    {

        get;

        private set;

    }

}

If you have no body for the getter and setter, the C# compiler will automatically create a field to store the value in. The accessibility of the setter can be set to private as above, so you have all the benefits of a private field, without having to create it yourself.

Now external callers can only reference the property Property1, while internal callers within your class can access both the getter and setter.

By following the above sequence of encapsulating, you won't have to manually change any of the callers of your original field to use the property:

  1. Encapsulate field
  2. Update all references to point to the new field
  3. Remove the original field, and the contents of the getter and setter
  4. Update access of the getter and setter as desired (e.g. make set private)

Snippets

If you're starting from scratch, and you don't already have a field to encapsulate, there are a couple of handy snippets you can use:

prop [tab] [tab] - this will create a regular automatic property, and let you set the type and name:

public int MyProperty { get; set; }

propg [tab] [tab] - this will do the same, but with a private setter:

public int MyProperty { get; private set; }

 

I hope this post gives you some quick tips when dealing with properties.

Kirk

 

Extras

It's always tempting to go off on a tangent talking about technicalities. If you're interested in such stuff, read on...

Field names for automatic properties

The compiler will choose a "magic" name for your field that is storing the data exposed by the automatic property. You can't refer to it directly from your code, and the compiler will make sure it's unique.

For example, my "Property1" has the following fieldname chosen by the compiler:

private int <Property1>k__BackingField;

You can see how it can be guaranteed to be unique -- it uses characters that are not allowed in C#!

Encapsulation

Why is it nicer to use a property than a field? There's a few reasons that I listed above: different permissions for get and set, add behaviour when the value is set, and make it easy to version your class so that callers won't notice when you change your internal features.

Accessibility

If you expose a field as public, you can't restrict your callers from updating it's value. A property can be made public for get, and private for set, which makes it easy to expose data, but still encapsulate it within your class.

Behaviour

Sometimes you'll want to do some validation checks when a property is set. For example, you might want to check that Property1 is never set to 0:

set {

   if (_Property1 != 0)

      _Property1 = value;

}

If you use a property, then you have the ability to add this logic inside the get or set method -- with a field that's not possible.

Versioning

If you expose a public field to callers, changing the name or data type of that field breaks the contract your class has with it's callers, and they will all need to be updated.

With a property, you can change the internals of your class, and where it stores the data, without affecting the caller of your property -- as long as the property has the same name, type and accessibility, they won't know anything has changed.

This helps with versioning your class, as changes to your class won't affect the rest of your program.

 

Cheers,

Kirk

 

Previous C# tips:

And other Visual Studio tips:

posted on Sunday, October 26, 2008 8:45:38 AM (New Zealand Standard Time, UTC+12:00)  #    Comments [0]
 Thursday, October 23, 2008

A common scenario is wanting to delete an item while iterating over a collection using foreach:

foreach (string name in names)

{

    if (name == "Kirk")

    {

        names.Remove(name);

    }

}

Unfortunately, this will give the dreaded InvalidOperationException:

Collection was modified; enumeration operation may not execute.

This is because modifying the state of a collection invalidates the enumerator that foreach uses behind the scenes to loop over the collection.

A common work-around is to convert the foreach to a for loop:

for (int i = names.Count - 1; i >= 0; i--)

{

    if (names[i] == "Kirk")

    {

        names.RemoveAt(i);

    }

}

You'll notice that the for loop goes backwards from the end of the list (position: Count - 1) back to the start (position: 0), so that when an item is removed, our current index i is still a valid position in the list.

How do you do it?

Kirk

Source (such that it is) Program.txt

posted on Thursday, October 23, 2008 11:08:06 PM (New Zealand Standard Time, UTC+12:00)  #    Comments [0]
 Thursday, October 02, 2008

Cool to see that Amazon will be offering Windows pay-as-you-go EC2 instances:

Coming soon: Amazon EC2 with Windows

It will be good to have another option for running SQL and ASP.NET solutions out in the cloud.

posted on Thursday, October 02, 2008 9:14:13 PM (New Zealand Standard Time, UTC+12:00)  #    Comments [1]