A selection represents a set of objects utilized for the storage and retrieval of facts. Collections help you to allocate memory dynamically to store aspects and then retrieve them using a crucial or index as required.

You can have common or generic collections. Whilst common collections really do not give style-security, generic collections are style-risk-free. The common collections are component of the System.Collections namespace, and the generic collections are component of the System.Collections.Generic namespace.

An immutable object is defined as an object that can not be altered after it has been established. Not all collections are immutable, but you can use the browse-only selection styles in .Internet Main these kinds of as IReadOnlyList, IReadOnlyDictionary, and IReadOnlyCollection to implement immutable styles. These all are component of the System.Collections.Generic namespace.

This write-up discusses these browse-only immutable selection styles in .Internet Main and how you can work with them in C#. To work with the code illustrations supplied in this write-up, you should have Visual Studio 2019 mounted in your program. If you really do not previously have a copy, you can obtain Visual Studio 2019 below.

Create a console application challenge in Visual Studio

1st off, let us develop a .Internet Main console application challenge in Visual Studio. Assuming Visual Studio 2019 is mounted in your program, follow the measures outlined below to develop a new .Internet Main console application challenge in Visual Studio.

  1. Start the Visual Studio IDE.
  2. Simply click on “Create new challenge.”
  3. In the “Create new project” window, pick “Console Application (.Internet Main)” from the list of templates exhibited.
  4. Simply click Next.
  5. In the “Configure your new project” window revealed subsequent, specify the title and area for the new challenge.
  6. Simply click Create.

This will develop a new .Internet Main console application challenge in Visual Studio 2019. We’ll use this challenge in the subsequent sections of this write-up.

Examine-only collections, dictionaries, and lists in .Internet Main

The IReadOnlyCollection interface extends the IEnumerable interface and represents a essential browse-only selection interface. It also features a Depend property apart from the IEnumerable users as revealed in the code snippet supplied below.

IReadOnlyCollection facts = products
int numOfRecords = facts.Depend

The IReadOnlyDictionary interface gives a browse-only view of a dictionary and represents a browse-only selection of crucial/worth pairs. The next code snippet illustrates how you can determine an IReadOnlyDictionary occasion.

Copyright © 2021 IDG Communications, Inc.