You might not need to use multi-threading in all of your C# applications but as a modern software developer, you should make this part of your toolkit.
- Wikipedia best explains what thread is. It also explains the difference between threads and processes and delves into the topic of multi-threading as well, which is what thread is for.
- Benefits of using multi-threading:
- To maintain a responsive user interface.
- To perform CPU bound work while waiting for I/O operations to complete.
- To scale the application using parallel execution.
- Price to pay for using multi-threading:
- Slower execution time on single-processor machines due to context switching.
- Added program complexity.
- Threading (C#) at Microsoft docs describes the basic concurrency and synchronization mechanisms provided by the .NET Framework, but not much example C# code though.
- Threading in C# by Joseph Albahari is a good one as it provides example C# code. It goes through the basics of threading and synchronization in C# which would help you get started writing C# code. Then it talks about the event-based asynchronous pattern (EAP) and lastly about parallel programming.
- C# Programming Examples on Threads is the simplest C# codes I found on C# threading.
- Microsoft recommends using the task-based asynchronous pattern (TAP) though. The async and await keywords in C# support TAP. So I think it would be best to focus on this instead of the older patterns like the event-based asynchronous pattern (EAP) and the asynchronous programming model (APM).
- Parallel Processing and Concurrency in the .NET Framework in Microsoft docs contains links to information about threading, asynchronous programming patterns (both legacies and new) and parallel programming.
- For parallel programming, the Task Parallel Library (TPL) is the center of it all and would be wise to get familiar on this library.
Even though I’m still using VS2013 for all the projects I’m working on, it won’t hurt to know the new features of C# 6 in VS2015. I might start converting some of them or maybe start the new projects in VS2015, who knows.
I won’t try to write about the new features in C# 6 in this post as I am pretty sure you can find them on the Internet. Though there is one that I like and it’s New Language Features in C# 6. I hope this helps.
Microsoft Docs has a What’s new in C# 6 article which I think is where the above article is based on. Also check out their The history of C# article to refresh yourself on the features added to previous versions of C#. There is now also What’s new in C# 7.0 and What’s new in C# 7.1.
I am not referring to the using directive to import types defined in other namespaces, but I am referring to the using statement to define a scope where at the end of it an object will be disposed, such as this:
using (var cn = new SqlConnection())
// your code here...
Just be aware that the using statement is just a shortcut or a convenient syntax for the below code:
var cn = new SqlConnection();
// your code here...
if (cn != null) ((IDisposable)cn).Dispose();
And if you have a try–catch block in your method, either outside or inside the using statement, or maybe even both, then you need to make sure the flow of control and the logic when an exception occurs is what you would expect, because nested try–catch block can become confusing. Just remember the using statement is a try–finally block.
My point is, don’t haphazardly use using statement especially if you are using it for several objects in your method, because depending on what you are trying to achieve, sometimes its better to just use a complete try–catch–finally block to make your code simpler to understand.
Rule of thumb is if the resource object you are trying to use in your method needs to be disposed of by your method then using statement is best. And make sure you are instantiating the resource object inside the using statement and not outside and passing the variable to it. See using Statement (C# Reference).
I don’t know about you, but whenever I switch between C# and VB.NET (at one point I was maintaining desktop applications written in both languages), I always get hit by a brick wall when I go over a VB.NET code, it’s like my fingers are all numb? I guess if you are working on desktop applications only, it does not matter which language you use. But nowadays, developer needs to be versatile and be able to work on web applications too, and if you’re lucky enough, on mobile applications as well.
And you know what? New languages are being created based on the C programming language, which is where C# is based from by the way. You might have known about the new language developed by Microsoft, the TypeScript. And there is Dart from Google as well.
So I say, you wouldn’t go wrong if you go the C# way. And that’s why I now prefer C# over VB.NET.
Well, I’ll tell you. You can define your own strings with Enums using reflection. You can also use the type-safe-enum pattern if reflection seems slow to you. And do you know you can use Enums to represent bit flags?
Associating Enums with strings in C# shows you how to define your own strings with Enums using reflection. For type-safe-enum pattern implementation, check it out here. The ins and outs of C# enums shows you how to use Enums to represent bit flags.