Class vs Dynamic vs Tuple in C#
Tags: csharp, dotnet, beginners
Published: 2020-03-05 18:01:23

---
title: Class vs Dynamic vs Tuple in C# 
published: true
description: 
tags: #csharp #dotnet #beginners
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/bfhqoyoxnuxr5sksfoe6.png
---



Nowadays, with c# 8.0 we have a lot of options to make code, with all these options, it is hard for the first time to know-how is the best option to use when we create complex objects. 

With a functional approach inserting more and more in .NET framework, some people use less code thinking in class and object or using an Object-Oriented approach (when all is an object or a Class). 

This is more evident with the new data structure "Tuple", inserted in c# 7.0.


But how and when I choose between Class, Dynamic or Tuple? Well, we have some options here...

## 1. Class
Real classes are very useful when we use a DDD approach to architect our systems, creating a class is very simple, just create a new file in your project and we can create our class to make instances of then after



```csharp
public class Person
{
    public string FirstName {get;set;}
    public string LastName {get;set;}
    public int Age {get;set;}
}
```

Classes are very useful to prevent duplicates in your code, besides, we have a lot of documentation of software architecture, design patterns, SOLID teaching how we can organize classes to reduce the complexity of our code. Real classes are evident when a new programmer is analyzing our code.

## 2. Anonymous Type Object
Anonymous is a feature introduced in c# 3.0 to create complex structures in real-time, with a name and a value, eg...

```csharp
var Person = new 
{
   FirstName = "Leonardo",
   LastName = "Gasparini",
   Age = 26
}
```

Anonymous types are useful when we deal with Linq operations to select some properties in our program we did not reuse that code in other parts eg...

```csharp
var PersonCompleteNameList = Persons.Select(Person => new{CompleteName = Person .FirstName + " " + Person.LastName})
```

Anonymous types have a lighter structure comparing with classes because we don't have many options to create constructors or functions inside dynamic. I like to use dynamic when I working on with JSON objects that change all the time and I work on it in a few methods 

## 3. Tuple
A tuple is a data structure introduced as a feature in c# 7.0. Tuples are common in other languages like Python to manage complex types without a class. here an example of a tuple

```csharp
//Unamed tuple
var Person = ("Leonardo", "Gasparini", 26);

//Named tuple
Person = (FirstName: "Leonardo", LastName:"Gasparini", Age:26);
```

Tuples are not very different a dynamic when we use a named type, but tuples can be lighter than dynamic because we don't need to name properties of the tuple, besides, comparison of tuples are more easily to do comparing to dynamic and are useful when we need a return a value inside a method.

### Which of this I choose to my next project?
And the answer is...depends. this is an example that code does not have a single way to learn and use, c# have all these options to make easier any type of programmer to feel comfortable with the language. Even with some differences and advantages among then, is possible to assume that all these options are a good suit to programming...

Some useful links:
[When to use: Tuple vs Class c# 7.0](https://stackoverflow.com/questions/44650636/when-to-use-tuple-vs-class-c-sharp-7-0)
[Value Tuples Vs Anonymous Types Performance](https://stackoverflow.com/questions/48118187/value-tuples-vs-anonymous-types-performance)
[What are the advantages of declaring anonymous type Object ?](https://www.codeproject.com/Questions/842992/what-is-the-advantages-of-declaring-anonymous-type)