Press "Enter" to skip to content

Category: C# Tips and Techniques

How to encode and decode a Base64 string in C#

Here’s how to encode a string to Base64 in C#, and how to convert a Base64-encoded string back to a normal string. In the example code, the Main function creates the “myString” variable, passes it into the EncodeToBase64 function (getting back a Base64-encoded string), then passes the encoded string into DecodeFromBase64 function (getting back the original string) In the EncodeToBase64 function, we convert the string to a byte array using the System.Text.Encoding.UTF8.GetBytes function. Then we pass the byte array into the System.Convert.ToBase64String function to get the Base64-encoded string. In the DecodeFromBase64 function, we pass in a Base64-encoded string and convert…

Leave a Comment

How to throw an exception in C#

Here is how to throw an exception in C#, and when you may not want to throw exceptions. The sample code will save an inventory item object, but not allow the save if the item’s Name property is an empty string, or the Quantity property is a negative number. We’ll do one way with throwing exceptions, and one way without. Keep in mind, you normally only want to throw an exception is something unexpected is happening. Common situations, like a user entering an invalid value, may better be handled without exceptions. In general, throwing an exception should be your last…

Leave a Comment

How to handle exceptions in C#

The three basic ways to handle exceptions in C# are: Swallow – Ignore and continue Rethrow, with the exception variable – Resets the stack trace Rethrow, without the exception variable – Maintains the original stack trace I’ll demonstrate how to implement the different methods, why you’d want to use each one, why you wouldn’t want to use some of them, and how I decide which one to use in my code. To demonstrate the differences, I created five functions that all call a Divide() function. In all cases, we’ll pass values into Divide() that will cause a “divide by zero”…

Leave a Comment

How to join two lists in C#

There are four common ways to combine two lists in C#: using Add() while looping, using Concat(), using AddRange(), and using ForEach() with Add(). Here’s how to use all four techniques. Combine two lists with Add() and a loop The first method is to use a foreach loop using the source list (the list you’re reading from), and calling .Add() on the destination list, passing in each value from the source list. In the code below, we pass the values from the “first” and “second” list variables into the “combined” list variable with the loops in lines 7-10 and 12-15.…

Leave a Comment

How to remove an item from a List in C#

If you’ve ever tried to remove an item from a list in C#, you may have encountered the error: “Collection was modified; enumeration may not execute”. Here are two ways that work. Why you get the error condition The error happens when using code like this: The problem is that the code is trying to modify the contents of the “values” List, while it is looping through it. When the loop starts, the code knows there are four items in this list, and that it is on the first item. If an item is removed from, or added to, the…

Leave a Comment