C# 5.0 All-in-One For Dummies
If there is any problem with the .NET Framework from the C# Programmer's perspective, it’s just that there is too much good stuff. A lot of classes have so many properties and events that it's hard to remember what the most used stuff is. Here’s the cream of the crop in the Collection, Connfiguration, and Async classes, all put together for easy reference.
Keeping Stuff in C# Collection Classes
Many variables are for storing one thing, like a number or a sentence. An object can have a lot of properties, storing a lot of information about a single concept. Sometimes, though, you just have to keep track of a big list of stuff. That’s where the collection classes come in.
The collection classes in C# are awesome because they can help you keep a list of items, or a group of keys and values, and provide searching, proctoring, and general maintenance tools for free. That’s a lot easier than writing the two hundredth ‘sort customers by last name’ method this month.
C# has basic collection classes, generic collection classes, and concurrent collection classes. These last ones are for thread safe operations in a multiprocessor environment. All of these collection classes are useful if you know where and when to use them.
Regular collections store untyped stuff for you. If you don’t know exactly what you’re going to store, regular collections are for you. Most everything in these classes are stored as Object.
|ArrayList||This is just a straightforward collection of objects whose size is initially declared, and that size is increased as needed (but not decreased).|
|HashTable||Just a two-column table of keys and values, where the pairs are linked for sorting a retrieval.|
|Queue||A first in, last out (FIFO) list of items. Great for storing program process stuff.|
|Stack||Like a Queue, but Last In First Out (LIFO).|
C# 2.0 introduced generic collections. Generic collections allow you to store typed collections of items, as declared at runtime.
|Dictionary<TKey, TValue>||Just a HastTable, except typed, so you don’t have to store Object, Object. TKey and TValue are types, so you can store a list of <Int32, String> if you want.|
|List<T>||This is the new Array. It’s a typed list of items. By far, the most common collection class used. It’s the default. If you need a list of people, it’s a List<Person>.|
|Queue<T>||Similar to the untyped Queue, but, well, typed!|
|SortedList<TKey, Value>||This awesome Dictionary implements IComparable so that it can be sorted.|
|Stack<T>||Just like the untyped Stack, except typed.|
Concurrent collection classes
Finally, the Concurrent classes make sure that the objects you store in a list are available in any memory space, no matter what processor. It’s a bigger deal than you think.
|BlockingCollection<T>||It’s a thread-safe class that will prevent one thread from altering a field that is accessible by another thread. Brilliant stuff, really.|
|ConcurrentDictionary<TKey, TValue>||Speaking of brilliant, this set of key/value pairs can be accessed by multiple threads.|
|ConcurrentQueue<T>||Remember that FIFO stack? Yeah. This one is generic AND thread safe.|
|ConcurrentStack<T>||The orchestrated collections are useful. They’re beneficial for memory, for one thing. Anyway, this one is LIFO and thread-safe, too.|
A whole namespace of classes is designed to configure applications written in any .NET language, such as C#, VB.NET, or any one of several others. The objects within it are, to say the least, a bit tough to navigate. The configuration in .NET applications takes place in a .config file. This XML formatted file has a <configuration> node, and a whole slew of project specific nodes within. Getting a reference to the configuration requires a call to OpenExeConfiguration.
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration();
Once that is done, getting a reference to a section is pretty straightforward. The GetSection method just takes a string that is the node name, and because the config file is just text, you can grab it anytime.
var sectionName = “Whatever!”; System.Configuration.AppSettingsSection section = (System.Configuration.AppSettingsSection) config.GetSection(sectionName);
You can add sections to the config file using the XML manipulation tools in the framework. You can use a preset group on sections for the corresponding functionality.
|Startup||Describes the .NET version to use.|
|Runtime||Lists the .NET elements to bind to the executable.|
|Network||Describes proxy and other network settings.|
|Cryptography||A place to keep accessible values referencing crypto resources|
|Configuration||The most used section. It’s where you keep custom sections of the config file.|
|Trace/Debug||Two settings that allow you to set up tracing and logging.|
|Application Settings (appsettings)||Application scoped things like database connection strings.|
|Web Settings (websettings)||ASP.NET specific details.|
A lot of Windows 8 is about being fast and fluid. The way to do this is by using asynchronous methods in callbacks from long running functionality. The problem with this is that in C#, the best way to implement an asynchronous method is with threading. But you can’t always depend on when the items from a threading operation will come back. There needs to be some control if you want the users to be able to feel like there is some control.
Enter async and await. async is used to declare an asynchronous function, which returns a Task. All async methods must contain at least one await expression. await tells C# to take referred code and run it in a separate thread than the user thread. The table breaks down the new asynchronous parts of the language.
|async||A modifier that shows the compiler that the function it modifies is asynchronous.|
|await||An operator that suspends the execution of the containing method until the awaited task completes.|
|Task||Represents an asynchronous operation.|
|Task<TResult>||An asynchronous operation that returns a value.|
|Task.ContinueWith||A continuation that is started after the operation in the Task is completed.|