Learn C# and Master Software Development for Windows

learncsharpIt’s the dream of many people to become a computer programmer. Working at one of the top companies in Silicon Valley, or even creating your own programs in your garage, everyone will have a different reason to want to learn these skills. Perhaps you simply want to become more employable, or you are looking for a new challenge at your work. Regardless of the “why” the key to mastering any new skill is determination.

You need the desire to apply yourself, to take your new learning on board, practising and developing your skills. There are many different paths you can take to become a “programmer,” as well as many different languages to learn. If this is all very new to you  brush up on your knowledge of computer science. I know, I know, this course is aimed at children, but for the aspiring computer programmer you won’t find a resource simpler to understand than one delivered by a ten year old. If he can teach, you can learn.

C# is a fantastic language to begin an introduction to programming. It’s Microsoft’s leading object-oriented language, allowing you to build programs and applications for Windows 8. Many people say that first you should learn C, the source for the ideas and structure behind C#. If you want to follow this direction, you have a wide range of courses to teach you both the fundamentals and the language, such as Learn C the Hard Way. It throws you in the deep end, but you will learn an awful lot over the course. I prefer students to start with C#, because its simpler, and easier to use. Once you wrap your head around the fundamentals of C#, you can always study C after.

This guide covers why programming is an essential tool, and what you can do with C# programs. You will learn what programmers love about their jobs, as well as how to get started learning C#. I give tips on staying motivated, outline a couple of resources to aid your learning, walk you through your first program, and dive into a detailed explanation of variables so you understand what it’s like to learn C# for real. What are you waiting for?

What’s so important about learning to program?

Computers today are so advanced, most ordinary people understand very little about how they actually work. It wasn’t always like this. If you owned one of the first computers, you had to understand not only how they worked but also how to build it. In today’s society your TV, smartphone, and car all have computers inside. For the majority of us, we can interact with it, using a remote, your finger or the steering wheel, but that doesn’t give us any comprehension of what is going on behind the scenes.

There’s a long standing joke among computer programmers, “there are 10 types of people in the world: those who understand binary, and those who don’t”

Playing on the different way that programmers and people read, someone with no understanding of binary reads this sentence as “there are ten types…” To someone familiar with machine language, the figure “10” also represents the binary number two. The joke being that only people who can read binary will read your shirt correctly, “there are two types of people in the world…”

Perhaps not very funny, but the point is clear. Not enough people in the world have the skill to work with computers. Professionally, good programmers are in high demand, as more jobs are requiring advanced technical skills. Students that excel in programming, have very bright futures in some of the greatest software companies around. Microsoft, Google and Apple all hire programmers to maintain, build and create new products, with incentives and large salaries to keep them happy. Sounds good to me!

Why is programming fun?

Everyone enjoys different things. A glass of wine after a hard day in the office, heading to the mall for a little shopping, or even watching American Idol. What set’s a good programmer apart, is their love for the profession. This can be said about many jobs, a great doctor enjoys healing their patients, while a fantastic scientist enjoys making new discoveries in their lab.

Behind it all, for a computer programmer the sheer joy of creation is what makes it fun. Like an artist, you take an idea and transform it into being. You develop the concept, no matter how complex, and find the code that will let you achieve it. Working, sometimes for months at a time, testing and implementing your project, nothing is more satisfying than seeing it work for the first time. Building on this, is the reaction from other people to their program. Seeing others take delight in what you have created is immensely rewarding.

For the people who get a thrill from continually learning, programming is without equal. Every day, there are new tools, techniques, platforms and breakthroughs. For anyone who is eager to learn, there will always be another article, tutorial or technology you can study and hone your programming skills. That’s why many people devote their lives and they career’s to this, the truly exceptional programmers are those with a lifelong passion for computers and learning. They just have fun doing it.

What can C# programs do?

The general nature of C# means that you can perform virtually any programming task in this language. From manipulating databases, generating high-speed graphics in a video game, controlling electronic devices and building software, C# has you covered.

One key drawback that developers see in C# is that it’s an interpreted language. For this reason, many still prefer to use C++ in game development. This is a misconception, because the .NET Framework compiles the interpreted code the first time a program is run, making it just as fast as any competing language.

But why C#?

There are so many programming languages you can learn, why should you choose C#?

