Roslyn: Please add TryParse and ChangeType generic to System.Convert class

Created on 11 Apr 2016  路  2Comments  路  Source: dotnet/roslyn

In addition to ToInt32() and all other parsing things. System.Convert should have generic parser

``` C#
public static bool TryParse(string s,out T v) where T : struct,IConvertible,IFormattable,IComparable,IEquatable
{
return T.TryParse(s,out v);
}

public static T ChangeType(object obj) where T : IConvertible
{
T v;
ChangeType(obj,out v);
return v;
}

public static bool ChangeType(object obj,out T v) where T : IConvertible
{
try
{
/// Should be better algorithm instead of try/catch + unboxing
v = (T)Convert.ChangeType(obj,typeof(T));
return true;
}
catch
{
v = default(T);
return false;
}
}

/// usage

int i;
if(Convert.TryParse("123456",out i))
DoSomething(i);

double d;
if(Convert.ChangeType(i,out d))
DoSomething(d);

```

Area-External Resolution-External

Most helpful comment

This repository is for the language compiler rather than framework libraries.

https://github.com/dotnet/corefx

https://github.com/dotnet/coreclr

All 2 comments

This repository is for the language compiler rather than framework libraries.

https://github.com/dotnet/corefx

https://github.com/dotnet/coreclr

Can you please suggest this on corefx?

Was this page helpful?
0 / 5 - 0 ratings