Showing tag results for Code

Aug 14, 2009
Post comments count0
Post likes count1

Why can't I declare a type that derives from a generic type parameter?

Raymond Chen

A lot of questions about C# generics come from the starting point that they are just a cutesy C# name for C++ templates. But while the two may look similar in the source code, they are actually quite different. C++ templates are macros on steroids. No code gets generated when a template is "compiled"; the compiler merely hangs onto the source co...

Code
Aug 13, 2009
Post comments count0
Post likes count1

Common gotchas when writing your own p/invoke

Raymond Chen

If you're looking to get into some p/invoke action, you'd be well-served to check out the pinvoke wiki to see if somebody else has done it too. If what you need isn't there, you may end up forced to write your own, and here are some gotchas I've seen people run into: C++ and Win32 are not the same as C# (aka ). In Win32, is ...

Code
Aug 12, 2009
Post comments count0
Post likes count1

Why can't I pass a reference to a derived class to a function that takes a reference to a base class by reference?

Raymond Chen

"Why can't I pass a reference to a derived class to a function that takes a reference to a base class by reference?" That's a confusing question, but it's phrased that way because the simpler phrasing is wrong! Ths misleading simplified phrasing of the question is "Why can't I pass a reference to a derived class to a function that takes a base c...

Code
Aug 11, 2009
Post comments count0
Post likes count1

Actually, FlagsAttribute can't do more; that's why it's an attribute

Raymond Chen

A few years ago, Abhinaba wondered why FlagsAttribute didn't also alter the way enumeration values are auto-assigned. Because attributes don't change the language. They are instructions to the runtime environment or (in rarer cases) to the compiler. An attribute can instruct the runtime environment to treat the function or class in a particular w...

Code
Aug 10, 2009
Post comments count0
Post likes count1

Why doesn't String.Format throw a FormatException if you pass too many parameters?

Raymond Chen

Welcome to CLR Week 2009. As always, we start with a warm-up. The method doesn't throw a if you pass too many parameters, but it does if you pass too few. Why the asymmetry? Well, this is the type of asymmetry you see in the world a lot. You need a ticket for each person that attends a concert. If you have too few tickets, they won't let you...

Code
Aug 7, 2009
Post comments count0
Post likes count1

SHCIDS_CANONICALONLY is the moral equivalent in the shell namespace of the Unicode ordinal comparison

Raymond Chen

One of the flags you can pass to the method is . This flag means that the method should determine whether the two pointers refer to the same underlying object, and if they do not, then it should determine which one should come first by whatever mechanism it wants. It doesn't matter which one is declared as coming before the other one, as long as...

Code
Aug 6, 2009
Post comments count0
Post likes count1

The great thing about regular expression engines is that there are so many to choose from

Raymond Chen

Back in the days before perl ruled the earth, regular expressions were one of those weird niche features, one of those things that everybody reimplements when they need it. If you look at the old unix tools, you'll see that even then, there were three different regular expression engines with different syntax. You had , , and . Probably more. The ...

Code
Aug 4, 2009
Post comments count0
Post likes count1

Programming means that sometimes you have to snap two blocks together

Raymond Chen

Part of the challenge of programming (and for some people, the reason why programming is fun in the first place) is looking at the building blocks provided to you and deciding how to assemble them to build something new. After all, if everything you wanted a program to do already existed ready-made, it wouldn't be called programming any more. It wo...

Code
Jul 31, 2009
Post comments count0
Post likes count1

What is the difference between Directory and DirectoryBackground?

Raymond Chen

One item I left off the list of special progids is . Recall that is the progid for file system folders (a subset of which represents all shell folders, both file system and virtual). Closely related is , which isn't really a progid, but it is a place where shell extensions can register themselves. Specifically, it's where context menu handlers...

Code
Jul 30, 2009
Post comments count0
Post likes count1

What is the difference between CSIDL_DESKTOP and CSIDL_DESKTOPDIRECTORY?

Raymond Chen

Among the various values you can pass to functions like are and . What's the difference between them? The is the virtual folder that represents the desktop. The contents of this virtual folder is what gets displayed on top of your wallpaper. The virtual folder is populated from various locations, some of them virtual, and some of them physic...

Code