First, let’s look at the reason it was created. Microsoft developed C# to take the best practises from Java, C++ and C, forming their own mother language for the .NET Framework. Compared to other languages, Java originated as a language to operate toasters, C was designed for programming Operating Systems and PHP for web servers. It makes the most sense to study a language that has been specifically designed for software, using many of the best features of existing languages.

An added benefit, is that any computer which can run a .NET Framework, Linux or the dotGNU project can use programs written in C#. Build your programs in C# and anyone using Windows will be able to make use of your cool new application.

Introduction to the C# Language

Millions of developers worldwide use this language to build applications for Windows. Similar to Java, and the Objective-C used by Apple, C# is an object-oriented programming language. Using the .NET Framework as a base, C# is a straightforward language intended for general purpose programming. As such, it’s fast becoming an industry standard,

Pronounced “C Sharp”, it was developed by Microsoft to take advantage of the best practises from C, C++ and Java. Essentially, Microsoft realised that the .NET framework needed a strong, versatile language, so they created C#. The latest version is C# 5.0, released in August 2012. The name ‘C sharp’ was inspired from the musical notation ‘sharp’ indicating a semitone higher in pitch. It’s similar to C++ in its naming convention where the “++” indicates an increase of 1.

This language is very simple and easy to learn. As you discover the code, you will recognize many features that have been carried over from C, C++ and Java. Programmers that have prior programming knowledge tend to be very fast in picking up C#. If you have never studied one of these languages before, you should familiarize yourself with the concepts we will be talking about in this guide. If you are also interested to learn more about the backbone of C#, this is a great course for beginners to learn the C language. Once you have the foundation in place, you are ready to discover C#.

Why should I learn to program in C#?

First and foremost, it’s easy to learn. The structure and rules build into C# make it easy for beginners to avoid common mistakes, and you can very easily begin building your first program. It’s an industry standard, with a strong following growing each year. In 2012 it’s use expanded 2.3%, far exceeding every other programming language. It’s the language of choice for many programmers, because if has a unique set of features not available in other languages. The built-in functional and asynchronous programming capabilities, native garbage collection and type safety set it apart from other popular programming languages.

Whilst not yet as popular as Java or PHP, C# is steadily growing in application. With the launch of Windows 8, any serious developer wanting to build programs for Windows needs a background in C#. It’s the most widely used language for developing applications both in Windows 8 and on the Windows phone. Why not PHP? This is another language that is very popular. Typically, PHP is used for small web sites and web application, but it is not appropriate to use when developing large and complex systems. PHP is used first and foremost for small projects, being a procedural language it often results in developers creating bad code that is difficult to maintain. If you want to become a serious software engineer, learn C#.

What about C++? Both C++ and C# are derived from the C language. C++ is an acceptable language when programs need to work closely with computer hardware, or have special performance requirements like in 3D games. Otherwise, it is inadequate. C# is much better for building web applications or business software. It’s also much more difficult to code in C++. To write high quality software in this language you need to be an experienced programmer. In C# the same is not strictly required. Learning C++ also takes more time, and because of this C# has becoming the language of choice.

C# is easier to use, more efficient and all around just better than other programming languages out there. As an example, pointers are used extensively in C and C++. But many programmers hit problems when they use pointers incorrectly. It creates errors in their program, and is difficult to debug. Recognizing this problem, in C# you must explicitly invoke the use of pointers through fixed and unsafe keywords. For a beginner programmer, it’s much harder to make errors in C#.

With the backing of Microsoft, you can expect C# to stay around for a long time. Anyone looking for new skills, C# is a great start. Companies need more and more C# savvy programmers. According to simplyhired.com, an average C# starting salary is $54,000. Not a bad start for anyone with student loans to pay off. The most common professions in C# focus on creating business software applications, or writing control software. You could be a software engineer designing systems for nuclear reactors, aircraft and rockets!

For anyone with a background in programming, especially C, C++ or Java, you will find it very easy to learn and master C#. Sure, some of the words will be different, but the structure is fundamentally the same. Think of it like the difference between two dialects of English. An American who orders chips in London may receive french fries instead of crisps, but the basic structure of the language is the same. For this reason, C# is a fantastic language even if you have no prior programming experience. Master the code and you will be better off in the future if you decide to learn C++ or Java at a future date – it works both ways.

