© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
17 replies
Hackswell | SIGS3GV

Modernizing double loops in C#?

In C#, concerning an IEnumerable, is there a better way to handle searching? The code is currently using a double for loop to search in a "radius" around the character.
            for (int dx = -radius; dx <= radius; dx++)
            {
                for (int dy = -radius; dy <= radius; dy++)
                {
                    Vector2 loc = currentLocation + new Vector2(dx, dy);
                    if (location.Objects.ContainsKey(loc) && location.Objects[loc] is T t)
                    {
                        list.Add(t);
                    }
                }
            }
            for (int dx = -radius; dx <= radius; dx++)
            {
                for (int dy = -radius; dy <= radius; dy++)
                {
                    Vector2 loc = currentLocation + new Vector2(dx, dy);
                    if (location.Objects.ContainsKey(loc) && location.Objects[loc] is T t)
                    {
                        list.Add(t);
                    }
                }
            }

I was thinking something along the lines of:
location.Objects is a SerializableDictionary<Vector2, object>
Is it possible to use a Where()/Intersects()/Any()/other()? Maybe by testing overlap with a Rectangle?

Rectangle bubba = new Rectangle((int)location.X, (int)location.Y, radius, radius);

IEnumerable<T> shortlist = location.Objects.Where(?????);
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

❔ C# double variable
C#CC# / help
3y ago
✅ help with loops, new to c#
C#CC# / help
3y ago
❔ Possibilities in simplifying these loops?
C#CC# / help
3y ago