My Favorite C# Features - Part 1: Strong-types, Implicit-types, Anonymous-types, and Target-types
Tags: dotnet, csharp, programming
Published: 2021-02-19 16:10:15

---
title: My Favorite C# Features - Part 1: Strong-types, Implicit-types, Anonymous-types, and Target-types  
published: true
description: C# is a strongly-typed language and with the latest features in C# 9 and .NET 5, you can declare variables using the format that makes you happy.
tags: dotnet, csharp, programming
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nb50amjneu7zqtwxcsc3.PNG
series: My Favorite C# Features
---

I've been a long-time developer and fan of the C# programming language. Heck, I even named [my blog](https://csharpfritz.com) and took the nickname [csharpfritz](https://twitch.tv/csharpfritz)  One of my favorite capabilities of the language is the immediate compiler checking and tooling support for type-checking.  The instant check can save time and prevent a number of bugs in writing code quickly.  With the latest updates in C# 9 and .NET 5, we now have ultimate flexibility in how types are declared and interacted with in our code.  Let's review the easy ways to declare and initialize types with C#.

## In the beginning, there was explicit-typing

The original versions of C# required explicit declaration of every type in your code, and it felt so redundant to type the same names over and over again:

```c#
Sandwich myBLT = new Sandwich();
myBLT.Toppings.Add("Bacon");
myBLT.Toppings.Add("Lettuce");
myBLT.Toppings.Add("Tomato");
myBLT.Bread = "Gluten-Free";

Sandwich yourSandwich = new Sandwich();
// prep your sandwich as you'd prefer
```

Whether you are declaring the variable or initializing it with a class, you must explicitly declare the type assigned to the variable.  Both `myBLT` and `yourSandwich` were required to have the type declaration `Sandwich` preceding them.

This syntax has been valid since C# v1 and is still valid today.

## Enter Implicit-Typing and Initializers

The language was simplified a bit with C# 3 in 2007 when the [`var` keyword and implicit typing](https://docs.microsoft.com/dotnet/csharp/language-reference/keywords/var?WT.mc_id=friends-0000-jefritz) was introduced.  The compiler would use this keyword to infer variable types when they were initialized.  The C# team also introduced initializers in C# 3, a feature that would allow you to assign properties at declaration time.

```c#
var myBLT = new Sandwich {
  Toppings = new[] {"Bacon", "Lettuce", "Tomato"},
  Bread = "Gluten-Free"
};

Sandwich yourSandwich = new Sandwich();
// prep your sandwich as you'd prefer
```

The `var` keyword delivers no performance hit to your running code, nor does it add to compile time.  It is strictly a bit of 'syntactic sugar' that makes it easier to initialize and declare variables.  I've demonstrated an initializer in the above sample by setting the `Bread` value to "Gluten-Free" and I've introduced another feature that arrived with C# 3: an implictly-typed array of strings assigned to the `Toppings` property of `mySandwich`.  The compiler determines that the array is an array of strings and assigns the type appropriately.

## Types aren't for me - Anonymous Types

At the same time Implicit-Typing was introduced, the [anonymous type](https://docs.microsoft.com/dotnet/csharp/programming-guide/classes-and-structs/anonymous-types) was added to C#.  With this feature, you can declare a type with a set of read-only properties.

```c#
var myBLT = new Sandwich {
  Toppings = new[] {"Bacon", "Lettuce", "Tomato"},
  Bread = "Gluten-Free"
};

Sandwich yourSandwich = new Sandwich();
// prep your sandwich as you'd prefer

var ourLunchOrder = new {
  MyItem = myBLT,
  YourItem = yourSandwich
};
```

We can now inspect the `outLunchOrder` variable and read the properties.  While anonymous types are convenient for prototyping, they shouldn't be used across namespace or memory boundaries, preferring instead to deliver a clear API to your interacting methods.

## Welcome to 2021 - Target-Types

Implicit-typing forced the definition of types to the right of the equals sign when initializing a variable.  Perhaps you'd prefer to keep the types on the left of the equals sign, so that all types are in the same location in your code.  With C# 9 and .NET 5, this feature is now available to you and it's called [Target-Typing](https://docs.microsoft.com/dotnet/csharp/whats-new/csharp-9#fit-and-finish-features).  Let's rewrite our lunch order using this syntax:

```c#
Sandwich myBLT = new() {
  Toppings = new[] {"Bacon", "Lettuce", "Tomato"},
  Bread = "Gluten-Free"
};

Sandwich yourSandwich = new Sandwich();
// prep your sandwich as you'd prefer

var ourLunchOrder = new {
  MyItem = myBLT,
  YourItem = yourSandwich
};
```

Additionally, when calling methods and passing in new objects you can use the `new()` keyword to create an object of the expected type:

```c#
IceCreamSundae myDessert = new();
myDessert.Toppings.Add(new() { Flavor="Chocolate" });
```

## Summary

The C# programming language is so flexible and allows you to take advantage of the multitude of resources in the .NET frameworks easily and in the way you want.  With a smart compiler that provides access to alternate syntax that gives you the freedom to write code so that it is readable for you, I'm happier than ever to be using C#.

Over the next few weeks, I'm going to write additional entries in this series about my favorite features of C#.  What do you like about the language?  What do you want to see some discussion and samples of?  I'll be back next week with more C# features to discuss.

Looking to get started learning C#? Checkout our [free on-demand courses on Microsoft Learn](https://docs.microsoft.com/users/dotnet/collections/yz26f8y64n7k07?WT.mc_id=friends-0000-jefritz)!  Every Monday, check out my C# with CSharpFritz live-video training series [on Twitch](https://twitch.tv/visualstudio) and [archived on YouTube](https://www.youtube.com/playlist?list=PLdo4fOcmZ0oXv32dOd36UydQYLejKR61R)