Adam Weitzman Adam Weitzman - 1 year ago 87
C# Question

Long from List of Characters C#

I have a list of characters

[5, 1, 3]
what is the best way to convert this list to a long like
when I do this:


I get:


Answer Source

The reason you get a FormatException is that the ToArray().ToString()'s output is "System.Char[]" - which is obviously not a float :)

Use string.Join:


Use long.TryParse if there is a risk for non numeric items and use char.IsNumber if you want to keep only numeric items:

char[] charList = new char[] { '5', '1', '3' };
string.Join("", charList.Where(c => char.IsNumber(c)));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download