© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
10 replies
Radio

✅ List gets weird numbers

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);
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
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ inputing numbers into list
C#CC# / help
3y ago
❔ ✅ how to sort numbers in list without using funcitons
C#CC# / help
4y ago
trying to create a function that returns the list from even numbers to odd numbers
C#CC# / help
3y ago
❔ ✅ Inputing numbers
C#CC# / help
3y ago