Quick Tip: Remove items within a Range from C# Generic

Recently I’ve just been so busy I’m finding it hard to simply sit down and write a tutorial. To get back into the swing of things, I just made a new project (I was already in Visual Studio…) and put this little bit together. It’s actually a technique I found on my own yesterday while working in an actual application. I just knew that there must be a better way to remove values within a set of parameters! For this quick example, I’ll use a list generic of type int where I populate with natural numbers up to 20.

Lets start with the main function, as that’s where most of the code is:

        // Define our list generics
        private static List<int> _listOfInts = new List<int>();
        private static List<int> _listOfIntsCopy = new List<int>();

        static void Main(string[] args)
        {
            // Populate values
            PopulateList();

            // Copy so we have something to show what we've done.
            _listOfIntsCopy.AddRange(_listOfInts);

            PrintListsFormatted();

            // Remove all numbers less than or equal to 15, while also being
            // greater than or equal to ten. (Removes 6 numbers)
            int max = 15;
            int min = 10;
            _listOfInts.RemoveAll(i => i <= max && i >= min);

            Console.WriteLine();
            PrintListsFormatted();

            // Pause app so we can see everything
            Console.ReadKey();
        }

To break it down a little, what we are essentially doing is on line 19. Notice the handy expression that really neatens things up for us. If you aren’t familiar with the C# lambda operator, we are basically just taking all the values and removing them if they within certain parameters. Think of the lambada as more of a boolean expression in this case. Picture an if statement if you are still having trouble realizing what this means.

Well, that’s it for the quick tip! Hope you find it useful! Below are the functions that aren’t in the main code block above:

        private static void PopulateList()
        {
            for (int i = 1; i <= 20; i++)
            {
                _listOfInts.Add(i);
            }
        }
        private static void PrintListsFormatted()
        {
            Console.WriteLine("Working List with " + _listOfInts.Count + " elements: ");
            foreach (int i in _listOfInts)
            {
                Console.Write(i + " ");
            }

            Console.WriteLine();

            Console.WriteLine("\nCopied List with " + _listOfIntsCopy.Count + " elements: ");
            foreach (int i in _listOfIntsCopy)
            {
                Console.Write(i + " ");
            }

            Console.WriteLine();
        }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s