December 2008 - Posts
MMayerl asks over on the MSDN wiki, why we don’t document the undocumented C# keywords __makeref, __reftype, __refvalue and __arglist?
Because that defeats of the purpose of them being undocumented.
They are undocumented for a reason; so that the C# team is free to change and remove these keywords without worrying whether customers have taken dependencies on them. Documenting them conveys an implicit support agreement between Microsoft and the user, thereby preventing the team from ever changing the keywords.
We want less people to use them, not more.
I’ve seen recent reports of Visual Studio Code Analysis failing (such as this one) with the following error:
Invalid settings passed to CodeAnalysis task. See output window for details.
Looking in the output window will show something similar to:
Switch '/targetframeworkversion' is an unknown switch.
As I’ve mentioned earlier, the /targetframeworkversion switch was added in Visual Studio 2008 SP1 to support the new multi-targeting rule. The fact that the FxCop does not understand the switch, is a big indicator that something went astray with the installation of the service pack.
Luckily the fix is rather easy, simple re-install it.
This question came up the other day:
How do I return an empty IEnumerable<T> from a method using the C# yield keyword?
There are actually two ways of doing this. Both, admittedly, are not the most intuitive things in C#:
- yield break
- Do not return anything
yield break terminates the iterator. For example, the following iterator returns an IEnumerable<string> containing a single element ‘Value’.
public static IEnumerable<string> GetEnumerator(bool condition)
{
if (condition)
{
yield return "Value";
}
yield break;
}
That same method above can actually be rewritten without even specifying yield break and simply not returning anything outside of the condition:
public static IEnumerable<string> GetEnumerator(bool condition)
{
if (condition)
{
yield return "Value";
}
}
This is perfectly legal; as long there is at least one yield return in a method, the C# compiler will not require you to return a value on all code paths.
One of the problems that we’re facing today in the Framework, mainly stemming from the red bits/green bits design of .NET 3.5, is that certain ‘core’ types are located in the wrong assemblies.
For example, the extremely useful ObservableCollection<T> and ReadOnlyObservableCollection<T> types both live in WindowsBase.dll, which is a part of the Windows Presentation Foundation. For low-level assemblies (such as my team’s very own System.ComponentModel.Composition.dll), this means that to use types such as these, they need to take a reference to assemblies either at a higher level to themselves, or to technologies outside of their own. This is in turn leads to scary dependencies, such as the server assembly System.Web.dll’s current direct dependency on the client assembly System.Windows.Forms.dll in .NET 2.0.
Going forward for .NET 4.0, the Framework Architecture team will be leading an effort (being pushed by my colleague Mitch Trofin) to move these and other core types to lower-level assemblies such as mscorlib.dll and System.dll.
Now, as I’ve mentioned previously, Microsoft takes compatibility very seriously, so you might be thinking ‘how can they move these types without runtime breaking changes’?
Luckily, there’s already a solution for this; type forwarding. Introduced in .NET 2.0, type forwarding allows assembly writers to move types into other assemblies without needing existing consumers to recompile their applications. This done via the TypeFowardedToAttribute.
To move a type, do the following:
- Move the type from the original assembly to the assembly that will become the type’s new home.
- In the original assembly, add a reference to the new assembly if it does not already have one.
- Apply the following assembly-level attribute to the original assembly, replacing MyType with the type you moved in step 1:
[assembly: TypeForwardedToAttribute(typeof(MyType))]
- Compile both assemblies and that’s it.
A couple of things to note:
- The type must have the same name and namespace in the target assembly.
- This is considered a source breaking change; that is, users recompiling their application against the new versions of the assemblies, will be required to add a reference to the assembly that now contains the type.
- While fully supported by C# and C++/CLI, Visual Basic does not support moving types using the TypeForwardedToAttribute. It does, however, support consuming assemblies written by other languages that have had their types moved.
- Starting in .NET 4.0, a new attribute called TypeForwardedFromAttribute should be applied to types that have been forwarded indicating which assembly they came from. This allows runtime serialization to serialize these types correctly when interoping with previous versions of these assemblies.
Please read this first: About Dave’s ‘unofficial’ Framework Design Guidelines.
þ DO treat a null reference (Nothing in Visual Basic and nullptr in C++/CLI) and an empty collection or IEnumerable<T> as equivalent.
For example, the following constructor treats both a null an empty IEnumerable<string> instance similar; they both result in an empty StringCollection.
public class StringCollection : Collection<string>
{
public StringCollection(IEnumerable<string> items)
{
if (items != null) // Correct
{
foreach (string item in items)
{
Add(item);
}
}
}
}
ý DO NOT return a null reference (Nothing in Visual Basic and nullptr in C++/CLI) from an array, collection or IEnumerable<T> property or method; return an empty instance instead.
Similar to string properties, consumers of your class expect to be to enumerate over the returned enumerable instance without also needing to check for null.
For example, in the following code, performing a foreach over the returned Collection<T> from Directory.GetFiles should never cause a NullReferenceException to be thrown.
void PrintDirectory(string path)
{
Collection<string> fileNames = Directory.GetFiles(path);
foreach (string fileName in fileNames)
{
Console.WriteLine(fileName);
}
}
þ DO throw ArgumentException if an element in collection or IEnumerable<T> argument contains a null reference (Nothing in Visual Basic and nullptr in C++/CLI) and null is not a valid value for an element. Do not filter or skip over these values.
For example, the following method correctly throws ArgumentException when the passed types IEnumerable<Type> instance contains a null element.
public void RegisterTypes(params Type[] types)
{
if (types == null)
throw new ArgumentNullException("types");
if (types.Length == 0)
throw new ArgumentException("'types' cannot be empty.");
foreach (string item in items)
{
if (item == null)
throw new ArgumentException("'items' cannot contain a null element", "items"); // Correct
[...]
}
}
þ CONSIDER providing an params array based overload in addition to an IEnumerable<T> based overload so that user can pass elements without having to explicitly create a temporary array.
For example, the following class has two constructors, one taking an array, and one taking an IEnumerable<T>.
public class TypeCollection : Collection<string>
{
public TypeCollection(params Type[] types) // Correct
: this((IEnumerable<Type>)types)
{
}
public TypeCollection(IEnumerable<Type> types)
{
[...]
}
}
þ CONSIDER providing an IEnumerable<T> overload in addition to an array based overload. This allows users to pass arguments typed as other enumerable sources other than arrays, such as List<T>.
ý AVOID looping over IEnumerable<T> arguments multiple times. IEnumerable<T> instances returned from LINQ-based and yield generated methods are not cached underneath, which could cause work done in the enumerator to be repeated multiple times if an argument is iterated more than once. This could lead to unexpected behavior and performance problems.
For example, the following shows the pitfalls of looping over IEnumerable<T> instance multiple times.
public void DeleteFiles(IEnumerable<string> fileNames)
{
if (fileNames == null)
throw new ArgumentNullException("fileNames");
if (fileNames.Count() == 0) // 1st Loop
throw new ArgumentException("'fileNames' cannot be empty.");
foreach (string fileName in fileName) // 2nd Loop
{
[...]
}
}
This could cause a performance problem given the method as follows:
void DeleteDocumentsWithBackups(string path)
{
string[] fileNames = Directory.GetFiles(path, ".doc");
// Get every document that already has a backup
IEnumerable<string> filesToDelete = fileNames.Select(fileName => File.Exists(fileName + ".bak"));
DeleteFiles(filesToDelete);
}
The lambda expression within the Enumerable.Select extension method will be executed twice for every file name found in the directory; once for Count and once for the foreach statement.
þ DO make a copy of an array or IEnumerable<T> argument if outside modification is not expected. As arrays and IEnumerable<T> instances can be modified after they have been passed to a method, make a clone of the argument if storing it and you want to prevent outside manipulation.
The following example uses the LINQ method, Enumerable.ToArray, to make a copy of the types argument.
public class TypeCollection : ICollection<Type>
{
private readonly Type[] _types;
public TypeCollection(params Type[] types)
: this((IEnumerable<Type>)types)
{
}
public TypeCollection(IEnumerable<Type> types)
{
if (types != null)
{
// Make a copy
_types = types.ToArray(); // Correct
}
}
}
Related Framework Design Guidelines Section: 8.3 Collections
Nick Blumhardt (of autofac fame) recently joined our team, and just fired up a new blog on blogs.msdn.com. Similar to Mitch (with whom he shares an office), he's another big fan of languages with unreadable syntax.
He's started off with part one in a series on integrating Ruby with the Managed Extensibility Framework. Check it out:
Hosting Ruby Parts in MEF
Jeremy Miller takes a dig at the BCL team with his post, I love Ayende (and OSS):
From one of Ayende's many OSS projects.
/// This class actually already exists in the System.Core assembly...as an internal class.
/// I can only speculate as to why it is internal, but it is obviously much too dangerous
/// for anyone outside of Microsoft to be using...
And dear MS BCL team, since so many people are already using Reflector to go get the ExpressionVisitor code, would you just make this public? 'K, thx.
This seems like an oversight that this wasn't public in first place. It would just be a couple minutes of a single developer's time to change the accessibility from internal to public, right?
Well, actually, it's a lot more complicated than that. There's very good reasons we don't make things public and virtual by default; Eric Lippert summarizes it pretty well with his post, How many Microsoft employees does it take to change a lightbulb?
- One dev to spend five minutes implementing ChangeLightBulbWindowHandleEx.
- One program manager to write the specification.
- One localization expert to review the specification for localizability issues.
- One usability expert to review the specification for accessibility and usability issues.
- At least one dev, tester and PM to brainstorm security vulnerabilities.
- One PM to add the security model to the specification.
- One tester to write the test plan.
- One test lead to update the test schedule.
- One tester to write the test cases and add them to the nightly automation.
- Three or four testers to participate in an ad hoc bug bash.
- One technical writer to write the documentation.
- One technical reviewer to proofread the documentation.
- One copy editor to proofread the documentation.
- One documentation manager to integrate the new documentation into the existing body of text, update tables of contents, indexes, etc.
- Twenty-five translators to translate the documentation and error messages into all the languages supported by Windows.The managers for the translators live in Ireland (European languages) and Japan (Asian languages), which are both severely time-shifted from Redmond, so dealing with them can be a fairly complex logistical problem.
- A team of senior managers to coordinate all these people, write the cheques, and justify the costs to their Vice President.
In addition to the excellent reasons that Eric lists (and the one that often gets forgotten) is compatibility. Despite what appears to be evidence to the contrary, the compatibility bar between versions of the Framework is extremely high. Every single public member and type and its behavior needs be maintained indefinitely. The more APIs that are public and/or virtual, the more baggage that needs to be brought forward into future Framework versions so as to not break compatibility.
Developing good APIs is extremely difficult, especially the first time around. When we get things wrong (and we do get things wrong), trying to maintain backwards compatibility often stifles innovation as we try to correct these scenarios. Unfortunately, unlike others, we don't have the luxury to make only 95% of our new versions backwards compatible with our previous versions.
It is for these very reasons, we try to make our extension points deliberate and designed.
Why does Type.GetMember return an array of members? It's description states:
Gets the specified members of the current Type.
Here's a hint; if you are going to return multiple things from a method or property, give it a plural name - it just makes sense.
Windows Vista comes with a new feature called Default Programs (which replaces the Set Program Access and Default and File Types feature from Windows XP). This cool new feature provides a user friendly way of changing program defaults, such as file and protocol associations and AutoPlay settings.
The recommendations going forward is that on Windows Vista or higher, applications use the new UI to allow users to customize file associations for an application instead of providing their own. This provides a consistent look and feel for every application and provides a single place for users to change these settings.
Unfortunately, to programmatically interact with this feature is not as straight forward as you might think from managed code if you've never done COM interop. The docs for these APIs leave a lot to be desired (why common return values from COM methods aren't called out on MSDN - I don't know) and there are, at the time of writing, no samples for calling them.
To save you most of the trouble, I've gone ahead and written a small wrapper around the IApplicationAssociationRegistrationUI::LaunchAdvancedAssociationUI method, which displays the file association window for a particular application.
To use this method, simply take a reference to the below attached project and write something like:
static void Main()
{
System.Windows.DefaultApplications.ShowAssociationsWindow("Internet Explorer");
}
The above example, will open the Set associations for a program window for Internet Explorer:
The name of the application to pass to DefaultApplications.ShowAssociationsWindow comes from one of the registry values under HKEY_LOCAL_MACHINE\Software\RegisteredApplications. Information on this key can be found at Registering an Application for Use with Default Programs.
For now, download the wrapper (System.Windows.DefaultApplications.zip) and tell me what you think.
For additional information on how to make your application play nicely with Default Programs, see http://msdn.microsoft.com/en-us/library/bb776873.aspx