Automatic Memory Management
One of the biggest advantages of the .NET Framework is the built-in automatic memory management. It protects the programmers from the complex task of manually allocating memory for objects and then waiting for a suitable moment to release it. This significantly increases the developer productivity and the quality of the programs written in C#.
In the .NET Framework, there is a special component of the CLR that looks after memory management. It is called a "garbage collector" (automated memory cleaning system). The garbage collector has the following main tasks: to check when the allocated memory for variables is no longer in use, to release it and make it available for allocation of new objects.
It is important to note that it is not exactly clear at what moment the memory gets cleaned of unused objects (local variables for example). According to the C# language specifications, it happens at some moment after a given variable gets out of scope but it is not specified, whether this happens instantly, after some time or when the available memory becomes insufficient for the normal program operation.
Independence from the Environment and the Programming Language
One of the advantages of .NET is that programmers using different .NET languages can easily exchange their code. For example a C# programmer can use the code written by another programmer in VB.NET, Managed C++ or F#. This is possible because the programs written in different .NET
Chapter 1. Introduction to Programming 81
languages share a common system of data types, execution infrastructure and a unified format of the compiled code (assemblies).
A big advantage of the .NET technology is the ability to run code, which is written and compiled only once, on different operating systems and hardware devices. We can compile a C# program in a Windows environment and then execute it under Windows, Windows Mobile, Windows RT or Linux. Officially Microsoft only supports the .NET Framework on Windows, Windows Mobile and Windows Phone, but there are third party vendors that offer .NET implementation on other operating systems.
Mono (.NET for Linux)
One example of .NET implementation for non-Windows environment is the open-source project Mono (www.mono-project.com). It implements the .NET Framework and most of its accompanying libraries for Linux, FreeBSD, iPhone and Android. Mono is unofficial .NET implementation and some features may work not exactly as expected. It does implement well the core .NET standards (such as C# compiler and CLR) but does not support fully the latest .NET technologies and framework like WPF and ASP.NET MVC.
Microsoft Intermediate Language (MSIL)
The idea for independence from the environment has been set in the earliest stages of creation of the .NET platform and is implemented with the help of a little trick. The output code is not compiled to instructions for a specific microprocessor and does not use the features of a specific operating system; it is compiled to the so called Microsoft Intermediate Language (MSIL). This MSIL is not directly executed by the microprocessor but from a virtual environment called Common Language Runtime (CLR).
Common Language Runtime (CLR) – the Heart of .NET
In the very center of the .NET platform beats its heart – the Common Language Runtime (CLR) – the environment that controls the execution of the managed code (MSIL code). It ensures the execution of .NET programs on different hardware platforms and operating systems.
CLR is an abstract computing machine (virtual machine). Similarly to physical computers, it supports a set of instructions, registries, memory access and input-output operations. CLR ensures a controlled execution of the .NET programs using the full capabilities of the processor and the operating system. CLR also carries out the managed access to the memory and the other resources of the computer, while adhering to the access rules set when the program is executed.
82 Fundamentals of Computer Programming with C#
The .NET Platform
The .NET platform contains the C# language, CLR and many auxiliary instruments and libraries ready for use. There are a few versions of .NET according to the targeted user group:
- .NET Framework is the most common version of the .NET environment because of its general purpose. It is used in the development of console applications, Windows applications with a graphical user interface, web applications and many more.
- .NET Compact Framework (CF) is a "light" version of the standard .NET Framework and is used in the development of applications for mobile phones and other PDA devices using Windows Mobile Edition.
- Silverlight is also a "light" version of the .NET Framework, intended to be executed on web browsers in order to implement multimedia and Rich Internet Applications.
- .NET for Windows Store apps is a subset of .NET Framework designed for development and execution of .NET applications in Windows 8 and Windows RT environment (the so called Windows Store Apps).
.NET Framework
The standard version of the .NET platform is intended for development and use of console applications, desktop applications, Web applications, Web services, Rich Internet Applications, mobile applications for tablets and smart phones and many more. Almost all .NET developers use the standard version.
.NET Technologies
Although the .NET platform is big and comprehensive, it does not provide all the tools required to solve every problem in software development. There are many independent software developers, who expand and add to the standard functionality offered by the .NET Framework. For example, companies like the Bulgarian software corporation Telerik develop subsidiary sets of components. These components are used to create graphical user interfaces, Web content management systems, to prepare reports and they make application development easier.
The .NET Framework extensions are software components, which can be reused when developing .NET programs. Reusing code significantly facilitates and simplifies software development, because it provides solutions for common problems, offers implementations of complex algorithms and technology standards. The contemporary programmer uses libraries and components every day, and saves a lot of effort by doing so.
Let’s look at the following example – software that visualizes data in the form of charts and diagrams. We can use a library, written in .NET, which draws the charts. All that we need to do is input the correct data and the
Chapter 1. Introduction to Programming 83
library will draw the charts for us. It is very convenient and efficient. Also it leads to reduction in the production costs because the programmers will not need to spend time working on additional functionality (in our case drawing the charts, which involves complex mathematical calculations and controlling the graphics card). The application itself will be of higher quality because the extension it uses is developed and supported by specialists with more experience in that specific field.
Software technologies are sets of classes, modules, libraries, programming models, tools, patterns and best practices addressing some specific problem in software development. There are general software technologies, such as Web technologies, mobile technologies, technologies for computer graphics and technologies related to some platform such as .NET or Java.
There are many .NET technologies serving for different areas of .NET development. Typical examples are the Web technologies (like ASP.NET and ASP.NET MVC), allowing fast and easy creation of dynamic Web applications and .NET mobile technologies (like WinJS), which make possible the creation of rich user interface multimedia applications working on the Internet.
.NET Framework by default includes as part of itself many technologies and class libraries with standard functionality, which developers can use. For example, there are ready-to-use classes in the system library working with mathematical functions, calculating logarithms and trigonometric functions (System.Math class). Another example is the library dealing with networks (System.Net), it has a built-in functionality to send e-mails (using the System.Net.Mail.MailMessage class) and to download files from the Internet (using System.Net.WebClient).
A .NET technology is the collection of .NET classes, libraries, tools, standards and other programming means and established development models, which determine the technological framework for creating a certain type of application. A .NET library is a collection of .NET classes, which offer certain ready-to-use functionality. For example, ADO.NET is a technology offering standardized approach to accessing relational databases (like Microsoft SQL Server and MySQL). The classes in the package (namespace) System.Data.SqlClient are an example of .NET library, which provide functionality to connect an SQL Server through the ADO.NET technology.
Some of the technologies developed by software developers outside of Microsoft become wide-spread and as a result establish themselves as technology standards. Some of them are noticed by Microsoft and later are added to the next iteration of the .NET Framework. That way, the .NET platform is constantly evolving and expanding with new libraries and technologies. For instance, the object-relational mapping technologies initially were developed as independent projects and products (like the open code project NHibernate and Telerik’s OpenAccess ORM). After they gained enormous popularity, their inclusion in the .NET Framework became a necessity. And this is how the LINQ-to-SQL and ADO.NET Entity Framework technologies were born, respectively in .NET 3.5 and .NET 4.0.
84 Fundamentals of Computer Programming with C#
Application Programming Interface (API)
Each .NET library or technology is utilized by creating objects and calling their methods. The set of public classes and methods in the programming libraries is called Application Programming Interface or just API. As an example we can look at the .NET API itself; it is a set of .NET class libraries, expanding the capabilities of the language and adding high-level functionality. All .NET technologies offer a public API. The technologies are often referred to simply as API, which adds certain functionality. For example: API for working with files, API for working with charts, API for working with printers, API for reading and creating Word and Excel documents, API for creating PDF documents, Web development API, etc.
.NET Documentation
Very often it is necessary to document an API, because it contains many namespaces and classes. Classes contain methods and parameters. Their purpose is not always obvious and needs to be explained. There are also inner dependencies between the separate classes, which need to be explained in order to be used correctly. These explanations and technical instructions on how to use a given technology, library or API, are called documentation. The documentation consists of a collection of documents with technical content.
The .NET Framework also has a documentation officially developed and supported by Microsoft. It is publicly available on the Internet and is also distributed with the .NET platform as a collection of documents and tools for browsing and searching.
Chapter 1. Introduction to Programming 85
The MSDN Library is Microsoft’s official documentation for all their products for developers and software technologies. The .NET Framework’s technical documentation is part of the MSDN Library and can be found here: http://msdn.microsoft.com/en-us/library/vstudio/gg145045.aspx. The above screenshot shows how it might look like (for .NET version 4.5).
What We Need to Program in C#?
After we made ourselves familiar with the .NET platform, .NET libraries and .NET technologies, we can move on to writing, compiling and executing C# programs.
In order to program in C#, we need two basic things – an installed .NET Framework and a text editor. We need the text editor to write and edit the C# code and the .NET Framework to compile and execute it.
.NET Framework
By default, the .NET Framework is installed along with Windows, but in old Windows versions it could be missing. To install the .NET Framework, we must download it from Microsoft’s website (http://download.microsoft.com). It is best if we download and install the latest version.
Do not forget that we need to install the .NET Framework before we begin! Otherwise, we will not be able to compile and execute the program.
If we run Windows 8 or Windows 7, the .NET Framework will be already installed as part of Windows.
Text Editor
The text editor is used to write the source code of the program and to save it in a file. After that, the code is compiled and executed. There are many text editing programs. We can use Windows’ built-in Notepad (it is very basic and inconvenient) or a better free text editor like Notepad++ (notepad-plus.sourceforge.net) or PSPad (www.pspad.com).
Compilation and Execution of C# Programs
The time has come to compile and execute the simple example program written in C# we already discussed. To accomplish that, we need to do the following:
- Create a file named HelloCSharp.cs;
- Write the sample program in the file;
- Compile HelloCSharp.cs to an executable file HelloCSharp.exe using the console-based C# compiler (csc.exe);
- Execute the HelloCSharp.exe file.
86 Fundamentals of Computer Programming with C#
Now, let’s do it on the computer!
The instructions above vary depending on the operating system. Since programming on Linux is not the focus of this book, we will take a thorough look at what we need to write and execute the sample program on Windows. For those of you, who want to program in C# in a Linux environment, we already explained the Mono project, and you can download it and experiment.
Here is the code of our first C# program: HelloCSharp.cs class HelloCSharp { static void Main() { System.Console.WriteLine("Hello C#!"); } }
0 Comments