There are a ton of resources available to help you learn C#, hundreds of websites and instructional guides to take you through the learning process. I’ll touch on these in a later section specifically focused on how you can learn C#. Programmers wanting to create several different kinds of software can find their needs met in C#. Combined with the .NET Framework, you can build windows based programs, web applications, and even console programs that launch from the command prompt. You will find that it’s easy to learn, and has a great structure that produces efficient programs.

Finally, why not? There’s no good reason not to learn C#, particularly if you are a programmer with some knowledge of C, C++ or Java. The only investment it will take is your time, and the cost you spend on a course or a book. But what you get as return on investment is a working knowledge of one of the most useful, and marketable programming languages. It can help you land a job, build a program or simply boost the skills in your resume.

Introducing the .NET Framework

All C# programs run on the .NET Framework. A core part of Windows, the .NET Frameworks has a virtual execution system using Microsoft’s Common Language Runtime (CLR) version of the common language infrastructure (CLI) to execute and develop environments where languages and libraries work seamlessly together. In English, this means the .NET Framework lets you build and run programs.

C# source code is compiled into an intermediate language that conforms to the CLI specification. Once compiled, the code and all supplementary resources are stored in an executable file (otherwise known as an assembly), using the extension .exe or .dll. When you run a C# program, the assembly is loaded into the CLR, which performs just in time compilation to convert the intermediate language into native machine instructions.

The key feature of the .NET Framework is the large number of languages that can operate on it. Because the intermediate language produced from the C# compiler conforms to standard specifications, C# code can interact with more than 20 other compliant languages. In addition there is an extensive library in the .NET Framework, where over 4000 classes are organised into namespaces that perform all basic functions and controls. Typically a C# assembly uses this library alone to handle common structural requirements in designing a program.

To learn about working with C# and the .NET Framework, Infinite Skills have a great set of video lectures to get you started. Learn what it takes to master the C# language with over 114 lectures and seven hours of content. You will rapidly progress from a novice to making your own programs.

How do I get started with C#

To perform any coding, you will need two programs. An editor to create your code, and a compiler to put it altogether. There are many different options available for a compiler, Microsoft recommends their Visual Studio product. Using this suite of tools, you can create a variety of C# programs. Once you have this installed, you can use the embedded editor to write and edit your code, or you can do this separately in a text editor designed for coding. Notepad++ is the go-to choice for many programmers.

Visual Studio is a powerful integrated environment allowing programmers to develop software for Windows and the .NET framework. It supports many languages and development technology. What is great about Visual Studio, is that it’s an integrated development environment, you can write code, compile it, run, test and debug it all in one place. It’s a commercial platform, if you are looking for something more budget conscious Microsoft also have a free tool for compiling C# called Visual Studio Express.

Building a program is simple. First you create a series of instructions in C#, outlining the specific tasks you want your program to perform. These tasks are called statements, and look like mathematical formulas.

int c=0;

float b = c*3.4+1-;

Once you have your text file complete, you compile it. This process links your C# code into an intermediate language that can be understood by your computer in machine language. In English, the steps your computer follows when the application is run.

Easy.

Ways to learn C#

There are a huge range of options available for students who want to advance their skills in C#.

Many people learn computer programming through computer science or IT degrees at their university. If you are planning to attend college, be sure to take advantage of any C# programming courses available at your campus. Participating in a lecture and being able to ask clarifying questions to the instructor or fellow students will go far in developing your understanding of the language. If you can’t study this at college, you may have night classes in your area that you can enrol in to learn.

The most common way people learn C# is through textbooks and home study. There are many options available on the internet, and many different qualities of textbook. C# 5.0 is a great guide, covering what you need to know to learn C#, but also explaining the “why” behind the scenes. Not just teaching you programs, you build a solid foundation and understand what is causing a program works the way it does.

When learning from textbooks, be sure that you are using a guide to the current version of the language. Many of the resources available for free download are for earlier versions of C#, or are of a lower quality than purchased textbooks. It’s at this point investing money into a good textbook or online course is recommended. The code doesn’t fundamentally change, but newer books incorporate new learning, containing updated examples and code. Critical to both the classroom and home study options, you need to be practising your C# skills regularly in order to be successful.

