Skip to content

Instantly share code, notes, and snippets.

@sunmeat
Last active January 8, 2026 18:02
Show Gist options
  • Select an option

  • Save sunmeat/e99984200f754f3ff32e3f3a3322ab41 to your computer and use it in GitHub Desktop.

Select an option

Save sunmeat/e99984200f754f3ff32e3f3a3322ab41 to your computer and use it in GitHub Desktop.
aggregation C# example
using System;
namespace AggregationExample
{
public class Hat
{
public string Color { get; set; }
public string Model { get; set; }
public double Price { get; set; }
}
public class Person
{
public string? Name { get; set; }
public Hat? Hat { get; private set; } // агрегування - поле з типом іншого класу
public void TakeHat(Hat hat) // важливо: капелюх береться ззовні, а не створюється одночасно з людиною
{
this.Hat = hat;
}
public void GoWalk()
{
Console.WriteLine("раптом подув вітер...");
Hat = null;
}
public void WhereIsYourHat()
{
Console.WriteLine("а ось вона: " + Hat);
}
}
class Program
{
static void Main()
{
Console.OutputEncoding = System.Text.Encoding.UTF8;
var hat = new Hat();
var olena = new Person();
olena.TakeHat(hat);
olena.WhereIsYourHat();
olena.GoWalk();
olena.WhereIsYourHat();
Console.WriteLine("капелюх: " + hat);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment