Simple introduction for ISet
Created
July 10, 2024 13:28
-
-
Save karenpayneoregon/2df936d0acf3f42f7b99320ea2fc9e90 to your computer and use it in GitHub Desktop.
ISet basic sample
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.ComponentModel; | |
#pragma warning disable CA1067 | |
namespace YourNamespace.Models; | |
public class Person : INotifyPropertyChanged, IEquatable<Person> | |
{ | |
private int _id; | |
private string _firstName; | |
private string _lastName; | |
private DateOnly _birthDate; | |
public int Id | |
{ | |
get => _id; | |
set | |
{ | |
if (value == _id) return; | |
_id = value; | |
OnPropertyChanged(nameof(Id)); | |
} | |
} | |
public string FirstName | |
{ | |
get => _firstName; | |
set | |
{ | |
if (value == _firstName) return; | |
_firstName = value; | |
OnPropertyChanged(nameof(FirstName)); | |
} | |
} | |
public string LastName | |
{ | |
get => _lastName; | |
set | |
{ | |
if (value == _lastName) return; | |
_lastName = value; | |
OnPropertyChanged(nameof(LastName)); | |
} | |
} | |
public DateOnly BirthDate | |
{ | |
get => _birthDate; | |
set | |
{ | |
if (value.Equals(_birthDate)) return; | |
_birthDate = value; | |
OnPropertyChanged(nameof(BirthDate)); | |
} | |
} | |
public bool Equals(Person compareTo) | |
=> (FirstName == compareTo.FirstName && | |
LastName == compareTo.LastName && | |
BirthDate == compareTo.BirthDate); | |
public override int GetHashCode() | |
=> HashCode.Combine(FirstName, LastName, BirthDate); | |
public event PropertyChangedEventHandler? PropertyChanged; | |
protected virtual void OnPropertyChanged(string propertyName) | |
{ | |
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); | |
} | |
public override string ToString() => $"{FirstName,-12}{LastName}"; | |
} |
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
namespace YourNamespace; | |
internal partial class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var frozenSet = FrozenPeople(); | |
foreach (var person in frozenSet) | |
Console.WriteLine(person); | |
} | |
private static FrozenSet<Person> FrozenPeople() | |
{ | |
ISet<Person> peopleSet = new HashSet<Person>([ | |
new() { Id = 1, FirstName = "Karen", LastName = "Payne", BirthDate = new DateOnly(1956,9,24)}, | |
new() { Id = 2, FirstName = "Sam", LastName = "Smith", BirthDate = new DateOnly(1976,3,4) }, | |
new() { Id = 1, FirstName = "Karen", LastName = "Payne", BirthDate = new DateOnly(1956,9,24) } | |
]); | |
peopleSet.UnionWith([ | |
new() { Id = 1, FirstName = "Karen", LastName = "Payne", BirthDate = new DateOnly(1956,9,24)}, | |
new() { Id = 2, FirstName = "Sam", LastName = "Smith", BirthDate = new DateOnly(1976,3,4) }, | |
new() { Id = 3, FirstName = "Frank", LastName = "Adams", BirthDate = new DateOnly(1966,3,4) }, | |
new() { Id = 1, FirstName = "Karen", LastName = "Payne", BirthDate = new DateOnly(1956,9,24) } | |
]); | |
peopleSet.ExceptWith([ | |
new() { Id = 2, FirstName = "Sam", LastName = "Smith", BirthDate = new DateOnly(1976,3,4) }, | |
new() { Id = 3, FirstName = "Frank", LastName = "Adams", BirthDate = new DateOnly(1966,3,4) }, | |
]); | |
return peopleSet.ToFrozenSet(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment