Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
Nullable: System.Double, System.Single (#23503)
Browse files Browse the repository at this point in the history
  • Loading branch information
safern authored Mar 28, 2019
1 parent 652234c commit aca2720
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 54 deletions.
55 changes: 28 additions & 27 deletions src/System.Private.CoreLib/shared/System/Double.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
**
===========================================================*/

#nullable enable
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -122,7 +123,7 @@ public static unsafe bool IsSubnormal(double d)
//
// Returns a value less than zero if this object
//
public int CompareTo(object value)
public int CompareTo(object? value)
{
if (value == null)
{
Expand Down Expand Up @@ -159,7 +160,7 @@ public int CompareTo(double value)

// True if obj is another Double with the same value as the current instance. This is
// a method of object equality, that only returns true if obj is also a double.
public override bool Equals(object obj)
public override bool Equals(object? obj)
{
if (!(obj is double))
{
Expand Down Expand Up @@ -242,22 +243,22 @@ public override string ToString()
return Number.FormatDouble(m_value, null, NumberFormatInfo.CurrentInfo);
}

public string ToString(string format)
public string ToString(string? format)
{
return Number.FormatDouble(m_value, format, NumberFormatInfo.CurrentInfo);
}

public string ToString(IFormatProvider provider)
public string ToString(IFormatProvider? provider)
{
return Number.FormatDouble(m_value, null, NumberFormatInfo.GetInstance(provider));
}

public string ToString(string format, IFormatProvider provider)
public string ToString(string? format, IFormatProvider? provider)
{
return Number.FormatDouble(m_value, format, NumberFormatInfo.GetInstance(provider));
}

public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default, IFormatProvider provider = null)
public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default, IFormatProvider? provider = null)
{
return Number.TryFormatDouble(m_value, format, NumberFormatInfo.GetInstance(provider), destination, out charsWritten);
}
Expand All @@ -275,13 +276,13 @@ public static double Parse(string s, NumberStyles style)
return Number.ParseDouble(s, style, NumberFormatInfo.CurrentInfo);
}

public static double Parse(string s, IFormatProvider provider)
public static double Parse(string s, IFormatProvider? provider)
{
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
return Number.ParseDouble(s, NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.GetInstance(provider));
}

public static double Parse(string s, NumberStyles style, IFormatProvider provider)
public static double Parse(string s, NumberStyles style, IFormatProvider? provider)
{
NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
Expand All @@ -296,15 +297,15 @@ public static double Parse(string s, NumberStyles style, IFormatProvider provide
// PositiveInfinity or NegativeInfinity for a number that is too
// large or too small.

public static double Parse(ReadOnlySpan<char> s, NumberStyles style = NumberStyles.Float | NumberStyles.AllowThousands, IFormatProvider provider = null)
public static double Parse(ReadOnlySpan<char> s, NumberStyles style = NumberStyles.Float | NumberStyles.AllowThousands, IFormatProvider? provider = null)
{
NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
return Number.ParseDouble(s, style, NumberFormatInfo.GetInstance(provider));
}



public static bool TryParse(string s, out double result)
public static bool TryParse(string? s, out double result)
{
if (s == null)
{
Expand All @@ -320,7 +321,7 @@ public static bool TryParse(ReadOnlySpan<char> s, out double result)
return TryParse(s, NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.CurrentInfo, out result);
}

public static bool TryParse(string s, NumberStyles style, IFormatProvider provider, out double result)
public static bool TryParse(string? s, NumberStyles style, IFormatProvider? provider, out double result)
{
NumberFormatInfo.ValidateParseStyleFloatingPoint(style);

Expand All @@ -333,7 +334,7 @@ public static bool TryParse(string s, NumberStyles style, IFormatProvider provid
return TryParse((ReadOnlySpan<char>)s, style, NumberFormatInfo.GetInstance(provider), out result);
}

public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider provider, out double result)
public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider? provider, out double result)
{
NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
return TryParse(s, style, NumberFormatInfo.GetInstance(provider), out result);
Expand All @@ -353,77 +354,77 @@ public TypeCode GetTypeCode()
return TypeCode.Double;
}

bool IConvertible.ToBoolean(IFormatProvider provider)
bool IConvertible.ToBoolean(IFormatProvider? provider)
{
return Convert.ToBoolean(m_value);
}

char IConvertible.ToChar(IFormatProvider provider)
char IConvertible.ToChar(IFormatProvider? provider)
{
throw new InvalidCastException(SR.Format(SR.InvalidCast_FromTo, "Double", "Char"));
}

sbyte IConvertible.ToSByte(IFormatProvider provider)
sbyte IConvertible.ToSByte(IFormatProvider? provider)
{
return Convert.ToSByte(m_value);
}

byte IConvertible.ToByte(IFormatProvider provider)
byte IConvertible.ToByte(IFormatProvider? provider)
{
return Convert.ToByte(m_value);
}

short IConvertible.ToInt16(IFormatProvider provider)
short IConvertible.ToInt16(IFormatProvider? provider)
{
return Convert.ToInt16(m_value);
}

ushort IConvertible.ToUInt16(IFormatProvider provider)
ushort IConvertible.ToUInt16(IFormatProvider? provider)
{
return Convert.ToUInt16(m_value);
}

int IConvertible.ToInt32(IFormatProvider provider)
int IConvertible.ToInt32(IFormatProvider? provider)
{
return Convert.ToInt32(m_value);
}

uint IConvertible.ToUInt32(IFormatProvider provider)
uint IConvertible.ToUInt32(IFormatProvider? provider)
{
return Convert.ToUInt32(m_value);
}

long IConvertible.ToInt64(IFormatProvider provider)
long IConvertible.ToInt64(IFormatProvider? provider)
{
return Convert.ToInt64(m_value);
}

ulong IConvertible.ToUInt64(IFormatProvider provider)
ulong IConvertible.ToUInt64(IFormatProvider? provider)
{
return Convert.ToUInt64(m_value);
}

float IConvertible.ToSingle(IFormatProvider provider)
float IConvertible.ToSingle(IFormatProvider? provider)
{
return Convert.ToSingle(m_value);
}

double IConvertible.ToDouble(IFormatProvider provider)
double IConvertible.ToDouble(IFormatProvider? provider)
{
return m_value;
}

decimal IConvertible.ToDecimal(IFormatProvider provider)
decimal IConvertible.ToDecimal(IFormatProvider? provider)
{
return Convert.ToDecimal(m_value);
}

DateTime IConvertible.ToDateTime(IFormatProvider provider)
DateTime IConvertible.ToDateTime(IFormatProvider? provider)
{
throw new InvalidCastException(SR.Format(SR.InvalidCast_FromTo, "Double", "DateTime"));
}

object IConvertible.ToType(Type type, IFormatProvider provider)
object IConvertible.ToType(Type type, IFormatProvider? provider)
{
return Convert.DefaultToType((IConvertible)this, type, provider);
}
Expand Down
55 changes: 28 additions & 27 deletions src/System.Private.CoreLib/shared/System/Single.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
**
===========================================================*/

#nullable enable
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -117,7 +118,7 @@ public static unsafe bool IsSubnormal(float f)
// null is considered to be less than any instance.
// If object is not of type Single, this method throws an ArgumentException.
//
public int CompareTo(object value)
public int CompareTo(object? value)
{
if (value == null)
{
Expand Down Expand Up @@ -189,7 +190,7 @@ public int CompareTo(float value)
return left >= right;
}

public override bool Equals(object obj)
public override bool Equals(object? obj)
{
if (!(obj is float))
{
Expand Down Expand Up @@ -233,22 +234,22 @@ public override string ToString()
return Number.FormatSingle(m_value, null, NumberFormatInfo.CurrentInfo);
}

public string ToString(IFormatProvider provider)
public string ToString(IFormatProvider? provider)
{
return Number.FormatSingle(m_value, null, NumberFormatInfo.GetInstance(provider));
}

public string ToString(string format)
public string ToString(string? format)
{
return Number.FormatSingle(m_value, format, NumberFormatInfo.CurrentInfo);
}

public string ToString(string format, IFormatProvider provider)
public string ToString(string? format, IFormatProvider? provider)
{
return Number.FormatSingle(m_value, format, NumberFormatInfo.GetInstance(provider));
}

public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default, IFormatProvider provider = null)
public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default, IFormatProvider? provider = null)
{
return Number.TryFormatSingle(m_value, format, NumberFormatInfo.GetInstance(provider), destination, out charsWritten);
}
Expand All @@ -274,26 +275,26 @@ public static float Parse(string s, NumberStyles style)
return Number.ParseSingle(s, style, NumberFormatInfo.CurrentInfo);
}

public static float Parse(string s, IFormatProvider provider)
public static float Parse(string s, IFormatProvider? provider)
{
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
return Number.ParseSingle(s, NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.GetInstance(provider));
}

public static float Parse(string s, NumberStyles style, IFormatProvider provider)
public static float Parse(string s, NumberStyles style, IFormatProvider? provider)
{
NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
return Number.ParseSingle(s, style, NumberFormatInfo.GetInstance(provider));
}

public static float Parse(ReadOnlySpan<char> s, NumberStyles style = NumberStyles.Float | NumberStyles.AllowThousands, IFormatProvider provider = null)
public static float Parse(ReadOnlySpan<char> s, NumberStyles style = NumberStyles.Float | NumberStyles.AllowThousands, IFormatProvider? provider = null)
{
NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
return Number.ParseSingle(s, style, NumberFormatInfo.GetInstance(provider));
}

public static bool TryParse(string s, out float result)
public static bool TryParse(string? s, out float result)
{
if (s == null)
{
Expand All @@ -309,7 +310,7 @@ public static bool TryParse(ReadOnlySpan<char> s, out float result)
return TryParse(s, NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.CurrentInfo, out result);
}

public static bool TryParse(string s, NumberStyles style, IFormatProvider provider, out float result)
public static bool TryParse(string? s, NumberStyles style, IFormatProvider? provider, out float result)
{
NumberFormatInfo.ValidateParseStyleFloatingPoint(style);

Expand All @@ -322,7 +323,7 @@ public static bool TryParse(string s, NumberStyles style, IFormatProvider provid
return TryParse((ReadOnlySpan<char>)s, style, NumberFormatInfo.GetInstance(provider), out result);
}

public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider provider, out float result)
public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider? provider, out float result)
{
NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
return TryParse(s, style, NumberFormatInfo.GetInstance(provider), out result);
Expand All @@ -343,77 +344,77 @@ public TypeCode GetTypeCode()
}


bool IConvertible.ToBoolean(IFormatProvider provider)
bool IConvertible.ToBoolean(IFormatProvider? provider)
{
return Convert.ToBoolean(m_value);
}

char IConvertible.ToChar(IFormatProvider provider)
char IConvertible.ToChar(IFormatProvider? provider)
{
throw new InvalidCastException(SR.Format(SR.InvalidCast_FromTo, "Single", "Char"));
}

sbyte IConvertible.ToSByte(IFormatProvider provider)
sbyte IConvertible.ToSByte(IFormatProvider? provider)
{
return Convert.ToSByte(m_value);
}

byte IConvertible.ToByte(IFormatProvider provider)
byte IConvertible.ToByte(IFormatProvider? provider)
{
return Convert.ToByte(m_value);
}

short IConvertible.ToInt16(IFormatProvider provider)
short IConvertible.ToInt16(IFormatProvider? provider)
{
return Convert.ToInt16(m_value);
}

ushort IConvertible.ToUInt16(IFormatProvider provider)
ushort IConvertible.ToUInt16(IFormatProvider? provider)
{
return Convert.ToUInt16(m_value);
}

int IConvertible.ToInt32(IFormatProvider provider)
int IConvertible.ToInt32(IFormatProvider? provider)
{
return Convert.ToInt32(m_value);
}

uint IConvertible.ToUInt32(IFormatProvider provider)
uint IConvertible.ToUInt32(IFormatProvider? provider)
{
return Convert.ToUInt32(m_value);
}

long IConvertible.ToInt64(IFormatProvider provider)
long IConvertible.ToInt64(IFormatProvider? provider)
{
return Convert.ToInt64(m_value);
}

ulong IConvertible.ToUInt64(IFormatProvider provider)
ulong IConvertible.ToUInt64(IFormatProvider? provider)
{
return Convert.ToUInt64(m_value);
}

float IConvertible.ToSingle(IFormatProvider provider)
float IConvertible.ToSingle(IFormatProvider? provider)
{
return m_value;
}

double IConvertible.ToDouble(IFormatProvider provider)
double IConvertible.ToDouble(IFormatProvider? provider)
{
return Convert.ToDouble(m_value);
}

decimal IConvertible.ToDecimal(IFormatProvider provider)
decimal IConvertible.ToDecimal(IFormatProvider? provider)
{
return Convert.ToDecimal(m_value);
}

DateTime IConvertible.ToDateTime(IFormatProvider provider)
DateTime IConvertible.ToDateTime(IFormatProvider? provider)
{
throw new InvalidCastException(SR.Format(SR.InvalidCast_FromTo, "Single", "DateTime"));
}

object IConvertible.ToType(Type type, IFormatProvider provider)
object IConvertible.ToType(Type type, IFormatProvider? provider)
{
return Convert.DefaultToType((IConvertible)this, type, provider);
}
Expand Down

0 comments on commit aca2720

Please sign in to comment.