Professional C# 6 and .NET Core 1.0. Christian Nagel
Чтение книги онлайн.
Читать онлайн книгу Professional C# 6 and .NET Core 1.0 - Christian Nagel страница 11
NOTE When you use third-party packages from the NuGet server, you’re always at risk if a package is available at a later time. You also need to check about the support availability of the package. Always check for project links with information about the package before using it. With the package source, you can select Microsoft and .NET to only get packages supported by Microsoft. Third-party packages are also included in the Microsoft and .NET section, but they are third-party packages that are supported by Microsoft.
You can also use your own NuGet server with your development team. You can define to only allow packages from your own server to be used by the development team.
Because .NET Core is so modular, all applications – other than the simplest ones – need additional NuGet packages. To make it easier for you to find the package, with every sample application that’s built with .NET Core this book shows a table that lists packages and namespaces that need to be added.
NOTE More information about the NuGet Package Manager is covered in Chapter 17, “Visual Studio 2015.”
Common Language Runtime
The Universal Windows Platform makes use of Native .NET to compile IL to native code. With all other scenarios, with both applications using the .NET Framework 4.6 and applications using .NET Core 1.0, a Common Language Runtime (CLR) is needed. However,NET Core uses the CoreCLR whereas the .NET Framework uses the CLR. So, what’s done by a CLR?
Before an application can be executed by the CLR, any source code that you develop (in C# or some other language) needs to be compiled. Compilation occurs in two steps in .NET:
1. Compilation of source code to Microsoft Intermediate Language (IL)
2. Compilation of IL to platform-specific native code by the CLR
The IL code is available within a .NET assembly. During runtime, a Just-In-Time (JIT) compiler compiles IL code and creates the platform-specific native code.
The new CLR and the CoreCLR include a new JIT compiler named RyuJIT. The new JIT compiler is not only faster than the previous one; it also has better support for the Edit & Continue feature while debugging with Visual Studio. The Edit & Continue feature enables you to edit the code while debugging, and you can continue the debug session without the need to stop and restart the process.
The runtime also includes a type system with a type loader that is responsible for loading types from assemblies. Security infrastructure with the type system verifies whether certain type system structures are permitted – for example, with inheritance.
After creating instances of types, the instances also need to be destroyed and memory needs to be recycled. Another feature of the runtime is the garbage collector. The garbage collector cleans up memory from the managed heap that isn’t referenced anymore. Chapter 5, “Managed and Unmanaged Resources,” explains how this is done and when it happens.
The runtime is also responsible for threading. Creating a managed thread from C# is not necessarily a thread from the underlying operating system. Threads are virtualized and managed by the runtime.
NOTE How threads can be created and managed from C# is covered in Chapter 21, “Tasks and Parallel Programming,” and in Chapter 22, “Task Synchronization.”
.NET Native
A new feature of .NET 2015 is to compile a managed program to native code, .NET Native. With Windows apps this generates optimized code that can have a startup time that’s up to 60 percent faster and uses 15 to 20 percent less memory.
.NET Native started with compiling UWP apps to native code for apps deployed to the Windows Store.NET Native will also be available with other .NET Core applications. However, this feature did not make it into version 1 of .NET Core and will be available with a future version. You can compile .NET Core applications running on both Windows and Linux to native code. Of course, you need different native images on each of these platforms. Behind the scenes,NET Native shares the C++ optimizer for generating the native code.
Windows Runtime
Starting with Windows 8, the Windows operating system offers another framework: the Windows Runtime. This runtime is used by the Windows Universal Platform and was version 1 with Windows 8, version 2 with Windows 8.1, and version 3 with Windows 10.
Unlike the .NET Framework, this framework was created using native code. When it’s used with .NET applications, the types and methods contained just look like .NET. With the help of language projection, the Windows Runtime can be used with the JavaScript, C++, and .NET languages, and it looks like it’s native to the programming environment. Methods are not only behaving differently in regard to case sensitivity; the methods and types can also have different names depending on where they are used.
The Windows Runtime offers an object hierarchy organized in namespaces that start with Windows. Looking at these classes, there’s not a lot with duplicate functionality to the .NET Framework; instead, extra functionality is offered that is available for apps running on the Universal Windows Platform.
Let’s get into coding and create a Hello, World application. Since the 1970s, when Brian Kernighan and Dennis Ritchie wrote the book The C Programming Language, it’s been a tradition to start learning programming languages using a Hello, World application. Interestingly, the syntax for Hello, World changed with C# 6; it’s the first time this simple program has looked different since the invention of C#.
The first samples will be created without the help of Visual Studio so you can see what happens behind the scenes by creating the application with command-line tools and a simple text editor (such as Notepad). Later, you’ll switch to using Visual Studio because it makes programming life easier.
Type the following source code into a text editor, and save it with a .cs extension (for example, HelloWorld.cs). The Main method is the entry point for a .NET application. The CLR invokes a static Main method on startup. The Main method needs to be put into a class. Here, the class is named Program, but you could call it by any name. WriteLine is a static method of the Console class. All the static members of the Console class are opened with the using declaration in the first line. using static System.Console opens the static members of the Console class with the result that you don’t need to type the class name calling the method WriteLine (code file Dotnet/HelloWorld.cs):
As previously mentioned, the syntax of Hello, World changed slightly with C# 6. Previous to C# 6, using static was not available, and only a namespace could be opened with the using declaration. Of course, the following code still works with C# 6 (code file Dotnet/HelloWorld2.cs):
The using declaration is there to reduce the code with opening a namespace. Another way to write the Hello, World program