Created
January 4, 2021 15:00
-
-
Save elbruno/698509147608178ba1bfea59f53234b5 to your computer and use it in GitHub Desktop.
CSharp9RecordAndWith.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.CompilerServices; | |
using System.Runtime.InteropServices; | |
using System.Security.Cryptography.X509Certificates; | |
namespace ConsoleApp3 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("start"); | |
var bruno = new PersonRecord("Bruno", 40, true); | |
Console.WriteLine($"{bruno.ToString()}"); | |
Console.WriteLine($"Bruno Is Married created"); | |
var brunoMarried = bruno.CopyUsingMarriedTrue(); | |
Console.WriteLine($"{brunoMarried }"); | |
Console.WriteLine($"-----------------------------------"); | |
var valentino = new PersonRecord("Valentino", 13, false); | |
Console.WriteLine($"{valentino.ToString()}"); | |
Console.WriteLine($"Valentino Is Married created"); | |
var valentinoMarried = valentino.CopyUsingMarriedTrue(); | |
Console.WriteLine($"{valentinoMarried}"); | |
} | |
} | |
public record PersonRecord(string Name, int Age, bool Married = false) | |
{ | |
public Guid Id { get; init; } = Guid.NewGuid(); | |
public override string ToString() => $"Name: {Name} - Age: {Age} - Married: {Married} - Guid: {Id}"; | |
public PersonRecord CopyUsingMarriedTrue() => this with { Married = true }; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment