Skip to content

Commit 9c41bb9

Browse files
CM-1173: Differences Between Record Struct and Record Class in C# (#2044)
* initial repo * comments * test names * Move PR to folder csharp-classes-struct-record --------- Co-authored-by: Florian Prinz (FLPR) <flpr@simcorp.com>
1 parent df9f903 commit 9c41bb9

7 files changed

Lines changed: 128 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.10.34916.146
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DifferencesRecordStructAndRecordClassInCSharp", "DifferencesRecordStructAndRecordClassInCSharp\DifferencesRecordStructAndRecordClassInCSharp.csproj", "{B69E64F7-7941-47FE-933E-F57D9A5ED40E}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{C1552103-B1F0-456F-9B6C-A26103B9856C}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{B69E64F7-7941-47FE-933E-F57D9A5ED40E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{B69E64F7-7941-47FE-933E-F57D9A5ED40E}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{B69E64F7-7941-47FE-933E-F57D9A5ED40E}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{B69E64F7-7941-47FE-933E-F57D9A5ED40E}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{C1552103-B1F0-456F-9B6C-A26103B9856C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{C1552103-B1F0-456F-9B6C-A26103B9856C}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{C1552103-B1F0-456F-9B6C-A26103B9856C}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{C1552103-B1F0-456F-9B6C-A26103B9856C}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {851C533B-330F-416C-A6C0-E14B8B8228E8}
30+
EndGlobalSection
31+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace DifferencesRecordStructAndRecordClassInCSharp;
2+
public static class DefaultValues
3+
{
4+
public static PersonRecordClass? GetRecordClassDefaultValue()
5+
{
6+
return default(PersonRecordClass);
7+
}
8+
9+
public static PersonRecordStruct? GetRecordStructDefaultValue()
10+
{
11+
return default(PersonRecordStruct);
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using DifferencesRecordStructAndRecordClassInCSharp;
2+
3+
var recordClassDefaultValue = DefaultValues.GetRecordClassDefaultValue();
4+
var recordStructDefaultValue = DefaultValues.GetRecordStructDefaultValue();
5+
Console.WriteLine($"Record class default value is {(recordClassDefaultValue == null ? "null" : recordClassDefaultValue)}");
6+
Console.WriteLine($"Record struct default value is {(recordStructDefaultValue == null ? "null" : recordStructDefaultValue)}");
7+
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public record struct PersonRecordStruct(string Name, DateTime BirthdayDate, int YearsOld, string[] PhoneNumbers);
2+
3+
public readonly record struct PersonRecordStructReadOnly(string Name, DateTime BirthdayDate, int YearsOld, string[] PhoneNumbers);
4+
5+
public record class PersonRecordClass(string Name, DateTime BirthdayDate, int YearsOld, string[] PhoneNumbers);
6+
7+
public record class PersonWithAddressRecordClass(string Name, DateTime BirthdayDate, int YearsOld, string[] PhoneNumbers, string Address) :
8+
PersonRecordClass(Name, BirthdayDate, YearsOld, PhoneNumbers);
9+
10+
public record class PersonRecordClassInitSetters
11+
{
12+
public required string Name { get; init; }
13+
public DateTime BirthdayDate { get; init; }
14+
public int YearsOld { get; init; }
15+
public required string[] PhoneNumbers { get; init; }
16+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using DifferencesRecordStructAndRecordClassInCSharp;
2+
3+
namespace Tests;
4+
5+
[TestClass]
6+
public class DefaultValueTests
7+
{
8+
[TestMethod]
9+
public void GivenRecordStruct_WhenGetDefaultValue_ThenReturnsDefaultInitializedValues()
10+
{
11+
var recordStructDefaultValue = DefaultValues.GetRecordStructDefaultValue();
12+
13+
Assert.IsTrue(recordStructDefaultValue is not null);
14+
}
15+
16+
[TestMethod]
17+
public void GivenRecordClass_WhenGetDefaultValue_ThenReturnsNull()
18+
{
19+
var recordClassDefaultValue = DefaultValues.GetRecordClassDefaultValue();
20+
21+
Assert.IsTrue(recordClassDefaultValue is null);
22+
}
23+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
8+
<IsPackable>false</IsPackable>
9+
<IsTestProject>true</IsTestProject>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="coverlet.collector" Version="6.0.0" />
14+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
15+
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
16+
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
17+
</ItemGroup>
18+
19+
<ItemGroup>
20+
<ProjectReference Include="..\DifferencesRecordStructAndRecordClassInCSharp\DifferencesRecordStructAndRecordClassInCSharp.csproj" />
21+
</ItemGroup>
22+
23+
<ItemGroup>
24+
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
25+
</ItemGroup>
26+
27+
</Project>

0 commit comments

Comments
 (0)