38
loading...
This website collects cookies to deliver better user experience
This blog post is part of the 2021 C# Advent Calendar (the fifth year!). Please check out all of the other awesome posts in this years C# Advent.
record
type in C# these days, and keep thinking to yourself, “how are those different from just a regular ‘ole class?” or, “how is a record different from a struct?”, or, “what the crap are these things anyway?”class
first, because that’s probably what the majority of folx are familiar with.class
highlights -class
is a reference type and an instance can be created by using the new
operator or by assigning a compatible type.using System;
public class Person
{
public string FirstName { get; }
public string LastName { get; }
public Person(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName
}
}
using System;
public class Program
{
public static void Main()
{
var person1 = new Person("Calvin", "Allen");
Console.WriteLine(person1.FirstName);
Console.WriteLine(person1.LastName);
}
}
struct
. A struct
looks very similar to a class
, but instead of being a reference type, a struct
is considered a value type. Struct types are typically used for small, data-centric types that provide little to NO behavior. If behavior is important, then consider using a class
instead.struct
is a value type and an instance can be created by using the new
operator or by assigning a compatible type.System.ValueType
(which inherit from object
), but you can not have them inherit from your own base object (they can implement interfaces, however).struct
instead of a class
using System;
public struct Person
{
public string FirstName { get; }
public string LastName { get; }
public Person(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName
}
}
using System;
public class Program
{
public static void Main()
{
var person1 = new Person("Calvin", "Allen");
Console.WriteLine(person1.FirstName);
Console.WriteLine(person1.LastName);
}
}
public class Person
to public struct Person
.class
more often than not, and the struct
type is usually non-existant in the codebase. Having said that, your mileage may definitely vary depending on the type of application you’re building.record
types (introduced in C# 9, and enhanced again in C# 10 - I’m using C# 10 conventions in this post)? Well, they’re a lot like everything else we’ve seen so far - in fact, a record
is really a class
or struct
“under the hood”, that just provides special syntax and behavior.record
, which is just shorthand in code for, record class
, is a reference type, and a record struct
is a value type. An instance can be created by using the new
operator or by assigning a compatible type.positional parameters
in a record class
creates immutable properties. However, in a record struct
, those same positional parameters
would be entirely mutable. You can work around that by declaring your record struct
as a readonly record struct
.record class
can inherit from another record, but not from a class. Likewise, a class can’t inherit from a record. A record struct
does not allow for inheritance like a traditional struct
.record class
and record struct
use value equality, which means two objects must share the same type, and contain the same value or values.positional parameters
)-using System;
public record class Person(string FirstName, string LastName);
using System;
public class Program
{
public static void Main()
{
var person1 = new Person("Calvin", "Allen");
Console.WriteLine(person1.FirstName);
Console.WriteLine(person1.LastName);
}
}
record class
instead of a class
? Maybe.struct
instead of a record struct
? Maybe. Can you use a class
instead of a struct
? Maybe.struct
, record class
, or record struct
for your entities, since Entity Framework depends on reference equality, and these types utilize value equality.This post, “What’s the Difference Anyway?! Class, Struct, Record, oh my!”, first appeared on https://www.codingwithcalvin.net/whats-the-difference-anyway-class-struct-record-oh-my