DEV Community

Cover image for Class vs Dynamic vs Tuple in C#
Leonardo Gasparini Romão
Leonardo Gasparini Romão

Posted on • Edited on

Class vs Dynamic vs Tuple in C#

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

public class Person
{
    public string FirstName {get;set;}
    public string LastName {get;set;}
    public int Age {get;set;}
}
Enter fullscreen mode Exit fullscreen mode

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...

var Person = new 
{
   FirstName = "Leonardo",
   LastName = "Gasparini",
   Age = 26
}
Enter fullscreen mode Exit fullscreen mode

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...

var PersonCompleteNameList = Persons.Select(Person => new{CompleteName = Person .FirstName + " " + Person.LastName})
Enter fullscreen mode Exit fullscreen mode

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

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

//Named tuple
Person = (FirstName: "Leonardo", LastName:"Gasparini", Age:26);
Enter fullscreen mode Exit fullscreen mode

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
Value Tuples Vs Anonymous Types Performance
What are the advantages of declaring anonymous type Object ?

Top comments (0)