Radio
Radio
CC#
Created by Radio on 12/1/2023 in #help
✅ List gets weird numbers
c#
string example = "9d4f1";
List<int> ints = new List<int>();

foreach (char c in example)
{
if (Char.IsDigit(c))
{
if (ints.Count != 2) ints.Add(c);
else ints[1] = c;
}
}

foreach (var item in ints) Console.WriteLine(item);
c#
string example = "9d4f1";
List<int> ints = new List<int>();

foreach (char c in example)
{
if (Char.IsDigit(c))
{
if (ints.Count != 2) ints.Add(c);
else ints[1] = c;
}
}

foreach (var item in ints) Console.WriteLine(item);
I was hoping for this to take the first int and the last int, yes there are probably better ways to do this but I am new to c# and want to come up with my own solutions. Atm if I run this code then "ints[0]" comes out as 57 (should be 9) and "ints[1]" comes out as 49 (should be 1) my idea was that it first uses the Add function to add 2 indexes (0 = the first number) then if it found more numbers then 2 if would just replace the last index (1). If anyone know why the values come out as they do, any help would be much appreciated
11 replies