本文整理汇总了C#中Option.ValueOr方法的典型用法代码示例。如果您正苦于以下问题:C# Option.ValueOr方法的具体用法?C# Option.ValueOr怎么用?C# Option.ValueOr使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Option
的用法示例。
在下文中一共展示了Option.ValueOr方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ToDateTime
#pragma warning restore 1573
// TODO-IG: Test with format provider set to invariant culture.
#pragma warning disable 1573 // Parameter does not have XML comment.
/// <summary>
/// Converts the specified string representation of a date and time to its <see cref="DateTime"/> equivalent using the specified format and culture-specific format information.
/// The return value indicates whether the conversion succeeded or failed.
/// </summary>
/// <remarks>
/// <para>
/// If the <paramref name="format"/> is specified (Some) the <paramref name="value"/> must match the specified format exactly.
/// If the <paramref name="format"/> is not specified (None) the method will try to convert the <paramref name="value"/> by using any of the standard .NET date and time format string (see <see cref="StandardDateTimeFormats"/>).
/// </para>
/// <para>
/// If the <paramref name="formatProvider"/> is not specified, the method will use the current thread culture (<see cref="CultureInfo.CurrentCulture"/>) as the format provider.
/// </para>
/// <para>
/// Date and time are returned as a Coordinated Universal Time (UTC).
/// If the <paramref name="value"/> denotes a local time, through a time zone specifier or <see cref="DateTimeAssumption.AssumeLocal"/>,
/// the date and time are converted from the local time to UTC.
/// If the input string denotes a UTC time, through a time zone specifier or <see cref="DateTimeAssumption.AssumeUniversal"/>, no conversion occurs.
/// </para>
/// <para>
/// The <see cref="DateTime.Kind"/> of the returned <see cref="DateTime"/> object is always <see cref="DateTimeKind.Utc"/>.
/// </para>
/// </remarks>
/// <inheritdoc cref="ToInt32(Option{string})" source="param[name='value']"/>
/// <inheritdoc cref="ToDateTime(Option{string}, Option{string})" source="param[name='format']"/>
/// <param name="formatProvider">
/// An object that supplies culture-specific formatting information about the <paramref name="value"/>.
/// If None, the method will use the current thread culture (<see cref="CultureInfo.CurrentCulture"/>).
/// </param>
/// <param name="dateTimeAssumption">An assumption about the date time stored in the <paramref name="value"/>; is it stored as a local or UTC time.</param>
/// <returns>
/// Date time value equivalent to the <see cref="DateTime"/> contained in the <paramref name="value"/> if the conversion succeeded.
/// <br/>-or-<br/>null if the <paramref name="value"/> is None option.
/// <br/>-or-<br/>null if the conversion failed.<br/>
/// In case of successful conversion, the returned date time will have <see cref="DateTime.Kind"/> set to <see cref="DateTimeKind.Utc"/>.
/// </returns>
/// <inheritdoc cref="ToDateTime(Option{string})" source="seealso"/>
public static DateTime? ToDateTime(Option<string> value, Option<string> format, Option<IFormatProvider> formatProvider, DateTimeAssumption dateTimeAssumption)
{
// TODO-IG: Check that dateTimeAssumptio is a valid enumeration value.
// The TryParseExact() fails if the string parameter is null.
// That means we don't need additional check if the value is None.
DateTime result;
return DateTime.TryParseExact(value.ValueOrNull,
format.IsNone ? StandardDateTimeFormats.AllStandardDateTimeFormats : new [] { format.Value },
formatProvider.ValueOr(CultureInfo.CurrentCulture),
DateTimeAssumptionHelper.ToDateTimeStyles(dateTimeAssumption) | DateTimeStyles.AdjustToUniversal,
out result) ? result : (DateTime?)null;
}