I don't remember if I have or not. The code itself would not be coherrent if you dreamt it. The idea might be, but the code would not.
Next time you manage to think of it in a dream, write down two three-or-more-digit numbers and try to add them together. Then check your math.
I don't think you can really dream in code -- that level of detail is too much for your brain to focus on while you're asleep. At least it would be for me. Maybe if there's a tricky problem that I might dream in code about I tend to stay up so late thinking about it that I'm too tired to dream. :P
But dreaming about slightly more abstract ideas related to programming? Certainly. I distinctly remember dreaming about tetris for more than one night when I was developing my first tetris game, for example. Several times I have dreamt about using a program I am developing, or replaying in a dream the process I used to fix bugs in it, or envisioning what I would like it to look like. Or I'll dream about my process of coding, writing text and looking things up and switching between files.
And I also dream about, shall we say, worlds with very structured rules. I'll explore some world in a dream where I'm trying to achieve some goal, but there are only certain places I can go or certain actions I can perform. Sometimes I'll be able to do something like float slowly around in the air like a glider, but it doesn't seem strange, it's just another rule of the world I'm dreaming about. Restrictions or goals rarely seem frustrating to me, it's as if my brain is just exploring a world that's laid out for it.
I think that's the essence of my dreams. My brain combines everything I've been doing or thinking about into one world and just explores it to see what's possible. Sometimes I pick pretty bizarre elements from completely different things and combine them into a world. Often it really doesn't make much sense after I wake up. I've definitely come up with a lot of good ideas while dreaming, though, so it seems to have its benefits . . . .
An interesting point is that I certainly think in code while I'm awake. And I don't seem to have much trouble switching my mind into "C mode" or whatever and think only in that language. At the same time if something is really stumping me, I'll often think "Gee, this would be so much easier if I had default parameters" or "this would be so simple in Haskell". I think it truly makes you a much better programmer to be exposed to as many languages as you can be. But I'm going off topic now, so I'll stop.