Generic Extensions - adyle5/ExtensionsDotNet GitHub Wiki

DeepCopyExt

Description
Performs a deep copy of an object.
Object must be a class and must be serializable.

Parameters
None

Usage

string str1 = "test";
string str2 = str1.DeepCopyExt();

Will str2 will be a deep copy of str1 that do not reference the same location in memory.


ToBooleanExt

Description
Maps to Convert.ToBoolean Converts value and returns new boolean.

Parameters
None

Usage

string t = "true";
bool b = t.ToBooleanExt();

ToByteExt

Description
Maps to Convert.ToByte Converts value and returns a new 8-bit unsigned integer.

Parameters
None

Usage

string t = "1";
byte b = t.ToByteExt();

ToCharExt

Description
Maps to Convert.ToChar Converts value and returns a new unicode character.

Parameters
None

Usage

string t = "c";
char c = t.ToCharExt();

ToDecimalExt

Description
Maps to Convert.ToDecimal Converts value and returns a new decimal number.

Parameters
None

Usage

string t = "1.25";
decimal d = t.ToDecimalExt();

ToDoubleExt

Description
Maps to Convert.ToDouble Converts value and returns a new double-precision floating-point number.

Parameters
None

Usage

string t = "1.25";
double d = t.ToDoubleExt();

ToFloatExt

Description
Converts a string to a float or throws a format exception.

Parameters
None

Usage

string t = "1.25";
float f = t.ToFloatExt();

ToInt16Ext

Description
Maps to Convert.ToInt16 Converts value and returns a new 16-bit signed integer

Parameters
None

Usage

string t = "1";
short s = t.ToInt16Ext();

ToIntExt

Description
Maps to Convert.ToInt32 Converts value and returns a new 32-bit signed integer

Parameters
None

Usage

string t = "1";
int s = t.ToIntExt();

ToInt32Ext [Deprecated]

Description
Deprecated. Use ToIntExt.
This extension will be removed in a future release.


ToInt64Ext

Description
Maps to Convert.ToInt64 Converts value and returns a new 64-bit signed integer

Parameters
None

Usage

string t = "1";
long l = t.ToInt64Ext();

ToLongExt

Description
Converts a string to a long or throws a Format Exception

Parameters
None

Usage

string t = "100";
long l = t.ToLongExt();

ToSByteExt

Description
Maps to Convert.ToSByte Converts value and returns a new 8-bit signed integer

Parameters
None

Usage

string t = "100";
sbyte sb = t.ToSByteExt();

ToShortExt

Description
Converts a string to a short or throws a format exception

Parameters
None

Usage

string t = "100";
short s = t.ToShortExt();

ToSingleExt

Description
Maps to Convert.ToSingle Converts value and returns a new single-precision floating-point number

Parameters
None

Usage

string t = "1.5";
float f = t.ToSingleExt();

ToUInt16Ext

Description
Maps to [Convert.ToUInt16]https://docs.microsoft.com/en-us/dotnet/api/system.convert.touint16) Converts value and returns a new 16-bit unsigned integer

Parameters
None

Usage

string t = "1";
ushort s = t.ToUInt16Ext();

ToUInt32Ext

Description
Maps to [Convert.ToUInt32]https://docs.microsoft.com/en-us/dotnet/api/system.convert.touint32) Converts value and returns a new 32-bit unsigned integer

Parameters
None

Usage

string t = "1";
uint i = t.ToUInt32Ext();

ToUInt64Ext

Description
Maps to [Convert.ToUInt64]https://docs.microsoft.com/en-us/dotnet/api/system.convert.touint64) Converts value and returns a new 64-bit unsigned integer

Parameters
None

Usage

string t = "1";
ulong l = t.ToUInt64Ext();

ToStreamExt

Description
Converts a stream.
Generated stream needs to be disposed or wrapped in using statement.

Parameters
None

Usage

int[] arr = { 1, 2, 3, 4, 5 };

using (Stream stream = arr.ToStreamExt())
{
    ...
}

or

Stream stream = arr.ToStreamExt();
...
stream.Dispose();

ToKeyValuePairExt

Description
Converts to a KeyValuePair with the value of the extended type as the key and the parameter as the value.

Parameters
value (U)

Usage

int key = 1;
string value = "red";
var kvp = key.ToKeyValuePairExt(value);

ToKeyValuePair2Ext

