Skip to content

Instantly share code, notes, and snippets.

View sunmeat's full-sized avatar
🐈
MEOW

Oleksandr Zahoruiko sunmeat

🐈
MEOW
View GitHub Profile
@sunmeat
sunmeat / Program.cs
Created November 24, 2023 13:59
builder C# example
/* Product */
class Pizza
{
private string dough = ""; // тесто, корж
private string sauce = ""; // соус
private string topping = ""; // начинка
public void SetDough(string dough)
{
this.dough = dough;
@sunmeat
sunmeat / Program.cs
Created November 24, 2023 13:56
singleton logger C# example
class Logger
{
private static Logger instance; // 1) приватная статическая ссылка на единственный экземпляр класса
private int logCount = 0; // сколько раз происходила запись строки в файл
private Logger() // 2) конструктор - приватный (запрещает создавать объекты за пределами класса)
{
}
public static Logger GetInstance() // 3) публичный статический геттер на получение единственного объекта
@sunmeat
sunmeat / Program.cs
Created November 24, 2023 13:53
singleton pattern C# example
class Singleton
{
private static Singleton instance;
private int value;
private Singleton()
{
value = 50;
}
@sunmeat
sunmeat / Program.cs
Created November 24, 2023 13:38
composition example C#
class PegLeg // деревянная нога
{
public string Color { get; set; } // brown will be OK
public bool Dirty { get; set; } // yes / no
public double Length { get; set; } // inches
public int Usability { get; set; } // 0 - 100%
public PegLeg()
{
Console.WriteLine("Peg Leg C-TOR!");
@sunmeat
sunmeat / Program.cs
Created November 24, 2023 13:15
aggregation example C#
class Hat
{
public string Color { get; set; }
public string Model { get; set; }
public double Price { get; set; }
public override string ToString()
{
return Model;
}
@sunmeat
sunmeat / Program.cs
Created November 24, 2023 13:06
assossiation example C#
using System;
class Person
{
public string Name { get; set; } // 1) наличие свойства с типом другого класса/структуры - это ассоциация
public string Surname { get; set; }
public string SaySomething() // 2) возврат объекта с типом другого класса/структуры - это тоже ассоциация
{
string local; // 3) создание локальной переменной/константы с типом другого класса/структуры - это третий вариант ассоциации
@sunmeat
sunmeat / Program.cs
Created October 23, 2023 10:27
read attribute by reflection
using System.Reflection;
[AttributeUsage(AttributeTargets.Property)]
public class MyAttribute : Attribute
{
public string Description { get; }
public MyAttribute(string description)
{
Description = description;
@sunmeat
sunmeat / main.cpp
Created October 20, 2023 12:43
copy current exe to startup folder - cpp console app example with runas administrator option
#include <iostream>
#include <windows.h>
#include <shlwapi.h>
using namespace std;
int main()
{
setlocale(0, "");
////////////////////////////////////////////////////////////////
@sunmeat
sunmeat / example.cs
Last active March 20, 2023 06:50
character background and foreground color in particular coordinates C# console application example
using System.Runtime.InteropServices;
class Tester
{
public static void GetColor(short y, short x, uint length = 1)
{
var colors = new ushort[1];
uint numberOfCharactersRead;
if (ReadConsoleOutputAttribute(GetStdHandle(-11), colors, length, new Coord(x, y), out numberOfCharactersRead))