C# Ranges and their exclusionary upper bound

I’ve been moving to using more of the range based code and shorthand in my C# code recently- The ranges are pretty cool in how you can specify easily the lower..upper shorthand to specify a subset of an array or even letters in a string.

Recently I fully noticed something odd about the upper bound- if you perform this-

string s = "this_is_a_string";
string s2 = s[0..5];

One might assume this would pull the characters starting at index 0 (or “t”) and continue through the character at index 5 (“i”)- but instead the string will be:

"this_"

What?

So it turns out, the .Net team had to make a decision as to whether the upper bound of ranges would be inclusive or exclusive of the last element – as discussed here. The most obvious answer would be inclusive, since the letter at index 5 should be “i”… but they apparently weighted the pros and cons (and, there are quite a few things to consider), and decided on exclusive.

This results in a cleaner implementation for most use cases (as demonstated in the linked document above), but you definitely need to know this information when working with range notation. Feels like it needs a big flashing warning sign on it somewhere!

My last comment, I find it curious that a second range notation was being considered for start:length, instead of just start..end. This would be quite useful as well but we’ll have to see if it gets implemented in the future (unless it has been but I’m just overlooking it somehow).

facebookexternalhit/1.1

You’re seeing these in your user agent app logs and wondering what’s going on? Is this a link from facebook.com or some kind of sinister bot?

When someone posts a link to your page on facebook, you’ll notice a preview image of the page will often show up- so facebook is going out and loading your page url and scraping the content to figure out what’s on the page, and what should be shown for the preview… it also is just checking that the url is valid and not some bad/spammy/malicious content that should not be allowed. Since this is effectively a bot hitting your site, facebook sets the user agent to:

facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)

Note that if your site is set to block bots, it may block this request and thus facebook won’t get a valid response to the request- so it may either disallow the post or it may allow the link but just not show any preview content for it.

Do you want a specific images to be shown on facebook when a link is posted? You can use OpenGraph headers to specify meta data to use for this, and set the og:image tag to point to the image you would prefer be used.

For more info, here are references for this content:

Meta Web Crawlers: https://developers.facebook.com/docs/sharing/webmasters/web-crawlers

Images in Link Shares: https://developers.facebook.com/docs/sharing/webmasters/images