What are additional types in C Sharp?
In the fascinating world of C Sharp, not only classes play a central role in object-oriented programming. There are other types, such as structures and records, which, although less well-known, are equally relevant for specific scenarios. Taking advantage of these differences can optimize your code and make it easier to solve various problems in C Sharp.
What is the difference between class, structure and record?
Throughout the course, we have explored classes in depth. Here, the differences between class, structure, and register become vital:
- Classes: They operate through references. When an object is created, a memory location is reserved and each instance will be unique, even if the data is identical.
- Structures and registers: They operate based on values. Two objects with the same data are considered equal.
Classes are incredibly versatile and suitable for any level of complexity, from small components to large and complex solutions. In contrast, structures and records are ideal for small, value-centric objects.
When to use structures or records?
Structures and records are useful for:
- Comparing objects by value: ideal for very small objects with few properties.
- Immutable data: Especially relevant for records, where data does not change over time.
For example, if you need to compare properties of objects without evaluating each one manually, records provide an efficient solution.
Practical example: comparing classes and records
Comparing with classes
In the following example, we create two objects superman
and superman2
based on the superhero
class. Both have the same data, but when comparing them, the result is false
because they are separate instances with different memory references:
var superman = new Superhero { Id = 1, Name = "Superman", SecretIdentity = "Clark Kent" };varsuperman2 = new Superhero { Id = 1, Name = "Superman", SecretIdentity = "Clark Kent" };Console.WriteLine(superman == superman2);
How records work
When using records, the same comparison would evaluate to true
, since C Sharp allows comparing internal value sets directly:
public record SuperheroRecord(int Id, string Name, string SecretIdentity);
var superhero1 = new SuperheroRecord(1, "Superman", "Clark Kent");var superhero2 = new SuperheroRecord(1, "Superman", "Clark Kent");Console.WriteLine(superhero1 == superhero2);
As you can see, while in classes the result is false
, with records it is true
.
Why use records in microservices?
Imagine you work with microservices, where logs of multiple messages must be compared. Here, logs are a perfect choice, as they simplify the comparison and allow a more direct and efficient approach, without the need for property-by-property verification.
Recommended exercise
To put what you have learned into practice, we invite you to:
- Complete the
record
type by adding more properties and compare values.
- Create a
structure
type and perform a similar exercise.
Coming soon: Access modifiers in C Sharp
The knowledge continues to grow. In the next lesson, we will explore access modifiers in C Sharp, further enriching your skills and understanding of the language. See you there!
Want to see more contributions, questions and answers from the community?