Table of Contents

Interface IConvertService

Namespace
ToSic.Sxc.Services
Assembly
ToSic.Sxc.dll

Conversion helper for things which are very common in web-code like Razor and WebAPIs. It's mainly a safe conversion from anything to a target-type.

Some special things it does:

  • Strings like "4.2" reliably get converted to int 4 which would otherwise return 0
  • Numbers like 42 reliably converts to bool true which would otherwise return false
  • Numbers like 42.5 reliably convert to strings "42.5" instead of "42,5" in certain cultures
[PublicApi]
public interface IConvertService

Remarks

New in v12.05

Properties

Json

Sub-Service to convert JSON

IJsonService Json { get; }

Property Value

IJsonService

Methods

ForCode(object)

Convert any object safely to string to put into source code like HTML-attributes, inline-JavaScript or similar. This is usually used to ensure numbers, booleans and dates are in a format which works. Especially useful when giving data to a JavaScript, Json-Fragment or an Html Attribute.

  • booleans will be true or false (not True or False)
  • numbers will have a . notation and never a comma (like in de-DE cultures)
  • dates will convert to ISO format without time zone
string ForCode(object value)

Parameters

value object

Returns

string

ForCode(object, string)

Same as ForCode(object), but with fallback, in case the conversion fails.

string ForCode(object value, string fallback = null)

Parameters

value object
fallback string

Returns

string

ToBool(object)

Convert any object safely to bool. This does the same as To<T>(object) but this is easier to type in Razor.

Note that it's called ToBool, not ToBoolean, because the core type is also called bool, not boolean. This is different from System.Convert.ToBoolean(...)

bool ToBool(object value)

Parameters

value object

Returns

bool

ToBool(object, bool)

Convert any object safely to bool, or if that fails, return the fallback value.

Note that it's called ToBool, not ToBoolean, because the core type is also called bool, not boolean. This is different from System.Convert.ToBoolean(...)

bool ToBool(object value, bool fallback = false)

Parameters

value object
fallback bool

Returns

bool

ToDecimal(object)

Convert any object safely to decimal. This does the same as To<T>(object) but this is easier to type in Razor.

decimal ToDecimal(object value)

Parameters

value object

Returns

decimal

ToDecimal(object, decimal)

Convert any object safely to decimal, or if that fails, return the fallback value. This does the same as To<T>(object) but this is easier to type in Razor.

decimal ToDecimal(object value, decimal fallback = 0)

Parameters

value object
fallback decimal

Returns

decimal

ToDouble(object)

Convert any object safely to double. This does the same as To<T>(object) but this is easier to type in Razor.

double ToDouble(object value)

Parameters

value object

Returns

double

ToDouble(object, double)

Convert any object safely to double, or if that fails, return the fallback value. This does the same as To<T>(object) but this is easier to type in Razor.

double ToDouble(object value, double fallback = 0)

Parameters

value object
fallback double

Returns

double

ToFloat(object)

Convert any object safely to float. This does the same as To<T>(object) but this is easier to type in Razor.

Note that it's called ToFloat, not ToSingle, because the core type is also called float, not single. This is different from System.Convert.ToSingle(...)

float ToFloat(object value)

Parameters

value object

Returns

float

ToFloat(object, float)

Convert any object safely to float, or if that fails, return the fallback value. This does the same as To<T>(object) but this is easier to type in Razor.

Note that it's called ToFloat, not ToSingle, because the core type is also called float, not single. This is different from System.Convert.ToSingle(...)

float ToFloat(object value, float fallback = 0)

Parameters

value object
fallback float

Returns

float

ToGuid(object)

Convert any object safely to a Guid This does the same as To<T>(object) but this is easier to type in Razor.

Guid ToGuid(object value)

Parameters

value object

Returns

Guid

ToGuid(object, Guid)

Convert any object safely to standard guid, or if that fails, return the fallback value. This does the same as To<T>(object) but this is easier to type in Razor.

Guid ToGuid(object value, Guid fallback = default)

Parameters

value object
fallback Guid

Returns

Guid

ToInt(object)

Convert any object safely to standard int. This does the same as To<T>(object) but this is easier to type in Razor.

int ToInt(object value)

Parameters

value object

Returns

int

ToInt(object, int)

Convert any object safely to standard int, or if that fails, return the fallback value. This does the same as To<T>(object) but this is easier to type in Razor.

int ToInt(object value, int fallback = 0)

Parameters

value object
fallback int

Returns

int

ToString(object)

Convert any object safely to string. This does the same as To<T>(object) but this is easier to type in Razor.

string ToString(object value)

Parameters

value object

Returns

string

ToString(object, string, NoParamOrder, bool)

Convert any object safely to string - or if that fails, return the fallback value.

This does NOT do the same as To<T>(object, NoParamOrder, T). In the standard implementation would only give you the fallback, if conversion failed. But this ToString will also give you the fallback, if the result is null.

string ToString(object value, string fallback = null, NoParamOrder noParamOrder = default, bool fallbackOnNull = true)

Parameters

value object

The value to convert

fallback string

Fallback in case conversion fails or result is null

noParamOrder NoParamOrder

see Convention: Named Parameters

fallbackOnNull bool

Determine that nulls should also fallback, default is true

Returns

string

To<T>(object)

Convert any object safely to the desired type T. If conversion fails, it will return default(T), which is 0 for most numbers, false for boolean or null for strings or objects.

T To<T>(object value)

Parameters

value object

Returns

T

Type Parameters

T

To<T>(object, NoParamOrder, T)

Convert any object safely to the desired type T. If conversion fails, it will return the fallback parameter as given. Since the fallback is typed, you can usually call this method without specifying T explicitly, so this should work:

var c1 = Convert.To("5", 100); // will return 5
var c2 = Convert.To("", 100);  // will return 100
T To<T>(object value, NoParamOrder noParamOrder = default, T fallback = default)

Parameters

value object
noParamOrder NoParamOrder

see Convention: Named Parameters

fallback T

The value used if conversion fails.

Returns

T

Type Parameters

T