In line with home study, the internet has a vast array of resources for you to quickly and rapidly progress with C#. Microsoft has official tutorials that run you through the core aspects of this language, which you can follow at your own pace. They provide a wide range of exercises, from very simple tasks to complex problems that take hours or even days to complete. To assist with student learning, there are official Microsoft forums where you can ask any questions you have on the C# language. A quick search in Google highlights you have many other non-microsoft forum options as well.

For more interactivity, Code Academy describes itself as the free alternative to learning to code. A good platform, with a focus on JavaScript and developing web language, they have yet to release a C# course that suits our needs. Keep it in mind though, as you progress there are many ways you can grow your programming skills.

Practise is key

Everything you learn about C# is worthless if you are not practising what you have learnt. Sitting through courses in your university, or spending time reading textbook after textbook is not the same as actually trying it for yourself. To learn to drive, you do much more than read a driving manual. You actually get behind the wheel and learn. Programming is the same. As you are reading, follow along with your tutorials examples and do them yourself in Visual Studio. Practise as many C# problems and exercises as you can, making sure to master every aspect of each before progressing to the following chapter.

Learning a programming language is simple if you apply yourself, but it takes practise to become good. If you want to be a programmer, you need to know that true programmers are serious, persevering and questioning people – able to handle many different kinds of problems. They learn to master many different platforms and languages, with the truly great programmers spending a huge amount of time developing their skills, learning new technologies and new ways to do their job, everyday.

A great programmer is capable of:

  • Logical thinking; coming up with ways to solve problems.

  • Imagination; designing their solutions as a series of steps.

  • Modelling their view of the world using programming language and technology.

  • Implementing their ideas as applications.

  • Testing their programs and being adaptable as change is needed.

If you are really committed to learning C#, don’t just be ordinary. Take the time to learn, practise and hone your skills so that you become truly great.

Motivation

The only person that can motivate you to study C# is you. You alone are responsible for your learning. We have covered the basics, outlining many reasons why you should learn the C# language. There is a huge potential for you to develop this skill through readily accessible people, resources and tutorials. Whether or not you do, all comes down to you. Ask yourself. “Why do I want to learn this language?” Do you want to build your skills as a programmer? Is it the first step you are taking in a long career of software development? Do you just want to build an app for the Windows phone?

Crystallize what it means to you to be able to successfully create any program you want in C#. Use this as your motivation to practise coding every day. I say every day, because you will need to use this language every day to master it. At the beginning, perhaps you only dedicate 10-15 minutes. As you learn more, build up over time until you are using it for 1-2 hours every day. Over the course of a year, you will soon have a fantastic foundation of skills. You will probably be better than most programmers with a years experience at this point. Without practise, and the motivation to keep practising, soon you will forget the programming languages you have learnt.

Hello World Example

A staple of computer programming, nearly every textbook on the subject uses a simple program as the introduction to the language. This is an inheritance from Bell Laboratories, where Brian Kernighan created a program using the C language to output the text “hello, world”

In C#, the code is a little different to Brian’s, but the output is the same. This is the code:

using System;
class Program
{
   static void Main()
   {
       Console.Write(“Hello world!”);
   }
}

We spoke earlier on the comparison between C# and Java, as an example of the similarities, the Hello World program in Java uses this code:

public class Hello {
  public static void main (String args[]){
     System.out.printIn(“Hello World!”);
  }
}

Both of these programs will output “Hello World!” in your console. To understand what is happening, let’s look at each line of the C# code in detail.

using System;

This code tells the computer to use System as a candidate prefix for types used in the source code. The word using is a keyword to include the System namespace in the program, Typically, a program will have many using statements. The using statement allows a programmer to state all candidate prefixes during compilation.

class Program

This is a class declaration, that Program is our class and what follows is the data and method definitions our Program uses.

static void Main ()

This is where the class member method is defined, and the program begins to run. The keyword static makes the method accessible without an instance of Program, and each of the applications Main entry points must be declared static, otherwise the program would have a circular dependency. If an entry point is not static, the program requires an instance, but any instance requires a program! If you make this mistake, C# compilers will report an error if the Main method is not static (if you were using C or C++, it would be much harder to debug!). Finally, the void keyword determines that there is no return value for Main. Main gets your program ready to run, letting you set the scene for what statements you wish the program to follow.

Console.Write(“Hello world!”);

This line generates the output. Console is a static class in the System namespace, providing an interface to input, output and error streams for console applications. This uses the Console method WriteLine, to display the string “Hello World!” in the console. Success!

