Mike-Ward.Net

C#'s Unsung Hero

I was updating some legacy Windows C++ code last night and the pain of using it hit me like a ton of bricks. In particular, the string handling was a nightmare. Here are all the ways I found strings used in this program:

  • CString
  • Bse_String (private class)
  • OLESTR
  • LPCTSTR
  • LPSTR
  • wchar_t (include const and pointer variants)
  • TCHAR (include const and pointer variants)
  • char (include const and pointer variants)
  • STL string
  • BSTR
  • _bstr_t
  • CSimpleStringT

What a mess. And worse still, this only address the width of the characters, not the encoding. Now admittedly, some of macro types overlap but still if you haven’t used this stuff in a while, it is hard to remember what maps to what.

And then there are all the _t variants for the library methods. For instance, _tstrcpy replaces strcpy. Finally, there are macros to help with conversions like W2A, T2W, CA2T, etc.

C# String handling is light and breezy by comparison. There’s one class and it pretty much deals with everything concerning string handling. Encodings are equally easy and the support for common encodings like UTF-8 are supported. Ever try to do UTF-8 in C++?

And what about localizations? I never did get the hang of the C++ locale and facet classes. In C# (well .Net really) it’s built-in.

So here’s to C#’s unsung hero, the humble and unifying String class.

← newer older →
.Net, Technology, Life, Whatever

Recent Posts

Quick Tip - Refined GitHub
Vertical Layout for HTML Tables
Visual Studio 2017 Installer Quick Tip
VSColorOutput for VS 2017 Released
Quick ReSharper tip for inline templates
NoSQL vs. NewSQL: What's the Difference?
Exploring Mithril, A JavaScript Framework for Building Brilliant Applications
More... (1076)

Donate with PayPal