Chapter 4. Using .NET Standard Types

This chapter is about .NET Standard 1.6 types that are included with .NET Core 1.0 and 1.1. This includes common types for manipulating text, collections, and implementing internationalization. You will learn how the .NET types are related to C#.

You will learn about .NET Core and its class library assemblies and packages of types that are defined in .NET Standard that allow your applications to connect existing components together to perform common practical tasks.

This chapter covers the following topics:

  • Using assemblies and namespaces
  • Storing and manipulating text
  • Storing data with collections
  • Internationalizing an application

Using assemblies and namespaces

.NET Core is made up of several pieces, which are as follows:

  • Language compilers: These turn your source code (written with languages such as C#, F#, Visual Basic, and others) into intermediate language (IL) code stored in assemblies (applications and class libraries). C# 6 introduced a completely rewritten compiler known as Roslyn.
  • Common Language Runtime (CoreCLR): The runtime loads assemblies, compiles the IL code stored in them into native code instructions for your computer's CPU, and executes the code within an environment that manages resources such as threads and memory.
  • Base Class Libraries and NuGet packages (CoreFX): These are prebuilt assemblies of types for performing common tasks when building applications. You can use them to quickly build anything you want, rather like combining LEGO™ pieces. .NET Core 1.0 and 1.1 are based on .NET Standard 1.6, which is a superset of all previous versions of .NET Standard.

Comparing .NET Framework with .NET Core

The .NET Framework is a superset of .NET Core.

Although .NET Core has less functionality today, once .NET Core 2.0 is released in late summer 2017, with support for .NET Standard 2.0, .NET Core will have comparable functionality to .NET Framework. Going forward, Microsoft has said that new features will be added to .NET Core first, and then ported back to .NET Framework.

Base Class Libraries and CoreFX

The .NET Framework's BCL and the .NET Core's CoreFX are libraries of prebuilt code that are divided into assemblies and namespaces that make it easier to manage the tens of thousands of types available. It is important to understand the difference between an assembly and a namespace.

Assemblies, NuGet packages, and platforms

An assembly is where a type is stored in the filesystem. Assemblies are a mechanism for deploying code. For example, the System.Data.dll assembly contains types for managing data. To use types in other assemblies, they must be referenced.

Assemblies are often distributed as NuGet packages, which can contain multiple assemblies and other resources. You will also hear talk about platforms, which are combinations of NuGet packages.

To search for useful NuGet packages, follow:

https://www.nuget.org/packages

When using .NET Core, you reference the dependency assemblies, NuGet packages, and platforms that your application needs in a project file.

The original project file for .NET Core was a JSON format file named project.json. The "newer" format is an XML file with the extension .csproj. I say "newer" because it is actually the old format that has been used since the beginning of .NET. Microsoft changed their mind after the release of .NET Core 1.0!

Namespaces

A namespace is the address of a type. Namespaces are a mechanism to uniquely identify a type by requiring a full address rather than just a short name.

In the real world, Bob of 34 Sycamore Street is different from Bob of 12 Willow Drive.

In .NET Core, the IActionFilter interface of the System.Web.Mvc namespace is different from the IActionFilter interface of the System.Web.Http.Filters namespace.

Referencing a dependent assembly

If an assembly is compiled as a class library (it provides types for other assemblies to use), then it has the file extension .dll (dynamic link library) and it cannot be executed standalone, except by the dotnet run command.

If an assembly is compiled as an application, then it has the file extension .exe (executable) and can be executed standalone.

Any assembly (both applications and class libraries) can reference one or more class library assemblies as dependencies, but you cannot have circular references, so assembly B cannot reference assembly A if assembly A already references assembly B. Visual Studio will warn you if you attempt to add a dependency reference that would cause a circular reference.

Every application created for .NET Core has a dependency reference to the Microsoft .NET Core App platform. This special platform contains thousands of types in NuGet packages that almost all applications would need, such as the int and string variables.

Browsing assemblies with Visual Studio 2017

Using Visual Studio 2017, if you open one of your previous projects, and navigate to View | Object Browser, or press Ctrl + W, J, then you will see that your solution has dependencies on assemblies such as System.Collections that you will use later in this chapter, and on System.Console, used in all the coding exercises so far, as shown in the following screenshot:

Browsing assemblies with Visual Studio 2017

Object Browser can be used to learn about the assemblies and namespaces that .NET Core uses to logically and physically group types together.

Note

Unfortunately, Visual Studio Code does not yet have an equivalent feature.

When you click on an assembly in Object Browser, you can see the version of .NET Standard that the assembly was first released with. For example, System.Console.dll was introduced in .NET Standard 1.3, as shown in the following screenshot:

Browsing assemblies with Visual Studio 2017

In the following screenshot, you can see that System.IO.dll was released as part of .NET Standard 1.5:

Browsing assemblies with Visual Studio 2017

Later in this chapter, you will learn about generic collections, like List<T>, that are part of the System.Collections.dll assembly, as shown in the following screenshot:

Browsing assemblies with Visual Studio 2017

Understanding .NET Core project files

To understand how Visual Studio 2017 and Visual Studio Code store their dependencies, right-click any project in Solution Explorer and choose Unload Project.

The project will now be marked as (unavailable). Right-click the project and choose Edit <projectname>.csproj:

Understanding .NET Core project files

Note

If you are using Visual Studio Code, simply open the .csproj file.

This action will open the project file and reveal the XML inside, as shown in the following markup:

    <Project Sdk="Microsoft.NET.Sdk"> 
 
      <PropertyGroup> 
        <OutputType>Exe</OutputType> 
        <TargetFramework>netcoreapp1.1</TargetFramework> 
      </PropertyGroup> 
 
    </Project> 

Note

Note the <TargetFramework> of netcoreapp1.1

Close the file.

In Visual Studio 2017, right-click the project, and choose Reload Project.

Note

Visual Studio 2017 should allow you to view and modify the .csproj file without unloading the project first, but I have found that it works more reliably if you explicitly unload and reload.

Relating assemblies and namespaces

In Visual Studio 2017, press Ctrl + Shift + N or navigate to File | New | Project....

In the New Project dialog, in the Installed | Templates list, select Visual C#. In the list at the center, select Console App (.NET Core), type the name Ch04_Assemblies, change the location to C:Code, type the solution name Chapter04, and then click on OK.

In Visual Studio Code, use the Integrated Terminal to create a folder named Chapter04 with a subfolder named Ch04_Assemblies. Use dotnet new console to create a console application and restore packages.

Inside the Main method, type the following code:

    var doc = new XDocument(); 

The XDocument type is not recognized because we have not told the compiler what the namespace of the type is. Although this project already has a reference to the assembly that contains the type, we also need to either prefix the type name with its namespace or to import the namespace. We can get Visual Studio to fix this problem for us.

Importing a namespace

Click inside the XDocument class name. Visual Studio 2017 and Visual Studio Code both display a light bulb showing that it recognizes the type and can automatically fix the problem for you.

Click on the light bulb, or in Visual Studio 2017 press Ctrl + . (dot), or in Visual Studio Code press Cmd + . (dot).

Visual Studio 2017 shows a nicer explanation of your choices, and a preview of its suggested changes, as shown in the following screenshot:

Importing a namespace

Visual Studio Code has no explanations, but it does have the same choices, as shown in the following screenshot:

Importing a namespace

Choose using System.Xml.Linq; from the menu. This will import the namespace by adding a using statement to the top of the file.

Once a namespace is imported at the top of a code file, then all the types within the namespace are available for use in that code file by just typing their name.

Relating C# keywords to .NET types

One of the common questions I get from new C# programmers is, "What is the difference between string with a lowercase and String with an uppercase?"

The short answer is easy: none.

The long answer is that all C# type keywords are aliases for a .NET type in a class library assembly.

When you use the keyword string, the compiler turns it into a System.String type. When you use the type int, the compiler turns it into a System.Int32 type. You can even see this if you hover your mouse over an int type, as follows:

Relating C# keywords to .NET types

Tip

Good Practice

Use the C# keyword instead of the actual type because the keywords do not need the namespace imported.

The following table shows the 16 C# type keywords and their actual .NET types:

Keyword

.NET type

Keyword

.NET type

string

System.String

char

System.Char

sbyte

System.SByte

byte

System.Byte

short

System.Int16

ushort

System.UInt16

int

System.Int32

uint

System.UInt32

long

System.Int64

ulong

System.UInt64

float

System.Single

double

System.Double

decimal

System.Decimal

bool

System.Boolean

object

System.Object

dynamic

System.Dynamic.DynamicObject

Note

Other .NET programming language compilers can do the same thing. For example, the Visual Basic .NET language has a type named Integer that is its alias for System.Int32.

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

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