Home > database >  Defrences between struct and record struct
Defrences between struct and record struct

Time:10-23

Microsoft says:

A structure type (or struct type) is a value type that can encapsulate data and related functionality.

Record struct is value type that encapsulates data.

Also says:

For struct,two objects are equal if they're of the same type and store same value

For record struct,two objects are equal if they're of the same type and store same value

For me both struct and record struct have same definition. Also compared compiler generated code for struct and record struct but still confused about when struct or struct record should be used. Do you have any real world example?

CodePudding user response:

I think the difference should be the same as that between class and record, so I declare a record struct in sharplab.

public record struct C(string name);

It shows me the following result.

public struct C : IEquatable<C>
{
    [CompilerGenerated]
    private string <name>k__BackingField;

    public string name
    {
        [IsReadOnly]
        [CompilerGenerated]
        get
        {
            return <name>k__BackingField;
        }
        [CompilerGenerated]
        set
        {
            <name>k__BackingField = value;
        }
    }

    public C(string name)
    {
        <name>k__BackingField = name;
    }

    [IsReadOnly]
    [CompilerGenerated]
    public override string ToString()
    {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.Append("C");
        stringBuilder.Append(" { ");
        if (PrintMembers(stringBuilder))
        {
            stringBuilder.Append(' ');
        }
        stringBuilder.Append('}');
        return stringBuilder.ToString();
    }

    [IsReadOnly]
    [CompilerGenerated]
    private bool PrintMembers(StringBuilder builder)
    {
        builder.Append("name = ");
        builder.Append((object)name);
        return true;
    }

    [CompilerGenerated]
    public static bool operator !=(C left, C right)
    {
        return !(left == right);
    }

    [CompilerGenerated]
    public static bool operator ==(C left, C right)
    {
        return left.Equals(right);
    }

    [IsReadOnly]
    [CompilerGenerated]
    public override int GetHashCode()
    {
        return EqualityComparer<string>.Default.GetHashCode(<name>k__BackingField);
    }

    [IsReadOnly]
    [CompilerGenerated]
    public override bool Equals(object obj)
    {
        if (obj is C)
        {
            return Equals((C)obj);
        }
        return false;
    }

    [IsReadOnly]
    [CompilerGenerated]
    public bool Equals(C other)
    {
        return EqualityComparer<string>.Default.Equals(<name>k__BackingField, other.<name>k__BackingField);
    }

    [IsReadOnly]
    [CompilerGenerated]
    public void Deconstruct(out string name)
    {
        name = this.name;
    }
}

As you can see a record struct is a common struct plus the following auto generated functionalities:

  • Constructor and Deconstructor with properties in the record declaration.
  • Equatable implementation.
  • ToString.

When struct record should be used

When you need a simple struct but you don't want to repetitively write above methods. For example a vector struct now can be declared as:

public record struct Vector(float x, float y, float z);

CodePudding user response:

A record is a reference type and follows value-based equality semantics. You can define a record struct to create a record that is a value type.

Reference https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/tutorials/records#characteristics-of-records

  • Related