To delete a single file in C#, use File.Delete, and pass in a string of the name of the file. Delete a single file If…
Leave a CommentCategory: C# code snippets
To remove spaces from a string with C#, use the Replace function, passing in the value you want to remove and the value you want…
Leave a CommentTo convert a string value to an enum value, use the Enum.TryParse function. This is the enum we’ll use in the example code: Notice that…
Leave a CommentTo generate a random number in C#, either use Random.Next in the System namespace, or the RandomNumberGenerator.GetInt32 function in the System.Security.Cryptography namespace. In the first…
Leave a CommentExtension methods are a way to have a function that can be used by all objects of a specific datatype or interface. They “extend” the…
Leave a CommentHere’s how to set default property values for properties in C# On line 3, we have an auto-property “Name”. To set its default value, after…
Leave a CommentHere’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…
Leave a CommentHere is how to throw an exception in C#, and when you may not want to throw exceptions. The sample code will save an inventory…
Leave a CommentThe three basic ways to handle exceptions in C# are: Swallow – Ignore and continue Rethrow, with the exception variable – Resets the stack trace…
Leave a CommentThere 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…
Leave a Comment