All C# programs contain the following basic parts, a clear structure that must be followed:

  • Namespace declaration

  • Class declaration

  • Class methods & attributes

  • A Main method

  • Statements and expressions of the program

  • Comments

Let’s make this a little bit more fun, it’s time to try this one for yourself, Start Visual Studio, and open a new project. Select the Visual C# template, and you want it on Windows. Choose Console Application, create a name for your project and click OK. You should have a new project appear in Solution Explorer. Copy the following code (I’ve amended this slightly to create a pop-up window instead):

using System;
namespace HelloWorldApplication
{
  class HelloWorld
  {
     static void Main(string[] args)
     {
       Console.WriteLine(“Hello World”);
       Console.ReadKey();
     }
  }
}

Click the Run button or the F5 key. A command prompt window will appear that contains the words “Hello World”. Success! Your first C# program!

Understanding Variables

One of the key parts of any programming language is its use of variables. Used to store data from a program in memory, variables are temporary locations to do just that. Data values are called variables as it allows the value to vary during the life of an application.

In C# there are many different components to learn in mastering the language. This final chapter will provide an introduction and example of a basic tutorial on variables, similar to what you would find in a beginners course on C#. I want to demonstrate here that whilst there are many things to learn, a programming language is not rocket science. Despite never having learnt a computer language before, all that you need to do is study and practise.

In C# the main types of variables are:

  • Numbers                          You have both integers and floating points

  • Enumerated Types      A limited set of integer values

  • BOOL                                 A true/false statement

  • Characters                      Char

  • Text Strings                    Like a sentence

  • Arrays                              A multi-dimensional list of variables

  • Structs                              A conglomerate variable

  • Nullable types

  • Objects                             A “bunch” of data with functions that control the data

In C# there are two types of numbers. Whole numbers are called integers, or int for short, whilst numbers that have a decimal place are called floating point numbers or float. Integers are categorized into several sizes depending on how many bytes of storage they require, and whether they can hold negative numbers (signed), or not (unsigned). Typically for integers, the signed 32 bit type will be all you need for 90% of the applications you create, especially if you are using a 32 bit CPU.

Enumerated types (commonly called enum), are based on integers. Their benefit is that instead of using int directly, you can create a smaller scope of possible values. Take for example, the seven colours in a rainbow. You could use int with values 0-6 for the values, but if you accidentally assign a value of 7, the compiler will not be able to detect the error and you will have to manually track down this run-time bug. Instead, using an enum with seven variables, the compiler will not let you assign a variable of this type any other value.

C# supports three different types of floating points, with different levels of precision. Precision is how many digits are valid in a number, both before and after a decimal point. Be sure that you are using the right floating point in your code for the level of precision you need.

  • float              7 digits of precision

  • double        15-16 digits of precision

  • decimal     28-29 digits of precision

Similar to C++, C# supports a BOOL value that can be either true or false. The final result of an expression is a bool, and you can assign an expression to a particular bool variable. These are mostly used in IF situations, for example, IF an algorithm is TRUE, perform action 1, if it is FALSE, perform action 2.

The null value means that there is no data present, much like a blank cell in Microsoft Excel. This can rain havoc on your application, as the code does not know what to do if its expecting an int value and receives a null instead. To support this, C# allows you to make many variable types nullable. All it takes is adding a ? after the type name, and a variable type can hold a null value. It looks like this:

int? ivar = null;

In C#, only 16 bit characters are used. There are many ways to generate char values, and can be useful if you are doing text processing. Typically, they won’t be used that often. Normally you will use text within text strings. As an example, you can see four different char types that all produce the letter W:

char char1 = ‘W’;

char char2 = ‘\x0057’;

char char3 = (char)87;

chat char4 = ‘\u0057’;

It’s not that difficult right? How about plugging these four statements into your editor, compile it and generate the letter W for each? Don’t forget that reading textbooks is well and good, but to really understand how it works, you need to practise each lesson.

Final thoughts

Computer programming is a skill everyone needs to know. We have covered many topics in this guide, but my final point I would like to make is this. Don’t leave it up to anyone else but yourself. Take charge of your career, your skills, and learn something today that will help you throughout the rest of your life. C# is a language that will be around for many years to come, and demand for employees with this skill is only going to increase.

What are you waiting for? Start learning today.