Professional C# 6 and .NET Core 1.0. Christian Nagel
Читать онлайн книгу.J. Schneider
INDEXER
John Sleeva
COVER DESIGNER
Wiley
COVER IMAGE
© Digital Storn/Shutterstock
Acknowledgments
I WANT TO THANK Charlotte Kughen, who made my text so much more readable. Often I was working late at night writing while .NET Core was continuously evolving. Charlotte was of enormous help to change my ideas into great readable text. I’m almost sure that Charlotte now knows a lot more about programming than she would like to. Special thanks also goes to István Novák, who has authored several great books. Despite all the issues we had with the fast-evolving .NET Core and the interim builds I was using while working on the book, István challenged me to enhance the code samples that allow you – the reader – to better follow the flow. Thank you, Charlotte and István – you’ve been of great help for the quality of this book.
I also would like to thank Kenyon Brown and Jim Minatel and everyone else at Wiley who helped to get edition 10 of this great book to be published. I also want to thank my wife and children for supporting my writing. You understood and helped with the time I was working on the book, including evenings, nights, and weekends. Angela, Stephanie, and Matthias – you are my loved ones. This would not have been possible without you.
Introduction
IF YOU WERE TO DESCRIBE THE C# LANGUAGE and .NET as the most significant technology for developers available, you would not be exaggerating.NET is designed to provide an environment within which you can develop almost any application to run on Windows. Runs on Windows– wait, I would have said that with previous versions of the .NET Framework. The new version,NET Core 1.0 not only runs on Windows, but it also runs on Linux and Mac systems. The C# programming language is designed specifically to work with .NET. By using C#, you can, for example, write a web page, a Windows Presentation Foundation (WPF) application, a REST web service, a component of a distributed application, a database access component, a classic Windows desktop application, or even a Universal Windows Platform (UWP) app that enables online and offline capabilities. This book covers .NET Core 1.0 and also the full .NET Framework stack,NET Framework 4.6. If you code using any of the prior versions, there may be sections of the book that will not work for you.
Where possible, samples of this book make use of .NET Core 1.0. The book code is built on a Windows system, but you can run the samples on other platforms as well; small changes might be needed to run them on Linux. Read Chapter 1, “.NET Application Architectures,” to see how to build the applications for the Linux platform. What’s not possible to run on Linux? WPF applications still need the full .NET Framework and run only on Windows. UWP apps are using .NET Core, but also require the Windows Runtime. These apps require Windows as well. These UI technologies are covered in Part III of the book, “Core Apps.”
So what’s the big deal about .NET and C#?
To understand the significance of .NET Core, you must consider the long-lived .NET Framework. The .NET Framework 1.0 was released in the year 2002. Since then about every two years a new major release has been made available. With Visual Studio 2013 we had C# 5 and .NET 4.5. The .NET Framework 4.5 is huge, with more than 20,000 classes.
NOTE Get into more details of the releases of the .NET Framework and C# in Chapter 1.
What are the problems with this huge framework? How is this solved with .NET Core?
For new developers, getting into this huge framework is not easy. Many things exist that are important for legacy applications, but they’re not really important for new applications. For experienced developers it’s not that easy to decide between these technologies to select the best one. You have to decide between ASP.NET Web Forms and ASP.NET MVC for web applications, decide between Windows Forms and WPF or the Universal Windows Platform for client applications, decide between the Entity Framework and LINQ to SQL for data access, decide between ArrayList and List<T> for storing collections… For some experienced developers the choice is obvious; for most it’s not that easy. It’s even more difficult for developers just starting with .NET.
.NET Core is based on smaller units, small NuGet packages. The Console class is only needed with console applications. With the .NET Framework, the Console class is available with mscorlib, an assembly that’s referenced by every .NET application. Using .NET Core, you have to explicitly decide to use the System.Console NuGet package. Otherwise, the Console class is not available.
Smaller packages also allow you to get rid of parts of the framework more easily. In case you need older collection classes for legacy applications, they are available with the NuGet package System.Collections.NonGeneric. With new applications you can define a list of packages that can be used, and System.Collections.NonGeneric can be excluded from this list.
Nowadays, development is going a lot faster. With many products, customers receive ongoing updates of products instead of receiving new versions every 2 years. Even Windows, with Windows 10, is on this fast pace. Customers receive smaller features with each update, but they receive them at a faster pace. Having 2-year release cycles with the .NET Framework nowadays is not fast enough. Some technologies, like the Entity Framework, already circumvented the problem by offering new features via NuGet packages that can be released independently of the .NET Framework.
Updating smaller pieces allows for faster innovation.NET Core, which is based on many small NuGet packages, can be changed more easily.NET Core and ASP.NET are now open source. You can find the source code for .NET Core at http://www.github.com/dotnet and for ASP.NET at http://www.github.com/aspnet.
When .NET was released, Windows had a big market share both on the client and on the server. Now the world is more fragmented. Companies decided against running server-side code with ASP.NET because it didn’t run on Linux. ASP.NET Core 1.0 with .NET Core can run on Linux.
.NET Core is platform-independent and supports Windows, Linux, and Mac systems. For client applications, you can use .NET with Xamarin on iPhone and Android.
The .NET Framework required having the same version of the .NET runtime that was used during development to be installed on the target system. Many application developments have been restricted by the version of the .NET Framework to use based on client needs. This is not only an issue for client-based application development but also for the server. I had to switch back to older .NET runtime versions because my provider didn’t support the newest one. With .NET Core, the runtime is delivered with the application.
When ASP.NET was built, compatibility with the predecessor technology Active Server Pages (ASP) that was built with JavaScript or VBScript code running on the server was an important aspect. Nowadays this is not needed anymore. ASP.NET Web Forms was built with the idea that the developer doesn’t need to know anything about JavaScript and HTML, and everything could be done with server-side code. Now, because of the huge number of JavaScript frameworks and enhancements in HTML, more control on JavaScript and HTML is needed.
With the new version of ASP.NET, performance has a big role in the framework architecture. You only have performance impacts for the things you really need. In case you don’t have static files with your web application, you have to explicitly decide on using it, otherwise you don’t pay a performance impact for this. With fine-grained control you can decide what features you need.
To get an even bigger performance improvement,NET Core can be built to native code. This is possible not only on Windows but also on Linux and Mac systems. With this you can get performance improvement especially on program startup, and you use less memory.
Now there’s an issue with legacy applications. Most applications can’t switch that easily to .NET Core. The full .NET Framework – running just on Windows – is evolving as well. It’s not evolving in such big steps as .NET Core, but it is a mature framework. At the time of this writing,NET