Browse by Tags

All Tags » C# » Wacky Ideas (RSS)

Eduasync part 13: first look at coroutines with async

(This part covers project 18 in the source code .) As I mentioned in earlier parts, the "awaiting" part of async methods is in no way limited to tasks. So long as we have a suitable GetAwaiter() method which returns a value of a type which in...
Posted by skeet | 14 comment(s)
Filed under: , , , ,

Of memory and strings

This post was provoked by a recent Stack Overflow question which asked whether there was an efficient representation of ASCII strings in .NET. In particular, the questioner wanted to story hundreds of thousands - possibly millions - of strings in memory...
Posted by skeet | 32 comment(s)
Filed under: , ,

The curious case of the publicity-seeking interface and the shy abstract class

Noda Time has a guilty secret, and I'm not just talking about the fact that there's been very little progress on it recently. (It's not dead as a project - I have high hopes, when I can put some quality time into it.) This secret is called...
Posted by skeet | 26 comment(s)
Filed under: , ,

Non-iterable collection initializers

Yesterday on Stack Overflow, I mentioned that sometimes I make a type implement IEnumerable just so that I can use collection initializers with it. In such a situation, I use explicit interface implementation (despite not really needing to - I'm not...
Posted by skeet | 15 comment(s)
Filed under: ,

Iterate, damn you!

Do you know the hardest thing about presenting code with surprising results? It's hard to do so without effectively inviting readers to look for the trick. Not that that's always enough - I failed the last of Neal and Eric's C# puzzlers at...
Posted by skeet | 15 comment(s)
Filed under: ,

First encounters with Reactive Extensions

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...
Posted by skeet | 15 comment(s)
Filed under: , , ,

"Magic" null argument testing

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...

Contract classes and nested types within interfaces

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 {...
Posted by skeet | 9 comment(s)
Filed under: , , ,

Iterating atomically

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...

An object lesson in blogging and accuracy; was: Efficient "vote counting" with LINQ to Objects - and the value of nothing

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...
Posted by skeet | 11 comment(s)
Filed under: , ,

Generic constraints for enums and delegates

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>...
Posted by skeet | 56 comment(s)

The "dream book" for C# and .NET

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...
Posted by skeet | 34 comment(s)
Filed under: , ,

Faking COM to fool the C# compiler

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...
Posted by skeet | 1 comment(s)

A different approach to inappropriate defaults

I've had a couple of bug reports about my Protocol Buffers port - both nicely detailed, and one including a patch to fix it. (It's only due to my lack of timeliness in actually submitting the change that the second bug report occurred. Oops.)...
Posted by skeet | 9 comment(s)
Filed under: , ,

Breaking Liskov

Very recently, Barbara Liskov won the Turing award , which makes it a highly appropriate time to ponder when it's reasonable to ignore her most famous piece of work, the Liskov Substitution (or Substitutability) Principle . This is not idle speculation...
Posted by skeet | 32 comment(s)
Filed under: , ,

Quick rant: why isn't there an Exception(string, params object[]) constructor?

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...
Posted by skeet | 26 comment(s)

Horrible grotty hack: returning an anonymous type instance

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...
Posted by skeet | 21 comment(s)
Filed under: ,

Value types and parameterless constructors

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...

Redesigning System.Object/java.lang.Object

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...

C# 4.0: dynamic<T> ?

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...
More Posts Next page »