Skip to content

Instantly share code, notes, and snippets.

@elbruno
Created January 4, 2021 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elbruno/698509147608178ba1bfea59f53234b5 to your computer and use it in GitHub Desktop.
Save elbruno/698509147608178ba1bfea59f53234b5 to your computer and use it in GitHub Desktop.
CSharp9RecordAndWith.cs
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