Monthly Archives: May 2021

C# || How To Set & Get The Description Of An Enum Using C#

The following is a module with functions which demonstrates how to set and get the description of an enum using C#.

This function uses the DescriptionAttribute to get the description of an Enum element. If a description attribute is not specified, the string value of the Enum element is returned.


1. Get Description

The example below demonstrates the use of ‘Utils.Extensions.GetDescription‘ to get the description of enum elements.


2. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


3. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C# || How To Iterate & Get The Values Of An Enum Using C#

The following is a module with functions which demonstrates how to iterate and get the values of an enum using C#.

This function is a generic wrapper for the Enum.GetValues function.


1. Get Values – Basic Enum

The example below demonstrates the use of ‘Utils.Methods.GetEnumValues‘ to get the values of a simple Enum.


2. Get Values – Numbered Enum

The example below demonstrates the use of ‘Utils.Methods.GetEnumValues‘ to get the values of a numbered Enum.


3. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


4. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C# || How To Resize & Rotate Image, Convert Image To Byte Array, Change Image Format, & Fix Image Orientation Using C#

The following is a module with functions which demonstrates how to resize an image, rotate an image to a specific angle, convert an image to a byte array, change an image format, and fix an image orientation Using C#.

Contents

1. Overview
2. Resize & Rotate - Image
3. Resize & Rotate - Byte Array
4. Resize & Rotate - Memory Stream
5. Convert Image To Byte Array
6. Change Image Format
7. Fix Image Orientation
8. Utils Namespace
9. More Examples


1. Overview

To use the functions in this module, make sure you have a reference to ‘System.Drawing‘ in your project.

One way to do this is, in your Solution Explorer (where all the files are shown with your project), right click the ‘References‘ folder, click ‘Add References‘, then type ‘System.Drawing‘ in the search box, and add a reference to System.Drawing in the results Tab.

Note: Don’t forget to include the ‘Utils Namespace‘ before running the examples!


2. Resize & Rotate – Image

The example below demonstrates the use of ‘Utils.Image.Extensions.Resize‘ and ‘Utils.Image.Extensions.Rotate‘ to resize and rotate an image object.

Resizing and rotating an image returns a newly created image object.


3. Resize & Rotate – Byte Array

The example below demonstrates the use of ‘Utils.Image.Extensions.Resize‘ and ‘Utils.Image.Extensions.Rotate‘ to resize and rotate a byte array.

Resizing and rotating an image returns a newly created image object.


4. Resize & Rotate – Memory Stream

The example below demonstrates the use of ‘Utils.Image.Resize‘ and ‘Utils.Image.Rotate‘ to resize and rotate a memory stream.

Resizing and rotating an image returns a newly created image object.


5. Convert Image To Byte Array

The example below demonstrates the use of ‘Utils.Image.Extensions.ToArray‘ to convert an image object to a byte array.

The optional function parameter allows you to specify the image format.


6. Change Image Format

The example below demonstrates the use of ‘Utils.Image.Extensions.ConvertFormat‘ to convert the image object raw format to another format.

Converting an images format returns a newly created image object.


7. Fix Image Orientation

The example below demonstrates the use of ‘Utils.Image.Extensions.FixOrientation‘ to update the image object to reflect its Exif orientation.

When you view an image in a Photo Viewer, it automatically corrects image orientation if it has Exif orientation data.

Utils.Image.Extensions.FixOrientation‘ makes it so the loaded image object reflects the correct orientation when rendered according to its Exif orientation data.


8. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


9. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C# || How To Convert A String To Byte Array & Byte Array To String Using C#

The following is a module with functions which demonstrates how to convert a string to a byte array and a byte array to a string using C#.


1. String To Byte Array

The example below demonstrates the use of ‘Utils.Extensions.GetBytes‘ to convert a string to a byte array. The optional parameter allows to specify the encoding.


2. Byte Array To String

The example below demonstrates the use of ‘Utils.Extensions.GetString‘ to convert a byte array to a string. The optional parameter allows to specify the encoding.


3. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


4. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C# || How To Save, Open & Read File As A Byte Array & Memory Stream Using C#

The following is a module with functions which demonstrates how to save, open and read a file as a byte array and memory stream using C#.