Description
Converts to a KeyValuePair with the value of the extended type as the value and the parameter as the key.

Parameters
key (T)

Usage

int key = 1;
string value = "red";
var kvp = value.ToKeyValuePair2Ext(key);

RepeatExt

Description
Creates a new IEnumerable of the same type as the extended type and repeats the value of the extended times based on the integral parameter.
Maps to Enumerable.Repeat

Parameters
numberOfTimes (int)

Usage

int num = 3;
int repeat = 5;
IEnumerable<int> repeated = num.RepeatExt(repeat);

TraceExt

Description
Write's the value of a string or the ToString method to the Trace Listener.
Map of Trace.Write

Parameters
None

Usage

string text = "test";
text.TraceExt();

Writes test to the registered trace listener.


TraceExt (with category)

Description
Write's the value of a string or the ToString method and category specified to the Trace Listener.
Map of Trace.Write

Parameters
category (string)

Usage

string text = "test";
string category = "debug";
text.TraceExt(category);

Writes test to the registered trace listener with category of debug.


TraceIfExt

Description
Write's the value of a string or the ToString method specified to the Trace Listener based on a condition.
Map of Trace.WriteIf

Parameters
condition (bool)

Usage

string text = "test";
text.TraceIfExt(true);

TraceIfExt (with category)

Description
Write's the value of a string or the ToString method and category specified to the Trace Listener based on a condition.
Map of Trace.WriteIf

Parameters
condition (bool) category (string)

Usage

string text = "test";
text.TraceIfExt(true, category);

TraceLineExt

Description
Write's the value of a string or the ToString method to a new line in the Trace Listener.
Map of Trace.WriteLine

Parameters
None

Usage

string text = "test";
text.TraceLineExt();

Writes test to a new line the registered trace listener.


TraceLineExt (with category)

Description
Write's the value of a string or the ToString method and category specified to a new line in the Trace Listener.
Map of Trace.WriteLine

Parameters
category (string)

Usage

string text = "test";
string category = "debug";
text.TraceLineExt(category);

Writes test to a new line the registered trace listener with category debug.


TraceLineIfExt

Description
Write's the value of a string or the ToString method specified to a new line in the Trace Listener based on a condition.
Map of Trace.WriteLineIf

Parameters
condition (bool)

Usage

string text = "test";
text.TraceLineIfExt(true);

TraceIfExt (with category)

Description
Write's the value of a string or the ToString method and category specified to a new line in the Trace Listener based on a condition.
Map of Trace.WriteLineIf

Parameters
condition (bool) category (string)

Usage

string text = "test";
text.TraceLineIfExt(true, category);

ToStringBuilderExt

Description
Returns a StringBuilder where the extended object is the beginning of the StringBuilder and one or more objects parameters are appended to the StringBuilder.
All objects must be of type char, bool, ulong, uint, byte, string, float, ushort object, decimal, short, int, long, or double.

Parameters
appends (params object[])

Usage

string hello = "Hello";
string world = "World";
string space = " ";
string exclaim = "!";
StringBuilder sb = hello.ToStringBuilderExt(space, world, exclaim);

sb.ToString() OUTPUTS: "Hello World!"


IsDBNullExt

Description
Returns true if the object returned from the database is of type DBNull, otherwise returns false.
Maps to Convert.IsDBNull

Parameters
None

Usage

using var ctx = new TestModelContext();
ctx.Database.OpenConnection();
using var command = ctx.Database.GetDbConnection().CreateCommand();
command.CommandText = @"SELECT Description FROM Table1 WHERE Id = 1;";
object description = command.ExecuteScalar();
if (description.IsDBNullExt())
{
    // This is true!
}
ctx.Database.CloseConnection();

GetDescriptionExt

Description
Get value from a description attribute.

Parameters
None

Usage

// [Description("First Name")]
// public string FName { get; set; }

string description = FName.GetDescriptionExt();

OUTPUT: "First Name"


ToStringExt

Description
Returns a string representation of the values in a generic list that implements IConvertible separated by a comma.

Parameters
None

Usage

var list = new List<string>() { "one", "two", "three" };
var stringFromList = list.ToStringExt();

OUTPUT: "one,two,three"


IsExt

Description
Returns true if the extended object is of type T, otherwise returns false.

Parameters
None

Usage

object o = "This is a string";
bool isString = o.IsExt<string>();

OUTPUT: true

⚠️ **GitHub.com Fallback** ⚠️