Showing tag results for Code

Aug 28, 2008
Post comments count0
Post likes count1

What possible use are those extra bits in kernel handles? Part 2: Overcoming limited expressiveness

Raymond Chen
Raymond Chen

Last time, we saw how those extra bits can be used to develop safe sentinel values. That is a special case of a more general problem: How do you pack 33 bits of information into a 32-bit value? Whereas last time, we weren't forced into the use of a sentinel value because we could develop a (cumbersome) helper class and switch people over to ...

Code
Aug 27, 2008
Post comments count0
Post likes count1

What possible use are those extra bits in kernel handles? Part 1: Sentinels

Raymond Chen
Raymond Chen

Kernel handles are always a multiple of four; the bottom two bits are available for applications to use. But why would an application need those bits anyway? The short answer is extending the handle namespace. The long answer will take a few days to play out. (This series was written in response to Igor Levicki being unable to imagine "how thi...

Code
Aug 15, 2008
Post comments count0
Post likes count0

The implementation of iterators in C# and its consequences (part 4)

Raymond Chen
Raymond Chen

You can breathe a sigh of relief. Our long national nightmare is over: this is the end of CLR Week 2008. We wind down with a look back at iterators. Michael Entin points out that you can use C# iterators to make asynchronous code easier to write. You can use C# iterators for more than simply iterating. The automatic conversion of...

Code
Aug 14, 2008
Post comments count0
Post likes count0

The implementation of iterators in C# and its consequences (part 3)

Raymond Chen
Raymond Chen

I mentioned that there was an exception to the general statement that the conversion of an iterator into traditional C# code is something you could have done yourself. That's true, and it was also a pun, because the exception is exception handling. If you have a try ... finally block in your iterator, the language executes the finally block und...

Code
Aug 13, 2008
Post comments count0
Post likes count0

The implementation of iterators in C# and its consequences (part 2)

Raymond Chen
Raymond Chen

Now that you have the basic idea behind iterators under your belt, you can already answer some questions on iterator usage. Here's a scenario based on actual events: I have an iterator that is rather long and complicated, so I'd like to refactor it. For illustrative purposes, let's say that the enumerator counts from 1 to 100 twice. (In real l...

Code
Aug 12, 2008
Post comments count0
Post likes count0

The implementation of iterators in C# and its consequences (part 1)

Raymond Chen
Raymond Chen

Like anonymous methods, iterators in C# are very complex syntactic sugar. You could do it all yourself (after all, you did have to do it all yourself in earlier versions of C#), but the compiler transformation makes for much greater convenience. The idea behind iterators is that they take a function with yield return statements (and possible s...

Code
Aug 11, 2008
Post comments count0
Post likes count1

Psychic debugging: Why can’t StreamReader read apostrophes from a text file?

Raymond Chen
Raymond Chen

As is customary, the first day of CLR Week is a warm-up. Actually, today's question is a BCL question, not a CLR question, but only the nitpickers will bother to notice. Can somebody explain why StreamReader can’t read apostrophes? I have a text file, and I read from it the way you would expect: StreamReader sr = new StreamReader("myfil...

Code
Aug 8, 2008
Post comments count0
Post likes count1

If you return FALSE from DLL_PROCESS_ATTACH, will you get a DLL_PROCESS_DETACH?

Raymond Chen
Raymond Chen

If you return from , will you get a ? Yes. No. ... Yes. All three answers are correct, for different formulations of the question. From the kernel's point of view, the answer is a simple Yes. If a DLL's entry point returns to the notification, it will receive a notification. However, most C and C++ programs do not use the ra...

Code
Aug 6, 2008
Post comments count0
Post likes count1

The caret serves as the continuation character for batch files

Raymond Chen
Raymond Chen

We saw earlier that the caret is the escape character for the batch language. In a comment to that article, KJK::Hyperion mentioned that the caret serves as the line continuation character. A useful tip if you still find yourself messing with batch files. Mark Yocum elaborates on this point a bit more.

Code