Tuesday, May 10, 2011

What is a Static Class?

Sometimes you will want to create a class that contains only static members and that cannot be instantiated. This is often the case when creating utility classes, or as an alternative to using the singleton design pattern. When using the .NET framework version 1.1, this is achieved by decorating every member declaration in the class with the "static" modifier and by including only private constructors.

In C# version 2.0, Microsoft introduced static classes to provide this behaviour using simpler code. A static class is simply a class whose definition is decorated with the "static" modifier. Once declared in this manner, the class may only contain static members. If any of the class' members is not declared as static, the compiler will report an error.

Static classes provide some benefits over standard classes. Firstly, the compiler ensures that no instance members are added accidentally. Secondly, the program can have improved performance because the overhead of creating objects is removed. However, static classes should be used with care, as they can be more difficult to mock for automated testing purposes.

A good example of the use of static classes is the Math class in the System namespace. In .NET 1.1, this was a sealed class with no public constructor. In .NET 2.0, this has been modified and is now a static class.

Creating a Static Class
To create a static class, the definition is prefixed with the "static" keyword. In the following example, the class holds a single system setting for an application. This setting holds the maximum number of files that can be used by the program and is controlled by a static property. The property is initialised in the static constructor.

public static class SystemSettings
{
static int _maximumFiles;

public static int MaximumFiles
{
get { return _maximumFiles; }
set { _maximumFiles = value; }
}

static SystemSettings()
{
_maximumFiles = 20;
}
}The members of the static class can be called without first creating an object. Instead, the name of the member is prefixed with the class name and a full stop (period), as can be demonstrated using the following code:

int maxFiles = SystemSettings.MaximumFiles;
Console.WriteLine("File limit = {0}", maxFiles); // Outputs "File limit = 20"

Tuesday, May 3, 2011

C# Named Parameters

With the introduction of C# 4.0, named parameters have been included in the language. Named parameters can be used to enhance the readability of source code and simplify some calls by specifying which parameters the argument values refer to.

What are Named Parameters?

In C# 3.0 and earlier versions, the arguments provided when calling a method would be evaluated in the order in which they appeared in the method's signature. By default, this is also the means by which argument lists are supplied in C# 4.0. However, it is now possible to associate individual arguments with specific parameters when calling a method, constructor, indexer or delegate by using named parameters.
When using a named argument, you provide the name of the parameter that you wish to pass a value to within the method call. For calls that pass literal values, this can improve the readability of your source code. Instead of viewing the method signature or displaying Intellisense for the call, the parameter names can make the literals instantly understood.
We can demonstrate this with an example. The MessageSender class below includes a single method that simulates sending a message by outputting to the console. The parameters allow some text to be provided, along with two formatting options.
class MessageSender
{
    public bool SendMessage(string message, bool useQuotes, bool capitalise)
    {
        message = capitalise ? message.ToUpper() : message;
        message = useQuotes ? string.Format("\"{0}\"", message) : message;
        Console.WriteLine(message);
        return true;
    }
}
When calling the SendMessage method without named parameters, the following code could be used:
var sender = new MessageSender();
sender.SendMessage("Hello", false, true);
At first glance, the "message" parameter is reasonably simple to understand. The effect of the two Boolean values is less easy to determine without viewing the method's signature. However, we can use named parameters for all three arguments to make their purposes obvious. This is achieved by adding the parameter name followed by a colon (:) prior to the value for each argument, as shown below:
sender.SendMessage(message: "Hello", useQuotes: false, capitalise: true);
When you are using named parameters, the arguments for a call no longer need to be passed in the order in which they appear in the member's signature. You are free to provide them in any order, allowing you to make the purpose of a call even more obvious in some situations. The following example might not improve readability but does show that the order of named arguments is unimportant to the compiler.
sender.SendMessage(capitalise: true, useQuotes: false, message: "Hello");
You can mix named and unnamed arguments in a call as long as all of the unnamed arguments appear before any named ones. The unnamed arguments will be applied in the order that they appear in the method's signature. This allows code such as the following, where the first argument's purpose is obvious but where the readability of the two Booleans is improved by the naming:
sender.SendMessage("Hello", useQuotes: false, capitalise: true);

Named Arguments with Optional Parameters

