diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a80b2c5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,287 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.sln.docstates + +# Build results + +[Dd]ebug/ +[Rr]elease/ +x64/ +[Bb]in/ +[Oo]bj/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.log +*.svclog +*.scc + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opensdf +*.sdf +*.cachefile + +# Visual Studio profiler +*.psess +*.vsp +*.vspx + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# Click-Once directory +publish/ + +# Publish Web Output +*.Publish.xml +*.pubxml +*.azurePubxml + +# NuGet Packages Directory +## TODO: If you have NuGet Package Restore enabled, uncomment the next line +packages/ +## TODO: If the tool you use requires repositories.config, also uncomment the next line +!packages/repositories.config + +# Windows Azure Build Output +csx/ +*.build.csdef + +# Windows Store app package directory +AppPackages/ + +# Others +sql/ +*.Cache +ClientBin/ +[Ss]tyle[Cc]op.* +![Ss]tyle[Cc]op.targets +~$* +*~ +*.dbmdl +*.[Pp]ublish.xml + +*.publishsettings + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file to a newer +# Visual Studio version. Backup files are not needed, because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +App_Data/*.mdf +App_Data/*.ldf + +# ========================= +# Windows detritus +# ========================= + +# Windows image file caches +Thumbs.db +ehthumbs.db + +# Folder config file +Desktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Mac desktop service store files +.DS_Store + +_NCrunch* \ No newline at end of file diff --git a/Example1/Program.cs b/Example1/Program.cs new file mode 100644 index 0000000..148b6c6 --- /dev/null +++ b/Example1/Program.cs @@ -0,0 +1,29 @@ +using System; + +namespace StackVsHeap1 +{ + internal class Program + { + static void Main(string[] args) + { + var dateOfBirth = new DateTime(1987, 05, 01); + var age = CalculateAge(dateOfBirth); + + Console.WriteLine(age); + } + + private static int CalculateAge(DateTime dateOfBirth) + { + var today = DateTime.Today; + var age = today.Year - dateOfBirth.Year; + + if (dateOfBirth.Date > today.AddYears(-age)) + age--; + + return age; + } + } +} + + + diff --git a/Example1/StackVsHeap1.csproj b/Example1/StackVsHeap1.csproj new file mode 100644 index 0000000..0ca377e --- /dev/null +++ b/Example1/StackVsHeap1.csproj @@ -0,0 +1,11 @@ + + + + Exe + net7.0 + StackVsHeap1 + disable + enable + + + diff --git a/Example2/Program.cs b/Example2/Program.cs new file mode 100644 index 0000000..3ffe372 --- /dev/null +++ b/Example2/Program.cs @@ -0,0 +1,34 @@ +using System; + +namespace StackVsHeap2 +{ + internal class Program + { + const int MAX_AGE = 99; + + static void Main(string[] args) + { + var dateOfBirth = new DateTime(1900, 05, 01); + var age = CalculateAge(dateOfBirth); + + if (age >= MAX_AGE) + Console.WriteLine($"Are you sure you are {age}?"); + else + Console.WriteLine(age); + } + + private static int CalculateAge(DateTime dateOfBirth) + { + var today = DateTime.Today; + var age = today.Year - dateOfBirth.Year; + + if (dateOfBirth.Date > today.AddYears(-age)) + age--; + + return age; + } + } +} + + + diff --git a/Example2/StackVsHeap2.csproj b/Example2/StackVsHeap2.csproj new file mode 100644 index 0000000..bc7f20a --- /dev/null +++ b/Example2/StackVsHeap2.csproj @@ -0,0 +1,11 @@ + + + + Exe + net7.0 + StackVsHeap2 + disable + enable + + + diff --git a/Example3/Program.cs b/Example3/Program.cs new file mode 100644 index 0000000..896663e --- /dev/null +++ b/Example3/Program.cs @@ -0,0 +1,34 @@ +using System; + +namespace StackVsHeap3 +{ + internal class Program + { + static void Main(string[] args) + { + var maxAge = 99; + var dateOfBirth = new DateTime(1900, 05, 01); + var calculateAge = () => + { + var today = DateTime.Today; + var age = today.Year - dateOfBirth.Year; + + if (dateOfBirth.Date > today.AddYears(-age)) + age--; + + return age; + }; + + var age = calculateAge(); + + if (age >= maxAge) + Console.WriteLine($"Are you sure you are {age}?"); + else + Console.WriteLine(age); + } + + } +} + + + diff --git a/Example3/StackVsHeap3.csproj b/Example3/StackVsHeap3.csproj new file mode 100644 index 0000000..de7eb21 --- /dev/null +++ b/Example3/StackVsHeap3.csproj @@ -0,0 +1,11 @@ + + + + Exe + net7.0 + StackVsHeap3 + disable + enable + + + diff --git a/Example4/Program.cs b/Example4/Program.cs new file mode 100644 index 0000000..a1c5f51 --- /dev/null +++ b/Example4/Program.cs @@ -0,0 +1,44 @@ +using System; +using System.Threading.Tasks; + +namespace StackVsHeap4 +{ + internal class Program + { + static async Task Main(string[] args) + { + var dateOfBirth = new DateTime(1900, 05, 01); + var ageTask = CalculateAge(dateOfBirth); + var otherTask = DoSomethingElse(); + + await Task.WhenAll(ageTask, otherTask); + + var age = ageTask.Result; + + Console.WriteLine(age); + } + + private static async Task CalculateAge(DateTime dateOfBirth) + { + var today = DateTime.Today; + var age = today.Year - dateOfBirth.Year; + + await Task.Delay(1000); // Pretending to do work + + if (dateOfBirth.Date > today.AddYears(-age)) + age--; + + return age; + } + + + private static async Task DoSomethingElse() + { + Console.WriteLine("Doing some other work"); + await Task.Delay(2000); // Pretending to do work + } + } +} + + + diff --git a/Example4/StackVsHeap4.csproj b/Example4/StackVsHeap4.csproj new file mode 100644 index 0000000..548203d --- /dev/null +++ b/Example4/StackVsHeap4.csproj @@ -0,0 +1,11 @@ + + + + Exe + net7.0 + StackVsHeap4 + disable + enable + + + diff --git a/README.md b/README.md index e41e1f8..3f78c59 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,6 @@ # stack-vs-heap -The code from my YouTube video for Stack vs Heap \ No newline at end of file +> [!NOTE] +> [This repository](https://code.alexhyett.com/alexhyett/stack-vs-heap) is also mirrored on [Codeberg.org](https://codeberg.org/alexhyett/stack-vs-heap) if you want to raise an issue. + +The code from my YouTube video for [Stack vs Heap](https://youtu.be/5OJRqkYbK-4) \ No newline at end of file