How to Convert Object to JSON Without Escape Characters in C#

Convert object to JSON without escape characters in C#: Tom u0026 Jerryu0027s becomes Tom & Jerry's with UnsafeRelaxedJsonEscaping

Most people who search convert object to JSON without escape characters C# just ran JsonSerializer.Serialize on a name like Tom & Jerry’s and got this back: Quick answer: set Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping in JsonSerializerOptions. Use it only for JSON sent as UTF-8, never inside an HTML page. I tested every fix on .NET 10 with … Read more >>

How to Convert String to Double in C#

Convert string to double in C#: "1,234.56" becomes 1234.56 with double.Parse and CultureInfo.InvariantCulture

The safest way to convert string to double in C# is double.TryParse with an explicit culture. It returns false on bad text instead of crashing: Every snippet below ran on .NET 10 against a 2026 fuel log, including blank input, European commas and numbers too big for a double. double.Parse: Convert a String to Double … Read more >>

How to Check for Null in C# (is null, ?., ??, Empty Objects)

Check for null in C#: an overloaded == operator makes cart == null print True while cart is null prints False

The usual way to check for null in C#, if (order == null), once skipped real orders in a client’s checkout. Their Order class overloaded ==, so an empty cart compared equal to null. Quick answer: write order is null or order is not null. The compiler never calls an overloaded operator for those patterns. … Read more >>

How to Convert Int to Binary String with Leading Zeros in C#

Convert int to binary string with leading zeros in C#: 10 formatted with ToString("B8") as eight bit boxes 00001010

To convert int to binary string with leading zeros in C#, call ToString(“B8”) on .NET 8 or later, or Convert.ToString(n, 2).PadLeft(8, ‘0’) on older frameworks: Quick answer: the number after B is the minimum digit count, so B8 turns 10 into 00001010. I ran both on .NET 10, plus negative numbers, nibble grouping and parsing … Read more >>

How to Convert String to DateTime in C#

Convert string to DateTime in C#: DateTime.Parse reads 03/04/2026 as March 4, 2026 with en-US and April 3, 2026 with en-GB

Here’s a bug we catch in code reviews every month: DateTime.Parse(“03/04/2026”) returns March 4 on a US server and April 3 on a UK one. Same line, two dates, no error. Quick answer: to convert string to DateTime in C#, always pass the culture: DateTime.Parse(text, CultureInfo.GetCultureInfo(“en-US”)). Use TryParse for user input and ParseExact when you … Read more >>