It takes a lot of work to get content into a presentable format for students and IT professionals to read and get value from. The authors indeed have technical knowledge and experiences to share, but without the technical writers, technical reviewers, developers, editors, publishers, graphic designers, the list goes on, providing their valuable input, a book of high quality could not be written. The rate of change occurs too quickly for an individual to perform all these tasks and still publish a book that is valid before the technology becomes stale. This is why authors worked together with a great team to get all the components of the book together quickly. It was done to ensure that the most up to date information gets to the reader while the features are still fresh and current. I would like to thank Tom Dinse for his great project management and technical review of the content as well as John Mueller for his technical review and suggestions throughout the process. Lastly, I would like to thank all the numerous people behind the scenes who helped get this book together.
WHAT YOU WILL LEARN IN THIS CHAPTER
What the .NET Framework is
What C# is
Explore Visual Studio 2017
Welcome to the first chapter of the first section of this book. This section provides you with the basic knowledge you need to get up and running with the most recent version of C#. Specifically, this chapter provides an overview of the .NET Framework and C#, including what these technologies are, the motivation for using them, and how they relate to each other.
It begins with a general discussion of the .NET Framework. This technology contains many concepts that are tricky to come to grips with initially. This means that the discussion, by necessity, covers many concepts in a short amount of space. However, a quick look at the basics is essential to understanding how to program in C#. Later in the book, you revisit many of the topics covered here, exploring them in more detail.
After that general introduction, the chapter provides a basic description of C# itself, including its origins and similarities to C++. Finally, you look at the primary tool used throughout this book: Visual Studio (VS). Visual Studio 2017 is the latest in a long line of development environments that Microsoft has produced, and it includes all sorts of features (including full support for Windows Store, Azure, and cross‐platform applications) that you will learn about throughout this book.
WHAT IS THE .NET FRAMEWORK?
The .NET Framework (now at version 4.7) is a revolutionary platform created by Microsoft for developing applications. The most interesting thing about this statement is how vague and limited it is—but there are good reasons for this. To begin with, note that it doesn’t actually “develop applications only on the Windows operating system.” Although the Microsoft release of the .NET Framework runs on the Windows and Windows Mobile operating systems, it is possible to find alternative versions that will work on other systems. One example of this is Mono, an open source version of the .NET Framework (including a C# compiler) that runs on several operating systems, including various ‐flavors of Linux and Mac OS; you can read more about it at http://www.mono‐project.com.
Mono is a very important part of the .NET ecosystem, especially for creating client‐side applications with Xamarin. Microsoft has also created a cross platform open source library called .NET Core (https://github.com/dotnet/core) which they hope will have a positive impact on both the Mono and .NET Core frameworks. Programmers in both ecosystems can use examples from each other’s libraries to improve performance, security, and the breadth of language feature offerings—collaboration is a key characteristic in the open source community.
In addition, the definition of the .NET Framework includes no restriction on the type of applications that are possible. The .NET Framework enables the creation of desktop applications, Windows Store (UWP) applications, cloud/web applications, Web APIs, and pretty much anything else you can think of. Also, it’s worth noting that web, cloud, and Web API applications are, by definition, multi‐platform applications, since any system with a web browser can access them.
The .NET Framework has been designed so that it can be used from any language, including C# (the subject of this book) as well as C++, F#, JScript, Visual Basic, and even older languages such as COBOL. For this to work, .NET‐specific versions of these languages have also appeared, and more are being released all the time. For a list of languages, see https://msdn.microsoft.com/en‐us/library/ee822860(v=vs.100).aspx. Not only do these languages have access to the .NET Framework, but they can also communicate with each other. It is possible for C# developers to make use of code written by Visual Basic programmers, and vice versa.
All of this provides an extremely high level of versatility and is part of what makes using the .NET Framework such an attractive prospect.
What’s in the .NET Framework?
The .NET Framework consists primarily of a gigantic library of code that you use from your client‐ or server‐side languages (such as C#) using object‐oriented programming (OOP) techniques. This library is categorized into different modules—you use portions of it depending on the results you want to achieve. For example, one module contains the building blocks for Windows applications, another for network programming, and another for web development. Some modules are divided into more specific submodules, such as a module for building web services within the module for web development.
The intention is for different operating systems to support some or all of these modules, depending on their characteristics. A smartphone, for example, includes support for all the base .NET functionality, but is unlikely to require some of the more esoteric modules.
Part of the .NET Framework library defines some basic types. A type is a representation of data, and specifying some of the most fundamental of these (such as “a 32‐bit signed integer”) facilitates interoperability between languages using the .NET Framework. This is called the Common Type System (CTS).
As well as supplying this library, the .NET Framework also includes the .NET Common Language Runtime (CLR), which is responsible for the execution of all applications developed using the.NET library.
.NET Standard and .NET Core
When the .NET Framework was originally created, although it was designed for running on multiple platforms, there was no industry accepted open‐source forking concept. These days, usually on GitHub, a project can be forked and then customized to run on multiple platforms. For example, the .NET Compact Framework and the .NET Micro Framework are forks of the .NET Framework, like .NET Core, which was created as the most optimal solution for cross‐platform code development. Each of those .NET Framework “flavors” or “verticals” had a specific set of requirements and objectives that triggered the need to make that fork.
Included in the .NET Framework is a set of Base Class Libraries (BCL) that contain APIs for basic actions most developers need a program to do. These actions include, for example, file access, string manipulation, managing streams, storing data in collections, security attributes, and many others. These fundamental capabilities are often implemented differently within each of the .NET Framework flavors. This requires a developer to learn, develop, and manage multiple BCLs for each fork or flavor of their application based on the platform it runs. This is the problem that the .NET Standard has solved.
The expectation is that shortly, this forking concept will result in many more flavors of the .NET Framework. This increase will necessitate a standard set of basic programming APIs that works with each fork and flavor. Without this cross platform base library, the development and support complexities would prevent the speedy adoption of the forked version. In short, .NET Standard is a class library that exposes APIs that support any fork or flavor of application using the .NET Platform.
Writing Applications Using the .NET Framework and .NET Core
Writing an application using either the .NET Framework or .NET Core means writing code (using any of the languages that support the Framework) using the .NET code library. In this book you use Visual Studio for your development. Visual Studio is a powerful, integrated development environment that supports C# (as well as managed and unmanaged C++, Visual Basic, and some others). The advantage of this environment is the ease with which .NET features can be integrated into your code. The code that you create will be entirely C# but use the .NET Framework throughout, and you’ll make use of the additional tools in Visual Studio where necessary.
In order for C# code to execute, it must be converted into a language that the target operating ‐system understands, known as native code. This conversion is called compiling code, an act that is performed by a compiler. Under the .NET Framework and .NET Core, this is a two‐stage process.
CIL and JIT
When you compile code that uses either the .NET Framework or .NET Core library, you don’t immediately create operating system–specific native code. Instead, you compile your code into Common Intermediate Language (CIL) code. This code isn’t specific to any operating system (OS) and isn’t specific to C#. Other .NET languages—Visual Basic .NET or F#, for example—also compile to this language as a first stage. This compilation step is carried out by Visual Studio when you develop C# applications.
Obviously, more work is necessary to execute an application. That is the job of a just‐in‐time (JIT) compiler, which compiles CIL into native code that is specific to the OS and machine architecture being targeted. Only at this point can the OS execute the application. The just‐in‐time part of the name reflects the fact that CIL code is compiled only when it is needed. This compilation can happen on the fly while your application is running, although luckily this isn’t something that you normally need to worry about as a developer. Unless you are writing extremely advanced code where performance is critical, it’s enough to know that this compilation process will churn along merrily in the background, without interfering.
In the past, it was often necessary to compile your code into several applications, each of which targeted a specific operating system and CPU architecture. Typically, this was a form of optimization (to get code to run faster on an AMD chipset, for example), but at times it was critical (for applications to work in both Win9x and WinNT/2000 environments, for example). This is now unnecessary because JIT compilers (as their name suggests) use CIL code, which is independent of the machine, operating system, and CPU. Several JIT compilers exist, each targeting a different architecture, and the CLR/CoreCLR uses the appropriate one to create the native code required.
The beauty of all this is that it requires a lot less work on your part—in fact, you can forget about system‐dependent details and concentrate on the more interesting functionality of your code.
使用超级多功能的C＃7和Visual Studio 2017轻松开始编程
从C＃7开始使用Visual Studio 2017进行编程是初学者对世界上最流行的编程语言的最终指南。无论您是刚开始编程的新手，还是刚接触C＃的新手，都是开始时的最佳时机。新的C＃7和Visual Studio 2017更新功能提供了许多新工具和功能，可简化工作流程，简化代码，并使构建高质量应用程序变得前所未有的简单。本书将向您介绍您需要了解的所有内容，从非常基础开始，让您立即编程。您将学习变量，流控制和面向对象编程，然后进入Web和Windows编程以及数据库和XML。配套网站提供可下载的代码示例，实用的试用版部分提供了明确的，
使用C＃7和Visual Studio 2017中的最新功能构建更智能，更快速的应用程序
编程世界对于初学者来说似乎是令人生畏的，学习一门全新“语言”的前景似乎令人生畏。从C＃7开始使用Visual Studio 2017编程揭开过程的神秘面纱，并向您展示如何将您的想法变为现实