1. Read File – Byte Array

The example below demonstrates the use of ‘Utils.Methods.ReadFile‘ to read a file as a byte array.


2. Read File – Memory Stream

The example below demonstrates the use of ‘Utils.Methods.ReadFile‘ to read a file as a memory stream.


3. Save File – Byte Array

The example below demonstrates the use of ‘Utils.Methods.SaveFile‘ to save the contents of a byte array to a file.


4. Save File – Memory Stream

The example below demonstrates the use of ‘Utils.Methods.SaveFile‘ to save the contents of a memory stream to a file.


5. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


6. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C# || How To Validate An Email Address Using C#

The following is a module with functions which demonstrates how to determine if an email address is valid using C#.

This function is based on RFC2821 and RFC2822 to determine the validity of an email address.


1. Validate Email – Basic Usage

The example below demonstrates the use of ‘Utils.Email.IsValid‘ to determine if an email address is valid.


2. Validate Email – Additional Options

The example below demonstrates the use of ‘Utils.Email.IsValid‘ to determine if an email address is valid with additional validation options.

Supplying options to verify an email address uses the default validity rules, in addition to the provided validation options.

This allows you to add custom rules to determine if an email is valid or not. For example, the misspelling of common email domains, or known spam domains.


3. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


4. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C# || How To Validate A Phone Number Using C#

The following is a module with functions which demonstrates how to validate a phone number using C#.

Not only does this function check formatting, but follows NANP numbering rules by ensuring that the first digit in the area code and the first digit in the second section are 2-9.


1. Validate Phone Number

The example below demonstrates the use of ‘Utils.Methods.IsValidPhoneNumber‘ to determine if a phone number is valid.


2. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


3. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C# || How To Shuffle & Randomize An Array/List/IEnumerable Using C#

The following is a module with functions which demonstrates how to randomize and shuffle the contents of an Array/List/IEnumerable using C#.

This function shuffles an IEnumerable and returns the results as a new List(Of T).

This function is generic, so it should work on IEnumerables of any datatype.


1. Shuffle – Integer Array

The example below demonstrates the use of ‘Utils.Extensions.Shuffle‘ to randomize an integer array.


2. Shuffle – String List

The example below demonstrates the use of ‘Utils.Extensions.Shuffle‘ to randomize a list of strings.


3. Shuffle – Custom Object List

The example below demonstrates the use of ‘Utils.Extensions.Shuffle‘ to randomize a list of custom objects.


4. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


5. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C# || Word Wrap – How To Split A String Text Into lines With Maximum Length Using C#

The following is a module with functions which demonstrates how to split text into multiple lines using C#.

The following function is an extension method, which takes a string, and splits it into multiple lines (array indices) of a specified length.

An optional boolean parameter specifies whether this function should break up long individual words to fit as many characters as possible on a line.

With this option enabled, if a word is too long to fit on a line, the word is broken up to fit as many characters as possible up to the maximum length on the current line, and the remaining characters gets moved to the next line (the next array index).

Note: Don’t forget to include the ‘Utils Namespace‘ before running the examples!


1. Word Wrap – Don’t Break Words

The example below demonstrates the use of ‘Utils.Extensions.WordWrap‘ to split a string into multiple lines.

In this example, individual words are not broken up. They are simply moved onto the next line (if necessary).


2. Word Wrap – Break Words

The example below demonstrates the use of ‘Utils.Extensions.WordWrap‘ to split a string into multiple lines.

In this example, individual words are broken up and moved onto the next line (if necessary), with the default hyphen character used, which is a dash (‘-‘).


3. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


4. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C# || How To Get First/Last Day Of The Week And The First/Last Day Of The Month

The following is a module with functions which demonstrates how to get the first and last day of the week, as well as how to get the first and last day of the month using C#.


1. First & Last Day Of Week

The example below demonstrates the use of ‘Utils.DateRange.GetWeekStartAndEnd‘ to get the first and last day of the week.

By default, the start of the week is set on Monday. This can be changed by setting the second parameter to any valid property contained in the System.DayOfWeek enum.


2. First & Last Day Of Month

The example below demonstrates the use of ‘Utils.DateRange.GetMonthStartAndEnd‘ to get the first and last day of the week.


3. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


4. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.