A Deeper Dive into C#

Posted On Mar 17, 2021 |

2019-02-28 09:16 am

Have you ever taken a deep dive into C#? If you’re like me, you probably have been programming for a while and wonder why you would ever want or need to do that. As part of my personal growth plan for this year, I’m trying to achieve recertification for MCSD [https://www.microsoft.com/en-us/learning/mcsd-app-builder-certification.aspx], which I held as of 2007, and is currently “expired” with no ability to update. I figured I'd start with C#, and I recently passed the first of three exams on my quest to MCSD and ultimately MCT. The exam I passed is the 70-483: Programming in C# exam https://www.microsoft.com/en-us/learning/exam-70-483.aspx. To get ready, I had to spend some time honing my C# skills. I would like to take a moment to share a couple of interesting things with you that I learned along the way.

One of the topics that I haven’t used a lot, but found fascinating, is multi-threading and asynchronous processing. By now you’ve likely encountered Task and await operators to work with code asynchronously. Of course we can go much deeper than this.

One thing I didn’t know much (if anything) about before studying this material is the ability to do continuations on tasks, and to work with groups of tasks. For example, we can use the “WaitAll” operator to make sure that a group of tasks have all completed before an operation is completed or continued. Additionally, we can do a continuation using a “WhenAll” operator. Finally, we can do a “WhenAny” operation if we want to just have at least one thread complete out of the group before continuing with program execution. Another interesting object is a “TaskFactory”, which allows grouping threads together for common execution. Using a TaskFactory could be very handy for some of those long-running tasks that we might want to execute concurrently, perhaps running separate calculations or long-running algorithms, and then continuing with processing when all the threads have completed.

In addition to the basic multi-threading operations, the other concept I want to share today is the idea of using Parallel operations for basic iteration. Imagine we have a very large group of objects on which we need to perform an O(n) search algorithm [unsorted, need to find by some key, no binary option, etc]. In a case where order of iteration is irrelevant -- as it is when we’re just brute-force finding an object in the collection -- we should be using Parallel.Foreach. If we are not, we’ve likely been missing out on a performance gain.

While I was studying, I created a program that shows these concepts (and more) in action, if you’re interested in seeing more or learning more about these concepts, take a look at my program here: https://github.com/blgorman/70483_MultiThreadingStudy. Full disclosure: The code that I wrote is mostly verbatim from the book “Exam Ref 70-483: Programming in C# [1st Edition by Wouter de Kort]”. Of particular interest per above would be example 19 in my program.

Happy Coding!

Categories: C#, Programming, 70-483, Multi-Threading