Chapter 1. Getting Started with C#

In this chapter, we will talk about the general state of the industry at the time when C# was first introduced, and some of the reasons that it is a great language. By the end of the chapter, you will have a fully working development environment that is ready to go through all of the examples in this book.

Origins

As every comic book super hero has an origin story, so does every professional in every walk of life. Sharing origin stories with your colleagues is great because it can serve as a point of reflection about how things were in the past, how they have evolved, and where they may go in the future. My own personal story originated in high school in the late nineties, watching my brother, who was five years my elder and in college, learning C++. With a few arcane instructions, complex programs came to life and were ready for action. I was fascinated.

This first glimpse of power was just the beginning. Around the same time, a friend of mine in class started working on a game, again written in C++, in the style of the NES game, The Legend of Zelda. Although I had briefly peeked at old QBasic games such as Gorillas in the past, I was amazed at the quality that he was able to achieve in his small demo. I decided to start learning how to program in earnest, and seeing as everyone I knew was using C++, that was the default choice for my first programming language.

The first program I ever wrote was a very simple financial budgeting program. Having just recently started working at my first job in high school, I was keenly aware of the new responsibilities involved in the management of money, and so I wanted to write a program to help me better manage my funds. First, it asked for the amount of my paycheck in dollars, and then for a list of bills that I had to pay.

After a few basic calculations, it gave me a report of how much disposable income would remain after my responsibilities were taken care of. As far as programs go, it was not the most complex piece of software, but it helped me learn the basics, such as loops, conditional statements, storage of an indeterminate list of items, and performing aggregate operations on an array.

It was a great personal triumph, but after some initial exploration with C++, I found myself hitting a bit of a brick wall. C++ was difficult to fully grasp as someone brand new to programming (and in high school). Not only did I have to learn about the basics of software, but I had to be constantly aware of the memory that I was using. Eventually, I discovered the tools of web development which were, to me at the time, much simpler to understand. I had moved from one end of the complexity spectrum to the other.

Much of the software landscape at that time was dominated by computer languages that sat in one of three camps: low level system languages such as C++, which offered the most in terms of performance and flexibility, but were also difficult and complex to master; interpreted languages such as JavaScript and VBScript, whose instructions were evaluated at runtime, were very easy to use and learn, but could not match the performance of low level languages; and finally a group of languages that come somewhere in the middle.

This middle of the road, which encompassed languages such as Java and Visual Basic, offered some of the best of both worlds, along with the worst of both worlds. In these languages you have a garbage collector , which means that when you create an object you do not have to explicitly release the used memory when you are done. They are also compiled to an intermediate language (for example, p-code for VB, and byte code for Java) which are then executed in a Virtual Machine running natively on the target platform. Because this intermediate language is similar to machine code, it is able to execute much faster than the purely interpreted languages. This performance, however, was still not really a match for a properly tuned C++ program, so Java and Visual Basic programs were often regarded as slow languages in comparison with C++.

Despite some of these drawbacks, the benefits of having a managed memory environment were evident to Microsoft. Because the programmer did not have to worry about complex concepts such as pointers and manual memory management, programs could be written faster and with fewer bugs. Rapid Application Development (RAD for short) seemed to be the future direction for Microsoft's platforms.

In the late nineties, they developed a version of the Java Virtual Machine, which by many accounts was faster than some of the other implementations available on the market. Unfortunately due to their inclusion of some proprietary extensions, and the fact that they did not completely implement the Java 1.1 standard, they ran into some legal troubles in 1997. This resulted ultimately in Microsoft discontinuing the development on their implementation of Java, and ultimately removing it from their platform in 2001.

Although it is impossible to know if what happened next was a direct result of the legal action against the Microsoft Java Virtual Machine, what we do know is that in 1999 Microsoft started working on a new programming language, which was named Cool (C-like Object Oriented Language).

C# is born

And then it happened; in 2000, Microsoft announced that they were working on a new programming language. The language which was originally called Cool, was unveiled at the Professional Developers Conference 2000 in Orlando, FL as C#. Some of the highlights of this new language are:

  • It is based on the syntax of the C family of programming languages, so the syntax was very familiar for anyone who had experience with C++, Java, or JavaScript.
  • Memory management of C# is similar to that of Java and Visual Basic, with a very powerful garbage collector. This meant that the users could focus on the content of their application, rather than worrying about boilerplate memory management code.
  • The C# compiler along with a static type system means that certain classes of bugs can be caught at compile time, rather than having to deal with them at runtime as you would in JavaScript. This is a Just-In-Time compiler, which means that the code would be compiled to a native executable at runtime, and optimized for the operating system that is executing the code. Performance is an important goal of the new platform.
  • This language has a strong and extensive base class library, which means that many pieces of functionality would be built right into the framework. Aside from some industry standard libraries such as Boost, there were not very many common C/C++ libraries, which resulted in people often rewriting common functionality. Java, on the other hand, had a great many libraries, but they were written by a diverse set of developers, which meant that consistency in functionality and style was a problem.
  • It also has interoperability with other languages that worked on the Common Language Runtime (CLR). So a single program could use functionality written in different languages, thus using each language for what it was best at.
  • Microsoft submitted the specification to the ISO working group. This opened the door to a vibrant open source community around the framework, because it meant that there would always be a standard to work against. A popular open source implementation of the .NET Framework and C# called Mono lets you run your code on different platforms.

Although none of the elements described in this list were particularly new, C# aimed to take the best aspects of programming languages that came before, and incorporate them, namely the strength and power of C++, the simplicity of JavaScript, and the ease of hosting of VBScript/ASP, among other things.

People coming from ANY language (C, C++, or Java) could be productive in C# with little effort. C# found the sweet spot where productivity, features, and the learning curve all intersected.

Over the next decade, the language would go on to evolve a very attractive set of features that make it easier and faster to write great programs. Now in its fifth iteration, the C# language has become more expressive and powerful with features, such as Language Integrated Queries (LINQ), Task Parallel Library (TPL), a Dynamic Language Runtime (DLR), and asynchronous programming features. What's more, with the Mono framework, you can not only target Windows, but also every other mainstream platform such as Linux, Mac OS, Android, iOS, and even game consoles such as the Playstation Vita.

Whether you have been writing C# for the last decade, or are just picking it up now, this book will take you through all of the features of the latest version 5.0. We will also explore the evolution and history of C# so that you can understand why certain features developed the way they did, and how you can use them to their full potential.

Before we begin though, we need to configure your computer to be able to compile all of the samples. This chapter will guide you through installing everything you need to go through every example in this book.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.57.172