July 17, 2024


The Internet Generation

How to use anonymous types in C#

An anonymous type is a type that doesn’t have a name. You can use an anonymous type to encapsulate a set of read-only properties inside a single unit — and you don’t need to define the anonymous type beforehand. This article discusses what anonymous types are, why they are important, and how we can work with anonymous types in C#.

To work with the code examples provided in this article, you should have Visual Studio 2019 installed in your system. If you don’t already have a copy, you can download Visual Studio 2019 here. 

Create a console application project in Visual Studio

First off, let’s create a .NET Core console application project in Visual Studio. Assuming Visual Studio 2019 is installed in your system, follow the steps outlined below to create a new .NET Core console application project in Visual Studio.

  1. Launch the Visual Studio IDE.
  2. Click on “Create new project.”
  3. In the “Create new project” window, select “Console App (.NET Core)” from the list of templates displayed.
  4. Click Next.
  5. In the “Configure your new project” window shown next, specify the name and location for the new project.
  6. Click Create.