Site: http://msmvps.com/blogs/jon_skeet/default.aspx Link: http://feeds.feedburner.com/JonSkeetCodingBlog
by skeet via Jon Skeet: Coding Blog on 2/10/2010 9:56:00 PM
(Edited on February 11th, 2010 to take account of a few mistakes and changes in the .NET 4.0 release candidate.) I've just been fiddling with the first appendix of C# in Depth, which covers the standard query operators in LINQ, and describes a few details of the LINQ to Objects implementations. As well as specifying which operators stream and which buffer their results, and immediate vs deferred execution, I've where LINQ optimises for different collection types - or where it doesn' ...
[ read more ]
by skeet via Jon Skeet: Coding Blog on 1/26/2010 5:48:00 PM
This morning, while checking out an email I'd received about my brain-teasers page, I discovered an interesting change to the CLR in .NET 4.0. At least, I think it's interesting. It's possible that different builds of the CLR have exhibited different behaviour for a while - I only have 32-bit versions of Windows installed, so that's what I'm looking at for this whole post. (Oh, and all testing was done under .NET 4.0b2 - it could still change before release.) Note: to try any ...
by skeet via Jon Skeet: Coding Blog on 1/16/2010 8:13:42 PM
I've been researching Reactive Extensions for the last few days, with an eye to writing a short section in chapter 12 of the second edition of C# in Depth. (This is the most radically changed chapter from the first edition; it will be covering LINQ to SQL, IQueryable, LINQ to XML, Parallel LINQ, Reactive Extensions, and writing your own LINQ to Objects operators.) I've watched various videos from Channel 9, but today was the first time I actually played with it. I'm half excited, and ...
by skeet via Jon Skeet: Coding Blog on 12/9/2009 6:04:47 PM
Warning: here be dragons. I don't think this is the right way to check for null arguments, but it was an intriguing idea. Today on Stack Overflow, I answered a question about checking null arguments. The questioner was already using an extension similar to my own one in MiscUtil, allowing code like this: public void DoSomething(string name) { name.ThrowIfNull("name"); // Normal code here } That's all very well, ...
by skeet via Jon Skeet: Coding Blog on 11/17/2009 7:31:48 AM
Disclaimer: I don't want this to become a flame war in the comments. I'm coming from a position of ignorance, and well aware of it. While I'd like this post to provoke thought, it's not meant to be provocative in the common use of the term. Chapter 14 of C# in Depth is about dynamic typing in C#. A couple of reviewers have justifiably said that I'm fairly keen on the mantra of "don't use dynamic typing unless you need it" – and that possibly I'm doing dynam ...
by skeet via Jon Skeet: Coding Blog on 11/4/2009 7:40:15 AM
Almost every Stack Overflow question which includes the words "random" and "repeated" has the same basic answer. It's one of the most common "gotchas" in .NET, Java, and no doubt other platforms: creating a new random number generator without specifying a seed will depend on the current instant of time. The current time as measured by the computer doesn't change very often compared with how often you can create and use a random number generator – so code whi ...
by skeet via Jon Skeet: Coding Blog on 11/2/2009 10:20:10 PM
(Meta note: I tried to fix the layout for this, I really did. But my CSS skills are even worse than Tony's. If anyone wants to send me a complete sample of how I should have laid this out, I'll fix it up. Otherwise, this is as good as you're going to get :) Last week at Stack Overflow DevDays, London I presented a talk on how humanity had made life difficult for software developers. There's now a video of it on Vimeo - the audio is fairly poor at the very start, but it improves ...
by skeet via Jon Skeet: Coding Blog on 10/31/2009 10:04:54 PM
I've just been going through some feedback for the draft copy of the second edition of C# in Depth. In the contracts section, I have an example like this: [ContractClass(typeof(ICaseConverterContracts))] public interface ICaseConverter { string Convert(string text); } [ContractClassFor(typeof(ICaseConverter))] internal class ICaseConverterContracts : ICaseConverter { string ICaseConverter.Convert(string text ...
by skeet via Jon Skeet: Coding Blog on 10/23/2009 9:20:00 PM
The IEnumerable<T> and IEnumerator<T> interfaces in .NET are interesting. They crop up an awful lot, but hardly anyone ever calls them directly - you almost always use a foreach loop to iterate over the collection. That hides all the calls to GetEnumerator(), MoveNext() and Current. Likewise iterator blocks hide the details when you want to implement the interfaces. However, sometimes details matter - such as for this recent Stack Overflow question. The question asks how to create a ...
by skeet via Jon Skeet: Coding Blog on 9/20/2009 8:48:00 PM
EDIT: Please ignore this post for the moment. It's wrong. I'll leave up the incorrect version for the moment so you can see what it will be about, but it doesn't quite work. The size of Empty ends up being 1, annoyingly enough. I'll edit this post properly when I get the chance. A long time ago, I'm pretty sure I started writing Push LINQ in response to a request to effectively count votes. To recap the example in the linked blog post, I wanted to work out the most popular co ...
Well, this is embarrassing. Yesterday evening, I excitedly wrote a blog post about an interesting little idea for making a particular type of LINQ query (basically vote counting) efficient. It was an idea that had occurred to me a few months back, but I hadn't got round to blogging about it. The basic idea was to take a completely empty struct, and use that as the element type in the results of a grouping query - as the struct was empty, it would take no space, therefore "huge" a ...
by skeet via Jon Skeet: Coding Blog on 9/14/2009 4:38:05 PM
So, UnconstrainedMelody is coming on quite nicely. It now has quite a few useful options for flags enums, "normal enums" and delegates. However, there are two conflicting limitations which leave a couple of options. (Other related answers on Stack Overflow have suggested alternative approaches, basically.) Currently, most of the enums code is in two classes: Flags and Enums. Both are non-generic: the methods within them are generic methods, so they have type parameters (and constraint ...
by skeet via Jon Skeet: Coding Blog on 9/10/2009 8:09:00 PM
As most readers probably know, C# prohibits generic type constraints from referring to System.Object, System.Enum, System.Array, System.Delegate and System.ValueType. In other words, this method declaration is illegal: public static T[]GetValues<T>() where T : struct, System.Enum{ return (T[]) Enum.GetValues(typeof(T)); } This is a pity, as such a method could be useful. (In fact there are better things we can do... such as returning a read-only collection. ...
by skeet via Jon Skeet: Coding Blog on 9/3/2009 11:08:00 PM
It's been a little while since I've blogged, and quite a lot has been going on. In fact, there are a few things I'd have blogged about already if it weren't for "things" getting in the way. Rather than writing a whole series of very short blog posts, I thought I'd wrap them all up here... C# in Depth: next MEAP drop available soon - Code Contracts Thanks to everyone who gave feedback on my writing dilemma. For the moment, the plan is to have a whole chapter about Co ...
by skeet via Jon Skeet: Coding Blog on 8/20/2009 4:57:40 PM
This morning I showed my hand a little on Twitter. I've had a dream for a long time about the ultimate C# book. It's a dream based on Effective Java, which is my favourite Java book, along with my experiences of writing C# in Depth. Effective Java is written by Josh Bloch, who is an absolute giant in the Java world... and that's both the problem and the opportunity. There's no-one of quite the equivalent stature in the .NET world. Instead, there are many very smart people, a lot ...
by skeet via Jon Skeet: Coding Blog on 8/5/2009 4:16:15 PM
I'd like some feedback from readers, and I suspect my blog is the simplest way to get it. I'm currently writing chapter 15 of C# in Depth, tentatively about Code Contracts and Parallel Extensions. The problem is that I'm 15 pages in, and I haven't finished Code Contracts yet. I suspect that with a typesetter moving the listings around a little it can be shortened a little bit, but I'm still concerned. With the amount I've still got to write, Code Contracts is going to en ...
by skeet via Jon Skeet: Coding Blog on 7/13/2009 4:10:00 PM
(Note that this kind of breakage was mentioned a long time ago in Eric Lippert's blog, although not in this exact form.) Whenever a conversion becomes available where it wasn't before, overload resolution can change its behaviour. From C# 1 to C# 2 this happened due to delegate variance with method group conversions - now the same thing is true for generic variance for interfaces. What does the following code print? using System; using System.Collections.Generic; class B ...
by skeet via Jon Skeet: Coding Blog on 7/7/2009 10:28:46 PM
C# 4 has some great features to make programming against COM components bearable fun and exciting. In particular: PIA linking allows you to embed just the relevant bits of the Primary Interop Assembly into your own assembly, so the PIA isn't actually required at execution time Named arguments and optional parameters make life much simpler for APIs like Office which are full of methods with gazillions of parameters "ref" removal allows you to pass an argument by value ev ...
by skeet via Jon Skeet: Coding Blog on 7/3/2009 10:28:26 PM
At a glance, this code doesn't look particularly evil. What does it do though? Compile it with the C# 4.0b1 compiler and run it... using System; class Base { public virtual void Foo(int x, int y) { Console.WriteLine("Base: x={0}, y={1}", x, y); } } class Derived : Base { public override void Foo(i ...
by skeet via Jon Skeet: Coding Blog on 6/19/2009 4:50:00 PM
Last night I presented for the first time at the Google Open Source Jam at our offices in London. The room was packed, but only a very few attendees were C# developers. I know that C# isn't the most popular language on the Open Source scene, but I was still surprised there weren't more people using C# for their jobs and hacking on Ruby/Python/etc at night. All the talks at OSJam are just 5 minutes long, with 2 minutes for questions. I'm really not used to this format, and felt extrem ...
by skeet via Jon Skeet: Coding Blog on 6/17/2009 8:01:13 PM
There have been mutterings about the fact that I haven't been blogging much recently. I've been getting down to serious work on the second edition of C# in Depth, and it's taking a lot of my time. However, I thought I'd share a ghastly little example I've just come up with. I've been having an email discussion with Sam Ng, Chris Burrows and Eric Lippert about how dynamic typing works. Sam mentioned that even for dynamically bound calls, type inference can fail at compile ...
by skeet via Jon Skeet: Coding Blog on 3/16/2009 5:21:00 PM
Update (19th March 2009) Debate around this review is getting heated. I stand by all the points I make about the text, but I'd like to clarify a few things: If there are any ad hominem comments in the review against the author, please ignore them. I'm going to try to weed out any that I find, but if you spot one, please let me know and then ignore it. I feel very strongly that a review should be about the text of a book, not about its author. The text is what will inform the r ...
by skeet via Jon Skeet: Coding Blog on 2/27/2009 11:29:00 AM
T.S. Eliot had the right idea when he wrote "The naming of cats": The Naming of Cats is a difficult matter,It isn't just one of your holiday games...When you notice a cat in profound meditation,The reason, I tell you, is always the same:His mind is engaged in a rapt contemplationOf the thought, of the thought, of the thought of his name:His ineffable effableEffanineffableDeep and inscrutable singular Name. Okay, so developers may not contemplate their own names much, but I know I ...
by skeet via Jon Skeet: Coding Blog on 1/29/2009 11:08:02 PM
As promised earlier in the week, here are the results of benchmarking for and foreach. For each of int and double, I created an array and a List<T>, filled it with random data (the same for the list as the array) and ran each of the following ways of summing the collection: A simple for loop, testing the index against the array length / list count each time A for loop with an initial variable remembering the length of the array, then comparing the index against the variable each time A ...
by skeet via Jon Skeet: Coding Blog on 1/26/2009 8:57:55 PM
While I was answering a Stack Overflow question on the performance implications of using a for loop instead of a foreach loop (or vice versa) I promised to blog about the results - particularly as I was getting different results to some other posters. On Saturday I started writing the bigger benchmark (which I will post about in the fullness of time) and used a technique that I'd used when answering a different question: have a single timing method and pass it a delegate, expected input and ...
by skeet via Jon Skeet: Coding Blog on 1/23/2009 11:40:39 PM
I've started a small project (I'll post a link when I've actually got something worthwhile to show) with some extra LINQ operators in - things which I think are missing from LINQ to Objects, basically. (I hope to include many of the ideas from an earlier blog post.) That, and a few Stack Overflow questions where I've effectively written extra LINQ operators and compared them with other solutions, have made me think about the desirable properties of a LINQ operator - or at least t ...
by skeet via Jon Skeet: Coding Blog on 1/23/2009 9:42:52 PM
This Stack Overflow question has reminded me of something I often wish existed in common exception constructors - an overload taking a format string and values. For instance, it would be really nice to be able to write: throw new IOException("Expected to read {0} bytes but only {1} were available", requiredSize, bytesRead); Of course, with no way of explic ...
by skeet via Jon Skeet: Coding Blog on 1/9/2009 7:52:14 AM
One of the reasons I don't view anonymous types as being too bad is that they're nicely confined to methods. You can't declare the type that you're returning from a method if it's anonymous (or if one of its type arguments is generic, e.g. a List<T> where T is an anonymous type and T isn't a type parameter to the method itself). However, you can get around this if you're sneaky. I've always known that it's perfectly easy to return an instance of an anony ...
by skeet via Jon Skeet: Coding Blog on 1/7/2009 5:32:24 PM
LINQ is clearly gaining a fair amount of traction, given the number of posts I see about it on Stack Overflow. However, I've noticed an interesting piece of coding style: a lot of developers are using query expressions for every bit of LINQ they write, however trivial. Now, don't get the wrong idea - I love query expressions as a helpful piece of syntactic sugar. For instance, I'd always pick the query expression form over the "dot notation" form for something like this: va ...
by skeet via Jon Skeet: Coding Blog on 12/10/2008 5:55:40 PM
There have been a couple of questions on StackOverflow about value types and parameterless constructors: Structure vs Class in C# Why can’t I define a default constructor for a struct in .NET I learned quite a bit when answering both of these. When a further question about the default value of a type (particularly with respect to generics) came up, I thought it would be worth delving into a bit more depth. Very little of this is actually relevant most of the time, but it's interesting none ...
by skeet via Jon Skeet: Coding Blog on 12/5/2008 5:52:44 PM
I've had quite a few discussions with a colleague about some failures of Java and .NET. The issue we keep coming back to is the root of the inheritance tree. There's no doubt in my mind that having a tree with a single top-level class is a good thing, but it's grown a bit too big for its boots. Pretty much everything in this post applies to both .NET and Java, sometimes with a few small changes. Where it might be unclear, I'll point out the changes explicitly - otherwise I'll ...
by skeet via Jon Skeet: Coding Blog on 12/4/2008 10:28:38 PM
I think I've now done enough public speaking to make it worth having a page with resources etc, so I've added it to the C# in Depth site. Where feasible, it will include slides, code and videos (and anything else suitable, really) from various events. I haven't got any more booked at the moment, although I've had a couple of invitations I really ought to sort out at some point. A couple of points of interest: I've included "before, during and after" code for the &q ...
by skeet via Jon Skeet: Coding Blog on 11/23/2008 10:25:53 AM
Those of you who've read C# in Depth will know about Snippy - a little tool which makes it easy to build complete programs from small snippets of code. I'm delighted to say that reader Jason Haley has taken the source code for Snippy and built an add-in for Reflector. This will make it much simpler to answer questions like this one about struct initialization, where you really want to the IL generated for a snippet. Here's a screenshot to show what it does: This is really cool - if ...
by skeet via Jon Skeet: Coding Blog on 11/19/2008 7:17:31 AM
The videos from my one day talk about C# in Copenhagen are now on the MSDN community site. There are eight sessions, varying between about 25 minutes and 50 minutes in length. I haven't had time to watch them yet, but when I do I'll submit brief summaries so you can quickly get to the bits you're most interested in. (As far as I'm aware, they're only available via Silverlight, which I realise isn't going to be convenient for everyone.) Feedback is very welcome. ...
by skeet via Jon Skeet: Coding Blog on 11/6/2008 1:38:00 AM
Update: As Chris Nahr pointed out, there's a blog post by Melitta Andersen of the BCL team explaining this in more detail. Obviously I've been looking at the proposed C# 4.0 features pretty carefully, and I promise I'll blog more about them at some later date - but yesterday I watched a PDC video which blew me away. As ever, a new version of .NET means more than just language changes - Justin van Patten has written an excellent blog post about what to expect in the core of thee frame ...
by skeet via Jon Skeet: Coding Blog on 10/30/2008 10:04:10 PM
I've not played with the VS2010 CTP much yet, and I've only looked briefly at the documentation and blogs about the new C# 4.0 dynamic type, but a thought occurred to me: why not have the option of making it generic as a way of saying "I will dynamically support this set of operations"? As an example of what I mean, suppose you have an interface IMessageRouter like this: public interface IMessageRouter{ void Send(string message, string destination);} (Th ...
by skeet via Jon Skeet: Coding Blog on 10/23/2008 8:50:39 PM
A few questions on Stack Overflow have suggested to me that there might be some bits missing from LINQ to Objects. There's the idea of a "zip" operator, which pairs up two sequences, for instance. Or the ability to apply the set operators with projections, e.g. DistinctBy, IntersectBy etc (mirroring OrderBy). They're easy to implement, but it would be nice to get a list of what people would like to see. So, what's missing? ...
by skeet via Jon Skeet: Coding Blog on 10/8/2008 8:24:09 PM
A question came up on Stack Overflow yesterday which I've had to deal with myself before now. There are times when it's helpful to have one value per type, and that value should be an instance of that type. To express it in pseudo-code, you want an IDictionary<typeof(T), T> except with T varying across all possible types. Indeed, this came up in Protocol Buffers at least once, I believe. .NET generics don't have any way of expressing this, and you end up with boxing and a cast. ...
by skeet via Jon Skeet: Coding Blog on 10/8/2008 7:42:02 PM
I'm currently reading the (generally excellent) CLR via C#, and I've recently hit the section on boxing. Why is it that authors feel they have to scaremonger about the effects boxing can have on performance? Here's a piece of code from the book: using System;public sealed class Program { public static void Main() { Int32 v = 5; // Create an unboxed value type variable.#if INEFFICIENT &nbs ...
by skeet via Jon Skeet: Coding Blog on 10/6/2008 8:51:08 PM
I suspect this has been done several times before, but on my way home this evening I considered what would be needed for the reverse of nullable value types - non-nullable reference types. I've had a play with an initial implementation in MiscUtil (not in the currently released version) and it basically boils down (after removing comments, equality etc) to this: public struct NonNullable<T> where T : class{ private readonly T value; public ...
by skeet via Jon Skeet: Coding Blog on 10/6/2008 10:45:00 AM
A while ago I wrote an article about StringBuilder and a reader mailed me to ask about the efficiency of using String.Format instead. This reminded me of a bone I have to pick with the BCL. Whenever we make a call to String.Format, it has to parse the format string. That doesn't sound too bad, but string formatting can be used a heck of a lot - and the format is almost always hard-coded in some way. It may be loaded from a resource file instead of being embedded directly in the source code, ...
by via Jon Skeet: Coding Blog on 9/9/2008 7:18:45 AM
I'm currently reading Pro LINQ: Language Integrated Query in C# 2008 by Joe Rattz and yesterday I came across a claim about Enumerable.Intersect which didn't quite ring true. I consulted MSDN and the documentation is exactly the same as the book. Here's what it says: When the object returned by this method is enumerated, Intersect enumerates first, collecting all distinct elements of that sequence. It then enumerates second, marking those elements that occur in both sequences. Final ...
The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.