Edge cases in the string vs. String comparison in C#

Yan Cui

I help clients go faster for less using serverless technologies.

Whilst most C# developer would know that in C# the string type is equivalent to the System.String type because string is simply an alias for the System.String type. There are other framework defined alias like string, such as int for System.Int32 and long for System.Int64.

Whilst in normal usage the two are identical, thanks to Marc Gravell’s answer here, I found out about two interesting edge cases:

  • you need a  using System; to use String, you don’t for string
  • if you define a local class String { }, then String refers to that class, you can’t define a class called string

On a related topic, one thing that’s always been curious to me is that there are no ToInt or ToLong methods in the Convert class but only ToIn32 and ToInt64. Dan Puzey gave me a plausible explanation for this though:

Convert is a framework class, not a language keyword. If it had methods named after the C# keywords, the methods would make no sense in VB or F# or other .NET languages.


 

Whenever you’re ready, here are 4 ways I can help you:

  1. If you want a one-stop shop to help you quickly level up your serverless skills, you should check out my Production-Ready Serverless workshop. Over 20 AWS Heroes & Community Builders have passed through this workshop, plus 1000+ students from the likes of AWS, LEGO, Booking, HBO and Siemens.
  2. If you want to learn how to test serverless applications without all the pain and hassle, you should check out my latest course, Testing Serverless Architectures.
  3. If you’re a manager or founder and want to help your team move faster and build better software, then check out my consulting services.
  4. If you just want to hang out, talk serverless, or ask for help, then you should join my FREE Community.

 


Leave a Comment

Your email address will not be published. Required fields are marked *