Named parameters provide more flexibility when used with optional parameters. When you make calls to members that include several optional parameters, you can omit any parameter whilst supplying values to any others. To demonstrate, adjust the test method as follows to make the two Boolean properties optional:
public bool SendMessage(string message, bool useQuotes = false, bool capitalise = false)
{
    message = capitalise ? message.ToUpper() : message;
    message = useQuotes ? string.Format("\"{0}\"", message) : message;
    Console.WriteLine(message);
    return true;
}
Calls to the above method that do not use named parameters must provide the arguments in the correct order. This would prevent you from supplying a value for the capitalise parameter whilst using the default value for useQuotes. With named parameters, this restriction is removed:

sender.SendMessage("Hello", capitalise: true);

3 essential skills for IT professionals

Whether you are preparing for a career in information technology (IT) or you are a seasoned professional, it's important to know what skill needs are emerging in the marketplace. As I review the technology and business landscape, I've made some observations about what I believe will be increasingly valuable proficiencies to bring to the table.
Demand for certain skills or an increased focus in specific areas is being motivated by drivers such as the commoditization of IT, which is moving many countries to more right-brained jobs economies; by the data deluge, which is presenting considerable opportunity to understand business in completely new ways; and by rigorous competition in the marketplace, which is forcing greater velocity in the generation of new service and product ideas.
Many roles within IT will continue to be valuable but may be more sensitive in the long run to the business landscape shifts we are experiencing. Rather than a decreasing need overall, I predict we'll continue to see a greater role for IT in the future as well as IT skills being an important part of almost every information worker's inventory of capabilities.
It's also fair to point out that we'll see new skills emerge that we can't even imagine right now. For example, in the mid-1990s it would have been near impossible to predict skills in search engine optimization (SEO) or the whole range of IT careers that have spawned from social media.
The following three skill areas will find high demand in the marketplace either as standalone careers or in combination with other skills.

1. Coordination


In the context of IT, coordination is a skill set that provides guidance and oversight for the smooth interaction of multiple activities and their positive outcomes. It certainly includes project management, but it's not limited to it. People who can bridge relationships between disparate participants, such as developers in an offshore location and testers at a local facility; accommodating cultural differences, advocating for collective success, and expediting answers to questions and concerns, offer significant value.
The IT coordination skills can equally live in the business, the IT organization, or in a third-party provider. In a world where achieving results can often require the participation of a multitude a loosely related resources, effective coordination skills are paramount.
Acquiring great coordination proficiency certainly comes with experience, but preparation should include focusing on negotiation skills and communications in general; problem solving techniques; understanding the fundamentals of project management; and acquiring time management and prioritization methods.

2. Analysis


Our digital world is creating mountains of new data. In fact, we are experiencing exponential growth in its volume. As an example, every two days now, we create as much information as we did from the dawn of civilization up until 2003. It's both a challenge and an opportunity. The challenge is clearly making sense of it. The opportunity is using findings in the data for competitive advantage.
It's becoming clear that large volumes of data can reveal new insights that were previously unknown. As examples, analysis performed on unstructured data scattered across the web can reveal sentiment on people and products. Examining the patterns within social network connections can tell us a lot about where authority resides.
It's within this new context that we see demand for people with skills to identify and extract valuable data; perform extensive analysis on it; discover patterns and hidden secrets contained within; and make sense of it for decision-making purposes.
To acquire these skills includes training in critical thinking, analysis tools, presenting quality communications through writing and visualization, and statistics.

3. Innovation


We've seen large parts of IT turn into commoditized products and services. As an example, email is not a competitive advantage and it's largely dominated by one vendor. Whether you keep that capability and its attendant skills in-house is largely a cost and risk decision. Many organizations are reviewing their internal IT capabilities and concluding, that unless they are creating new value and a distinctive advantage, they simply remain a necessary cost center.
IT leaders are being tasked to reduce the cost center component to a minimum while ramping up the competitive elements of technology. The c-suite is requiring the IT organization to commit the biggest percentage of their available capacity to partnership activities with the business in creating new opportunities. It's this driver that is increasing the demand for innovation skills.
Innovation is the most abstract of the three skill areas in this blog as it is often the hardest to quantify. But it does include a wide range of skills that contribute to the conversion of ideas into net new business value. These include research, applied research, product evaluation and recommendations, problem solving, championing a new idea, and building a business case for investment that includes cost-benefit analysis.

As you consider your IT career, you might conclude that none of these skills are central to your interest. That's okay, too. My view is that, should you choose another IT path, it's still worth considering whether any of these three areas can complement your core interest. Whether you want to be or continue to be a programmer, business analyst, system administrator, or quality assurance analyst, adding one or more of the skills above can only add to your advantage.
We're guaranteed that the needs of the IT jobs marketplace will continue to change, but if each of us is ready to acquire new skills, a career in IT will remain one of the most lucrative and exciting of the professions.