finish lecture 6, edit lecture 10

- add em dash snippet
main
borb 2 weeks ago
parent 515fc40d32
commit 36809c09b3

@ -140,6 +140,13 @@
"</div>", "</div>",
] ]
}, },
"em dash" : {
"scope": "markdown",
"prefix": "em",
"body" : [
"—",
]
},
"marp footer" : { "marp footer" : {
"scope": "markdown", "scope": "markdown",
"prefix": "footer", "prefix": "footer",

@ -13,13 +13,13 @@
/* buutti.css */ /* buutti.css */
/* @theme buutti */div#\:\$p>svg>foreignObject>section .columns{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:calc(var(--marpit-root-font-size, 1rem) * 1)}div#\:\$p>svg>foreignObject>section .columns12{display:grid;grid-template-columns:1fr 2fr;gap:calc(var(--marpit-root-font-size, 1rem) * 1)}div#\:\$p>svg>foreignObject>section .columns21{display:grid;grid-template-columns:2fr 1fr;gap:calc(var(--marpit-root-font-size, 1rem) * 1)}div#\:\$p>svg>foreignObject>section .columns32{display:grid;grid-template-columns:3fr 2fr;gap:calc(var(--marpit-root-font-size, 1rem) * 1)}div#\:\$p>svg>foreignObject>section .columns23{display:grid;grid-template-columns:2fr 3fr;gap:calc(var(--marpit-root-font-size, 1rem) * 1)}div#\:\$p>svg>foreignObject>section .columns111{display:grid;grid-template-columns:1fr 1fr 1fr;gap:calc(var(--marpit-root-font-size, 1rem) * 1)}div#\:\$p>svg>foreignObject>section .centered{display:flex;flex-direction:column;justify-content:center;text-align:center}div#\:\$p>svg>foreignObject>section .tableborderless td,div#\:\$p>svg>foreignObject>section th{border:none!important;border-collapse:collapse}div#\:\$p>svg>foreignObject>section.extra{background-color:#5d275d;background-image:linear-gradient(to bottom,#401a40,#1d0c1d);color:white}div#\:\$p>svg>foreignObject>section.extra a{color:rgb(145,255,209)}div#\:\$p>svg>foreignObject>section.exercise{background-color:#29366f;background-image:linear-gradient(to bottom,#20636a,#173742);color:white}div#\:\$p>svg>foreignObject>section.exercise a{color:rgb(211,173,255)} /* @theme buutti */div#\:\$p>svg>foreignObject>section .columns{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:calc(var(--marpit-root-font-size, 1rem) * 1)}div#\:\$p>svg>foreignObject>section .columns12{display:grid;grid-template-columns:1fr 2fr;gap:calc(var(--marpit-root-font-size, 1rem) * 1)}div#\:\$p>svg>foreignObject>section .columns21{display:grid;grid-template-columns:2fr 1fr;gap:calc(var(--marpit-root-font-size, 1rem) * 1)}div#\:\$p>svg>foreignObject>section .columns32{display:grid;grid-template-columns:3fr 2fr;gap:calc(var(--marpit-root-font-size, 1rem) * 1)}div#\:\$p>svg>foreignObject>section .columns23{display:grid;grid-template-columns:2fr 3fr;gap:calc(var(--marpit-root-font-size, 1rem) * 1)}div#\:\$p>svg>foreignObject>section .columns111{display:grid;grid-template-columns:1fr 1fr 1fr;gap:calc(var(--marpit-root-font-size, 1rem) * 1)}div#\:\$p>svg>foreignObject>section .centered{display:flex;flex-direction:column;justify-content:center;text-align:center}div#\:\$p>svg>foreignObject>section .tableborderless td,div#\:\$p>svg>foreignObject>section th{border:none!important;border-collapse:collapse}div#\:\$p>svg>foreignObject>section.extra{background-color:#5d275d;background-image:linear-gradient(to bottom,#401a40,#1d0c1d);color:white}div#\:\$p>svg>foreignObject>section.extra a{color:rgb(145,255,209)}div#\:\$p>svg>foreignObject>section.exercise{background-color:#29366f;background-image:linear-gradient(to bottom,#20636a,#173742);color:white}div#\:\$p>svg>foreignObject>section.exercise a{color:rgb(211,173,255)}
/* @theme fa5agneqe2xxbat58gzzfpwqzuczahqrysq4tolfj */div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]{columns:initial!important;display:block!important;padding:0!important}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]:after,div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]:before,div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=content]:after,div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=content]:before{display:none!important}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]>div[data-marpit-advanced-background-container]{all:initial;display:flex;flex-direction:row;height:100%;overflow:hidden;width:100%}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]>div[data-marpit-advanced-background-container][data-marpit-advanced-background-direction=vertical]{flex-direction:column}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background][data-marpit-advanced-background-split]>div[data-marpit-advanced-background-container]{width:var(--marpit-advanced-background-split,50%)}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background][data-marpit-advanced-background-split=right]>div[data-marpit-advanced-background-container]{margin-left:calc(100% - var(--marpit-advanced-background-split, 50%))}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]>div[data-marpit-advanced-background-container]>figure{all:initial;background-position:center;background-repeat:no-repeat;background-size:cover;flex:auto;margin:0}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]>div[data-marpit-advanced-background-container]>figure>figcaption{position:absolute;border:0;clip:rect(0,0,0,0);height:1px;margin:-1px;overflow:hidden;padding:0;white-space:nowrap;width:1px}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=content],div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=pseudo]{background:transparent!important}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=pseudo],div#\:\$p>svg[data-marpit-svg]>foreignObject[data-marpit-advanced-background=pseudo]{pointer-events:none!important}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background-split]{width:100%;height:100%}</style></head><body><div class="bespoke-marp-osc"><button data-bespoke-marp-osc="prev" tabindex="-1" title="Previous slide">Previous slide</button><span data-bespoke-marp-osc="page"></span><button data-bespoke-marp-osc="next" tabindex="-1" title="Next slide">Next slide</button><button data-bespoke-marp-osc="fullscreen" tabindex="-1" title="Toggle fullscreen (f)">Toggle fullscreen</button><button data-bespoke-marp-osc="presenter" tabindex="-1" title="Open presenter view (p)">Open presenter view</button></div><div id=":$p"><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="1" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="fa5agneqe2xxbat58gzzfpwqzuczahqrysq4tolfj" lang="en-US" class="invert" data-marpit-pagination="1" style="--paginate:true;--class:invert;--heading-divider:5;--theme:fa5agneqe2xxbat58gzzfpwqzuczahqrysq4tolfj;" data-marpit-pagination-total="10"> /* @theme dil5c569ey7e10odkg3vpex7c4mrerdir64rpyfscbk */div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]{columns:initial!important;display:block!important;padding:0!important}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]:after,div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]:before,div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=content]:after,div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=content]:before{display:none!important}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]>div[data-marpit-advanced-background-container]{all:initial;display:flex;flex-direction:row;height:100%;overflow:hidden;width:100%}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]>div[data-marpit-advanced-background-container][data-marpit-advanced-background-direction=vertical]{flex-direction:column}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background][data-marpit-advanced-background-split]>div[data-marpit-advanced-background-container]{width:var(--marpit-advanced-background-split,50%)}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background][data-marpit-advanced-background-split=right]>div[data-marpit-advanced-background-container]{margin-left:calc(100% - var(--marpit-advanced-background-split, 50%))}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]>div[data-marpit-advanced-background-container]>figure{all:initial;background-position:center;background-repeat:no-repeat;background-size:cover;flex:auto;margin:0}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]>div[data-marpit-advanced-background-container]>figure>figcaption{position:absolute;border:0;clip:rect(0,0,0,0);height:1px;margin:-1px;overflow:hidden;padding:0;white-space:nowrap;width:1px}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=content],div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=pseudo]{background:transparent!important}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=pseudo],div#\:\$p>svg[data-marpit-svg]>foreignObject[data-marpit-advanced-background=pseudo]{pointer-events:none!important}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background-split]{width:100%;height:100%}</style></head><body><div class="bespoke-marp-osc"><button data-bespoke-marp-osc="prev" tabindex="-1" title="Previous slide">Previous slide</button><span data-bespoke-marp-osc="page"></span><button data-bespoke-marp-osc="next" tabindex="-1" title="Next slide">Next slide</button><button data-bespoke-marp-osc="fullscreen" tabindex="-1" title="Toggle fullscreen (f)">Toggle fullscreen</button><button data-bespoke-marp-osc="presenter" tabindex="-1" title="Open presenter view (p)">Open presenter view</button></div><div id=":$p"><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="1" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="dil5c569ey7e10odkg3vpex7c4mrerdir64rpyfscbk" lang="en-US" class="invert" data-marpit-pagination="1" style="--paginate:true;--class:invert;--heading-divider:5;--theme:dil5c569ey7e10odkg3vpex7c4mrerdir64rpyfscbk;" data-marpit-pagination-total="10">
<h1 id="static-members-methods-and-classes">Static Members, Methods and Classes</h1> <h1 id="static-members-methods-and-classes">Static Members, Methods and Classes</h1>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="2" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="fa5agneqe2xxbat58gzzfpwqzuczahqrysq4tolfj" lang="en-US" class="invert" data-marpit-pagination="2" style="--paginate:true;--class:invert;--heading-divider:5;--theme:fa5agneqe2xxbat58gzzfpwqzuczahqrysq4tolfj;" data-marpit-pagination-total="10"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="2" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="dil5c569ey7e10odkg3vpex7c4mrerdir64rpyfscbk" lang="en-US" class="invert" data-marpit-pagination="2" style="--paginate:true;--class:invert;--heading-divider:5;--theme:dil5c569ey7e10odkg3vpex7c4mrerdir64rpyfscbk;" data-marpit-pagination-total="10">
<h2 id="static-members">Static Members</h2> <h2 id="static-members">Static members</h2>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="3" data-marpit-fragments="2" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="fa5agneqe2xxbat58gzzfpwqzuczahqrysq4tolfj" lang="en-US" class="invert" data-marpit-pagination="3" style="--paginate:true;--class:invert;--heading-divider:5;--theme:fa5agneqe2xxbat58gzzfpwqzuczahqrysq4tolfj;" data-marpit-pagination-total="10"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="3" data-marpit-fragments="2" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="dil5c569ey7e10odkg3vpex7c4mrerdir64rpyfscbk" lang="en-US" class="invert" data-marpit-pagination="3" style="--paginate:true;--class:invert;--heading-divider:5;--theme:dil5c569ey7e10odkg3vpex7c4mrerdir64rpyfscbk;" data-marpit-pagination-total="10">
<h3 id="non-static-members">Non-static members</h3> <h3 id="non-static-members">Non-static members</h3>
<ul> <ul>
<li data-marpit-fragment="1">So far, we have used <em><strong>non-static</strong></em> fields in our classes</li> <li data-marpit-fragment="1">So far, we have used <em><strong>non-static</strong></em> fields in our classes</li>
@ -41,7 +41,7 @@
</li> </li>
</ul> </ul>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="4" data-marpit-fragments="1" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="fa5agneqe2xxbat58gzzfpwqzuczahqrysq4tolfj" lang="en-US" class="invert" data-marpit-pagination="4" style="--paginate:true;--class:invert;--heading-divider:5;--theme:fa5agneqe2xxbat58gzzfpwqzuczahqrysq4tolfj;" data-marpit-pagination-total="10"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="4" data-marpit-fragments="1" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="dil5c569ey7e10odkg3vpex7c4mrerdir64rpyfscbk" lang="en-US" class="invert" data-marpit-pagination="4" style="--paginate:true;--class:invert;--heading-divider:5;--theme:dil5c569ey7e10odkg3vpex7c4mrerdir64rpyfscbk;" data-marpit-pagination-total="10">
<ul> <ul>
<li data-marpit-fragment="1">Likewise, non-static class methods <strong><em>have to</em></strong> be called through an instance:<pre is="marp-pre" data-auto-scaling="downscale-only"><code class="language-csharp"><span class="hljs-keyword">class</span> <span class="hljs-title">MyAwesomeClass</span> <li data-marpit-fragment="1">Likewise, non-static class methods <strong><em>have to</em></strong> be called through an instance:<pre is="marp-pre" data-auto-scaling="downscale-only"><code class="language-csharp"><span class="hljs-keyword">class</span> <span class="hljs-title">MyAwesomeClass</span>
{ {
@ -66,7 +66,7 @@
<p><img src="imgs/10%20Static%20Members%2C%20Methods%20and%20Classes_1.png" alt="" style="width:500px;" /></p> <p><img src="imgs/10%20Static%20Members%2C%20Methods%20and%20Classes_1.png" alt="" style="width:500px;" /></p>
</div> </div>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="5" data-marpit-fragments="2" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="fa5agneqe2xxbat58gzzfpwqzuczahqrysq4tolfj" lang="en-US" class="invert" data-marpit-pagination="5" style="--paginate:true;--class:invert;--heading-divider:5;--theme:fa5agneqe2xxbat58gzzfpwqzuczahqrysq4tolfj;" data-marpit-pagination-total="10"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="5" data-marpit-fragments="2" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="dil5c569ey7e10odkg3vpex7c4mrerdir64rpyfscbk" lang="en-US" class="invert" data-marpit-pagination="5" style="--paginate:true;--class:invert;--heading-divider:5;--theme:dil5c569ey7e10odkg3vpex7c4mrerdir64rpyfscbk;" data-marpit-pagination-total="10">
<h3 id="static-members-1">Static members</h3> <h3 id="static-members-1">Static members</h3>
<ul> <ul>
<li data-marpit-fragment="1">Let's declare a property <code>MyProperty</code> with the <code>static</code> keyword</li> <li data-marpit-fragment="1">Let's declare a property <code>MyProperty</code> with the <code>static</code> keyword</li>
@ -90,7 +90,7 @@
<p><img src="imgs/10%20Static%20Members%2C%20Methods%20and%20Classes_2.png" alt="" style="width:600px;" /></p> <p><img src="imgs/10%20Static%20Members%2C%20Methods%20and%20Classes_2.png" alt="" style="width:600px;" /></p>
</div> </div>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="6" data-marpit-fragments="1" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="fa5agneqe2xxbat58gzzfpwqzuczahqrysq4tolfj" lang="en-US" class="invert" data-marpit-pagination="6" style="--paginate:true;--class:invert;--heading-divider:5;--theme:fa5agneqe2xxbat58gzzfpwqzuczahqrysq4tolfj;" data-marpit-pagination-total="10"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="6" data-marpit-fragments="1" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="dil5c569ey7e10odkg3vpex7c4mrerdir64rpyfscbk" lang="en-US" class="invert" data-marpit-pagination="6" style="--paginate:true;--class:invert;--heading-divider:5;--theme:dil5c569ey7e10odkg3vpex7c4mrerdir64rpyfscbk;" data-marpit-pagination-total="10">
<h3 id="static-members-an-example">Static members: An example</h3> <h3 id="static-members-an-example">Static members: An example</h3>
<ul> <ul>
<li data-marpit-fragment="1">In this example, a static field is used for keeping count on how many times the class has been instantiated:<pre is="marp-pre" data-auto-scaling="downscale-only"><code class="language-csharp"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span> <li data-marpit-fragment="1">In this example, a static field is used for keeping count on how many times the class has been instantiated:<pre is="marp-pre" data-auto-scaling="downscale-only"><code class="language-csharp"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span>
@ -112,7 +112,7 @@
</li> </li>
</ul> </ul>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="7" data-marpit-fragments="1" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="fa5agneqe2xxbat58gzzfpwqzuczahqrysq4tolfj" lang="en-US" class="invert" data-marpit-pagination="7" style="--paginate:true;--class:invert;--heading-divider:5;--theme:fa5agneqe2xxbat58gzzfpwqzuczahqrysq4tolfj;" data-marpit-pagination-total="10"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="7" data-marpit-fragments="1" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="dil5c569ey7e10odkg3vpex7c4mrerdir64rpyfscbk" lang="en-US" class="invert" data-marpit-pagination="7" style="--paginate:true;--class:invert;--heading-divider:5;--theme:dil5c569ey7e10odkg3vpex7c4mrerdir64rpyfscbk;" data-marpit-pagination-total="10">
<ul> <ul>
<li data-marpit-fragment="1">Now let's instantiate a couple of persons and print their info:</li> <li data-marpit-fragment="1">Now let's instantiate a couple of persons and print their info:</li>
</ul> </ul>
@ -135,10 +135,10 @@
</div> </div>
</div> </div>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="8" data-marpit-fragments="2" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="fa5agneqe2xxbat58gzzfpwqzuczahqrysq4tolfj" lang="en-US" class="invert" data-marpit-pagination="8" style="--paginate:true;--class:invert;--heading-divider:5;--theme:fa5agneqe2xxbat58gzzfpwqzuczahqrysq4tolfj;" data-marpit-pagination-total="10"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="8" data-marpit-fragments="2" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="dil5c569ey7e10odkg3vpex7c4mrerdir64rpyfscbk" lang="en-US" class="invert" data-marpit-pagination="8" style="--paginate:true;--class:invert;--heading-divider:5;--theme:dil5c569ey7e10odkg3vpex7c4mrerdir64rpyfscbk;" data-marpit-pagination-total="10">
<h2 id="static-methods">Static methods</h2> <h2 id="static-methods">Static methods</h2>
<ul> <ul>
<li data-marpit-fragment="1">Methods can also be static</li> <li data-marpit-fragment="1">Methods can also be static — for example, <code>Console.WriteLine</code></li>
<li data-marpit-fragment="2">What happens when you try to call a non-static method from a static method?<pre is="marp-pre" data-auto-scaling="downscale-only"><code class="language-csharp"><span class="hljs-keyword">class</span> <span class="hljs-title">Program</span> <li data-marpit-fragment="2">What happens when you try to call a non-static method from a static method?<pre is="marp-pre" data-auto-scaling="downscale-only"><code class="language-csharp"><span class="hljs-keyword">class</span> <span class="hljs-title">Program</span>
{ {
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">PrintHelloName</span>(<span class="hljs-params"><span class="hljs-built_in">string</span> name</span>)</span> <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">PrintHelloName</span>(<span class="hljs-params"><span class="hljs-built_in">string</span> name</span>)</span>
@ -157,7 +157,7 @@
<p><img src="imgs/10%20Static%20Members%2C%20Methods%20and%20Classes_4.png" alt="" style="width:800px;" /></p> <p><img src="imgs/10%20Static%20Members%2C%20Methods%20and%20Classes_4.png" alt="" style="width:800px;" /></p>
</div> </div>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="9" data-marpit-fragments="2" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="fa5agneqe2xxbat58gzzfpwqzuczahqrysq4tolfj" lang="en-US" class="invert" data-marpit-pagination="9" style="--paginate:true;--class:invert;--heading-divider:5;--theme:fa5agneqe2xxbat58gzzfpwqzuczahqrysq4tolfj;" data-marpit-pagination-total="10"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="9" data-marpit-fragments="2" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="dil5c569ey7e10odkg3vpex7c4mrerdir64rpyfscbk" lang="en-US" class="invert" data-marpit-pagination="9" style="--paginate:true;--class:invert;--heading-divider:5;--theme:dil5c569ey7e10odkg3vpex7c4mrerdir64rpyfscbk;" data-marpit-pagination-total="10">
<h2 id="static-classes">Static classes</h2> <h2 id="static-classes">Static classes</h2>
<ul> <ul>
<li data-marpit-fragment="1">Whole classes can also be static</li> <li data-marpit-fragment="1">Whole classes can also be static</li>
@ -180,7 +180,7 @@
</li> </li>
</ul> </ul>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="10" data-marpit-fragments="5" data-paginate="true" data-class="exercise invert" data-heading-divider="5" data-theme="fa5agneqe2xxbat58gzzfpwqzuczahqrysq4tolfj" lang="en-US" class="exercise invert" data-marpit-pagination="10" style="--paginate:true;--class:exercise invert;--heading-divider:5;--theme:fa5agneqe2xxbat58gzzfpwqzuczahqrysq4tolfj;" data-marpit-pagination-total="10"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="10" data-marpit-fragments="5" data-paginate="true" data-class="exercise invert" data-heading-divider="5" data-theme="dil5c569ey7e10odkg3vpex7c4mrerdir64rpyfscbk" lang="en-US" class="exercise invert" data-marpit-pagination="10" style="--paginate:true;--class:exercise invert;--heading-divider:5;--theme:dil5c569ey7e10odkg3vpex7c4mrerdir64rpyfscbk;" data-marpit-pagination-total="10">
<h2 id="exercise-1">Exercise 1</h2> <h2 id="exercise-1">Exercise 1</h2>
<ul> <ul>

@ -11,7 +11,7 @@ title: 10. Static Members, Methods and Classes
<!-- headingDivider: 5 --> <!-- headingDivider: 5 -->
<!-- class: invert --> <!-- class: invert -->
## Static Members ## Static members
### Non-static members ### Non-static members
@ -140,7 +140,7 @@ class Program
## Static methods ## Static methods
* Methods can also be static * Methods can also be static — for example, `Console.WriteLine`
* What happens when you try to call a non-static method from a static method? * What happens when you try to call a non-static method from a static method?
```csharp ```csharp
class Program class Program

File diff suppressed because one or more lines are too long

@ -1,270 +1,320 @@
# Methods ---
marp: true
![](imgs/6%20Methods_0.png) paginate: true
math: mathjax
theme: buutti
title: 6. Methods
--- ---
# Overview # Methods
Functions <!-- headingDivider: 5 -->
<!-- class: invert -->
Methods ## Overview
Parameters * Functions
* Methods
* Parameters
* Return values
* Scope of methods
Return Values ## What is a function?
Scope of Methods * Functions are self-contained modules of code that accomplish a specific task
* Basically, a named block of code that can be executed as many times as we want
* Instead of writing the same code over and over again, do it only once as a function, and ***call*** it many times
```csharp
void Example()
{
Console.WriteLine("Hello World");
}
# What is a function? Example(); // calling the function three times
Example();
Example();
```
* Functions are self contained modules of code that accomplish a specific task. ### Parameters and returns
* Function is basically a block of code that can be executed over and over again in the program
* Instead of writing the same code over and over again to the program, it can be written only once as a function
* Functions can get data, _parameters_ , from the _caller_
* Functions can return data, _return value_ , to the caller
* Defining parameters and return values are optional, not all functions have both and some have neither!
# Function execution * Functions can acquire data, a.k.a. **_parameters_**, from the **_caller_**
* Functions can return data, a.k.a. its **_return value_**, to the caller
* Defining parameters and return values are optional — not all functions have both, and some have neither!
* Here's a function that takes in one parameter and returns nothing (hence, `void`)
```csharp
void PrintName(string param)
{
Console.WriteLine("My name is " + param);
}
```
## Function execution
* When a function is called the program leaves the current section of the code and begins to execute the first line of code inside the function. When a function is called, the program leaves the current section of the code and begins to execute the first line of code inside the function.
* The program execution comes to the line of code where the function call is 1) The program execution comes to the line of code where the function call is
* The program enters the function 2) The program enters the function
* Instructions inside the function are executed from top to bottom 3) Instructions inside the function are executed from top to bottom
* Remember scopes: The variables defined inside the function are not visible outside the function! * Remember scopes: The variables defined inside the function are not visible outside the function!
* The program leaves the function and returns to the point where the function call was and continues the execution 4) The program leaves the function and returns to the point where the function call was and continues the execution
* If function returned any data, it can be used where the function was called 5) If the function returned any data, it can be used where the function was called
# Methods ### Methods vs functions
* In C#, most functions we see are *__methods__*: functions that are inside a ***class***
* Even the main function is inside a class in C#, so it is a method
* Classes are explained in [Lecture 7](7-classes-and-objects)
* Here's the earlier function as a method inside the `Program` class
```csharp
class Program
{
void PrintName(string param)
{
Console.WriteLine("My name is " + param);
}
}
```
In C#, __methods __ are functions which are inside of a class
Since the main function is inside of a class, all functions in C# are actually just called methods ### Calling methods
* A methods are executed only when it is __called__ * A method is executed only when it is *__called__*
* For example, Console.WriteLine() is a method which contains more complex code to print stuff on to the console * Some methods are already predefined, so we can call them right away
* Instead of writing all that complex stuff at every printout, you can just use the short method call Console.WriteLine() * For example, `Console.WriteLine()` is a method that contains code that prints stuff in the console
* Instead of writing ourselves all that complex stuff the method contains, we just use the short method call `Console.WriteLine()`
* The use of methods reduces repetitive code, adds modularity and makes designing your programs easier * The use of methods reduces repetitive code, adds modularity and makes designing your programs easier
# Methods - Example ### Methods: An example
![](imgs/6%20Methods_1.png) <div class='columns21' markdown='1'>
<div markdown='1'>
```csharp
class Program class Program
{ {
static void Main(string[] args)
{
void PrintBlock() void PrintBlock()
{ {
Console.WriteLine("---------------------"); Console.WriteLine("---------------------");
Console.WriteLine("| This is cool but |"); Console.WriteLine("| This is cool but |");
Console.WriteLine("| you wouldn't want |"); Console.WriteLine("| you wouldn't want |");
Console.WriteLine("| to write this |"); Console.WriteLine("| to write this |");
Console.WriteLine("| block of code |"); Console.WriteLine("| block of code |");
Console.WriteLine("| every time! |"); Console.WriteLine("| every time! |");
Console.WriteLine("---------------------"); Console.WriteLine("---------------------");
} }
static void Main(string[] args)
{
PrintBlock(); PrintBlock();
PrintBlock(); PrintBlock();
PrintBlock(); PrintBlock();
} }
} }
```
* In this example, we declared a new method called PrintBlock </div>
* Finally the method was called three times <div markdown='1'>
* Each time the entire code inside the method was executed
* The void statement means that the method does not return anything
* More on that later
# Parameters ![](imgs/6%20Methods_1.png)
Sometimes you want to pass data into the method when calling it </div>
</div>
This is done by adding __parameters __ to the method declaration, inside the brackets: ---
void PrintSum(int val1, int val2) * In the previous example, we declared a new method called `PrintBlock`
* Then, the method was called three times
* Each time the entire code inside the method was executed
* Why didn't we have to use the class name the method belonged to, unlike with `Console.WriteLine`?
* Short answer: Because we called the method inside the class
* The `void` statement means that the method does not return anything
* More on that later!
{ ## Parameters
* Sometimes you want to pass data into the method when calling it
* This is done by adding *__parameters__* to the method declaration inside the parentheses:
```csharp
void PrintSum(int val1, int val2)
{
int sum = val1 + val2; int sum = val1 + val2;
Console.WriteLine("Sum of " + val1 + " and " + val2 + " is " + sum); Console.WriteLine("Sum of " + val1 + " and " + val2 + " is " + sum);
} }
```
The values to be passed are set as __arguments __ in the method call, inside the brackets: * The values to be passed are set as ***arguments*** in the method call inside the parentheses:
```csharp
PrintSum(2, 5); // Outputs "Sum of 2 and 5 is 7 PrintSum(2, 5); // Outputs "Sum of 2 and 5 is 7
PrintSum(5, 5); // Outputs "Sum of 5 and 5 is 10 PrintSum(5, 5); // Outputs "Sum of 5 and 5 is 10
```
# Parameters - Example ### Parameters: An example
![](imgs/6%20Methods_2.png) <div class='columns21' markdown='1'>
<div markdown='1'>
```csharp
static void Main(string[] args) static void Main(string[] args)
{ {
int centuriesPrinted = 0; int centuriesPrinted = 0;
void PrintCentury(int century) void PrintCentury(int century)
{ {
centuriesPrinted++; centuriesPrinted++;
Console.WriteLine
Console.WriteLine("Current century: " + century); ("Current century: " + century);
Console.WriteLine
Console.WriteLine("Total centuries printed: " + centuriesPrinted); ("Total centuries printed: " + centuriesPrinted);
} }
for (int i = 0; i < 500; i += 100) for (int i = 0; i < 500; i += 100)
{ {
PrintCentury(i); PrintCentury(i);
} }
} }
```
</div>
<div markdown='1'>
# Optional Parameters ![](imgs/6%20Methods_2.png)
You can give a parameter a default value by assigning it in the method declaration Note that this method modifies a value that is ***outside*** the method, which is considered bad practice.
This makes the parameter optional </div>
</div>
double CircleArea(double radius = 0) ## Optional parameters
* You can give a parameter a default value by assigning it in the method declaration
* This makes the parameter optional
```csharp
double CircleArea(double radius = 0)
{ {
double area = Math.PI * radius * radius; double area = Math.PI * radius * radius;
return area; return area;
} }
Console.WriteLine(CircleArea()); // This outputs 0 Console.WriteLine(CircleArea()); // This outputs 0
Console.WriteLine(CircleArea(2)); // This outputs 12,56637... Console.WriteLine(CircleArea(2)); // This outputs 12,56637...
```
# Exercise 1 ## Exercise 1
<!--_class: "exercise invert" -->
Write two methods Sum and Difference, which both have two parameters of type int, named value1 and value2. Sum should print "The sum of __*value1*__ and __*value2*__ is __*sum*__ ", and Difference should print "The difference of __*value1*__ and __*value2*__ is __*difference*__ .
Call the methods from the main method multiple times with different values.
# Return Values 1) Write two methods `Sum` and `Difference` that both take in two parameters of type `int`, `value1` and `value2`.
* `Sum` should print `The sum of <value1> and <value2> is <sum>`
The return value of a method is __returned __ with the return keyword: * `Difference` should print `The difference of <value1> and <value2> is <difference>`
2) Call the methods from the main method multiple times with different arguments.
double radius = 2;
Console.WriteLine(CircleArea(radius)); // Outputs 12,56637... ## Return Values
* The return value of a method is *__returned__* with the `return` keyword:
```csharp
double CircleArea(double radius) double CircleArea(double radius)
{ {
double area = Math.PI * radius * radius; double area = Math.PI * radius * radius;
return area; return area;
} }
In the first examples, the method declarations started with the void expression, which means that the method doesn't return anything double radius = 2;
double result = CircleArea(radius);
Here the method was declared as type double, which means that the method has to return a variable of type double Console.WriteLine(result); // Outputs 12,56637...
```
* In the first examples, the method declarations started with the `void` keyword, which means that the method doesn't return anything
* Here the method was declared as type `double` — now the method ***must*** return a `double` type variable
# Multiple Return Values ### Note about shortening
It is possible to return multiple values in the form of a __tuple__ : * The earlier code
```csharp
double radius = 2;
double result = CircleArea(radius);
Console.WriteLine(result);
```
* can be shortened to
```csharp
double result = CircleArea(2);
Console.WriteLine(result);
```
* and even further into
```csharp
Console.WriteLine(CircleArea(2));
```
## Using tuples for multiple return values
* It is possible to return multiple values in the form of a *__tuple__*:
```csharp
(string, string) FirstAndLastName(string fullname) (string, string) FirstAndLastName(string fullname)
{ {
string[] names = fullname.Split(' '); string[] names = fullname.Split(' ');
return (names[0], names[names.Length - 1]); return (names[0], names[names.Length - 1]);
} }
(string first, string last) = FirstAndLastName("Hans \\"Dolph\\" Lundgren"); (string first, string last) = FirstAndLastName("Hans \"Dolph\" Lundgren");
Console.WriteLine($"First name: {first}, last name: {last}"); Console.WriteLine($"First name: {first}, last name: {last}");
// Outputs "First name: Hans, last name: Lundgren" // Outputs "First name: Hans, last name: Lundgren"
```
* ***Note:*** If only one value from a tuple is needed, you can use a *__discard__* (`_`)
```csharp
// Only the last name is allocated to a variable
(_, string lastName) = FirstAndLastName("Arnold Alois Schwarzenegger");
```
If only one value from a tuple is needed, you can use a __discard __ (\_) ## Scope of methods
// Only last name is allocated to a variable
(\_, string lastName) = FirstAndLastName("Arnold Alois Schwarzenegger");
# Scope of Methods
The variables declared inside of a method are __local variables__ and only accessible inside of that method
* The variables declared inside a method are *__local variables__* and only accessible inside the method
```csharp
void ScopeExample() void ScopeExample()
{ {
// Variable 'a' is only accessible inside of this method // Variable 'a' is only accessible inside of this method
int a = 10; int a = 10;
a = 20; // This works a = 20; // This works
} }
a = 30; // This throws an error a = 30; // This throws an error
```
# Exercise 2 * Contrastingly, variables declared outside the method are available inside, as seen in an earlier [example](#parameters-an-example)
Write a method that takes a string as a parameter and prints the total number of spaces in that string. ## Exercise 2
<!--_class: "exercise invert" -->
Without using the string.Trim() method, modify the method so that it removes the spaces in a string and returns the new string. 1) Write a method that takes a string as a parameter and prints the total number of spaces in that string.
2) Without using the `string.Trim()` method, modify the method so that it removes the spaces in a string and returns a new string.
(Hint: You can iterate a string just like arrays and lists.) ***Hint:*** You can iterate a string just like arrays and lists.
![](imgs/6%20Methods_3.png) ![w:900](imgs/6%20Methods_3.png)
# Exercise 3 ## Exercise 3
<!--_class: "exercise invert" -->
Write a method that takes a string as a parameter and prints each unique letter in that string. <div class='columns' markdown='1'>
<div markdown='1'>
Write a new method that takes a string as a parameter and prints the number of each unique letter in the string. 1) Write a method that takes in a string as a parameter and prints each unique letter in that string.
2) Write a new method that takes a string as a parameter and prints the number of each unique letter in the string.
![](imgs/6%20Methods_4.png) </div>
<div markdown='1'>
# Exercise 4 ![w:400](imgs/6%20Methods_4.png)
Write a method that takes a 10 digit number as a parameter and divides individual digits into even and odd arrays. Method should return both arrays. </div>
</div>
Remember to check that method accepts only numbers. ## Exercise 4
<!--_class: "exercise invert" -->
# Assignments * Write a method that takes in a 10-digit number as a parameter and divides individual digits into two arrays of even and odd digits. The method should return both arrays.
* Remember to check that method only accepts numbers!
[Assignments about this topic can be found here](https://gitea.buutti.com/education/academy-assignments/src/branch/master/C%23%20Basics/6.%20Methods) ## Exercise 5
<!--_class: "exercise invert" -->
* Fix this [example](#parameters-an-example) that utilized a bad practice where a variable declared outside the function is modified inside the function.
* So instead, take in the `centuriesPrinted` variable as another parameter, modify it in the function, and then return it.
## Assignments
[Assignments about this topic can be found here](https://gitea.buutti.com/education/academy-assignments/src/branch/master/C%23%20Basics/6.%20Methods)
Loading…
Cancel
Save