How to convert List<int>
to List<long>
in C#?
The recommended approach to convert a list of one type to another type is using the List<T>. ConvertAll() method. It returns a list of the target type containing the converted elements from the current list.
To convert a list to a string, use Python List Comprehension and the join() function. The list comprehension will traverse the elements one by one, and the join() method will concatenate the list's elements into a new string and return it as output.
Converting int to string in C# is used to convert non-decimal numbers to string character. This can be done by using int to string conversion, int to string with Int32. ToString(), int to string with string concatenation, int to string with StringBuilder, int to string with Convert.
Like this:
List<long> longs = ints.ConvertAll(i => (long)i);
This uses C# 3.0 lambda expressions; if you're using C# 2.0 in VS 2005, you'll need to write
List<long> longs = ints.ConvertAll<int, long>(
delegate(int i) { return (long)i; }
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With