© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
6 replies
Wacku

Is there a better way to do this? (multiple switch statements with repetitive names)

Hey there, I'm new to C#, so forgive me if this question seems a bit stupid.

To Explain, these are lists ObservableCollection lists. if a list has no items, it is not added to the master list. each switch case validates whether or not to add the list.

In the actual application there are 20 lists, but I have included 3 of the statements because they are all the same.

If I am not being clear enough, please let me know, Thank you.

switch (group1.Items.Count)
            {
                case 0:
                    break;
                default:
                    this.AllGroups.Add(group1);
                    break;
            }
            switch (group2.Items.Count)
            {
                case 0:
                    break;
                default:
                    this.AllGroups.Add(group2);
                    break;
            }
            switch (group3.Items.Count)
            {
                case 0:
                    break;
                default:
                    this.AllGroups.Add(group3);
                    break;
            }
switch (group1.Items.Count)
            {
                case 0:
                    break;
                default:
                    this.AllGroups.Add(group1);
                    break;
            }
            switch (group2.Items.Count)
            {
                case 0:
                    break;
                default:
                    this.AllGroups.Add(group2);
                    break;
            }
            switch (group3.Items.Count)
            {
                case 0:
                    break;
                default:
                    this.AllGroups.Add(group3);
                    break;
            }
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

Is there a better way to do this
C#CC# / help
3y ago
Is there a better way to write this QueryBuilder?
C#CC# / help
16mo ago
❔ Is there a better way to implement this "pattern"?
C#CC# / help
3y ago
better way to do this?
C#CC# / help
3y ago