I am looking for a better way to update values in my List/Array:
My code is like –
String[] values = baseValue.Split(new []{'+'});
Because the ‘values’ can have decimal/string values – I want to update items in this list to remove any value after decimal –
for (int i = 0; i < values.Count(); i++)
{
decimal newvalue;
if (Decimal.TryParse(values[i], out newvalue))
{
values[i] = Decimal.ToInt32(newvalue).ToString();
}
}
Is there a better way to achieve above using Linq/without Linq ?
You could use string operations, instead of converting to
decimalthen tointand finally back tostring.Of cause no rounding occurs here.
String.Splithas an overload with one argumentparams char[] separator. Because of theparamskeyword, you can specify as many characters as you want, without having to create an array explicitly.Use
values.Lengthwhich is a property built into array types.values.Count()is a LINQ extension method, which enumerates all the array items and is therefore not efficient.