finish lecture 3, update lecture 7

main
borb 1 week ago
parent 05d1e4a420
commit 8c4a02784b

File diff suppressed because one or more lines are too long

@ -1,263 +1,302 @@
# Conditionals
![](imgs/3%20Conditionals_0.png)
--- ---
marp: true
# Overview paginate: true
math: mathjax
Conditionals theme: buutti
title: 3. Conditionals
if and else ---
Logical Operators
# Conditionals # Conditionals
Less than: a < b <!-- headingDivider: 5 -->
<!-- class: invert -->
Less than or equal to: a <= b
## Overview
Greater than: a > b
* Conditionals
Greater than or equal to: a >= b * `if` and `else`
* Logical Operators
Equal to a == b
## Comparison operators
Not Equal to: a != b
* ***Comparison operators*** are used to compare two variables
* They return either `true` or `false`
* Two variables of any type can be compared with equality operators
* Equal to: `a == b`
* (Do not mix with the assignment operator, see [Lecture 2](2-variables-and-types#assignment-operators)!)
* Not equal to: `a != b`
* Two ***numbers*** can be further compared with less than/greater than operators:
* Less than: `a < b`
* Less than or equal: `a <= b`
* Greater than: `a > b`
* Greater than or equal: `a >= b`
<!-- | Comparison | Syntax |
|:-------------------------|:---------|
| Less than | `a < b` |
| Less than or equal to | `a <= b` |
| Greater than | `a > b` |
| Greater than or equal to | `a >= b` |
| Equal to | `a == b` |
| Not equal to | `a != b` | -->
### `bool` data type
* As shown in [Lecture 2](2-variables-and-types#primitive-data-types), `bool` is a data type for storing truth values `true` or `false`
* Because conditionals return `true` or `false`, the result can be stored in a variable
```csharp
int a = 4;
int b = 3;
bool areEqual = a == b; // outputs False
bool biggerOrNot = a > b; // outputs True
```
* Useful for making multiple comparisons at once
## `if`, `else if` and `else` statements
<div class='columns21' markdown='1'>
<div markdown='1'>
* `if` checks truthfulness of a given statement
* If it results in `false`, we can check if another condition is met with `else if`
* Multiple `else if` can be chained indefinitely
* If no statement returns `true`, the `else` block is executed
* The statements are checked in order, and the first `true` condition is executed
* No other block is executed
* (You can also have just `if` and `else` without the `else if`, or just a single `if`.)
</div>
<div markdown='1'>
```csharp
int a = 2
if (a > 4)
{
// do something
}
else if (a < 2)
{
// do something else
}
else if (a < 3)
{
// do something else
}
else
{
// a is 4??
}
```
Conditionals return true or false, meaning the result can even be allocated into a variable if needed </div>
</div>
bool areEqual = (a == b);
# if , else and else if statements ### Conditionals: An example
```csharp
double temperatureInOulu = 2.3; double temperatureInOulu = 2.3;
double temperatureInIvalo = -10.9; double temperatureInIvalo = -10.9;
if (temperatureInOulu > temperatureInIvalo) if (temperatureInOulu > temperatureInIvalo)
{ {
Console.WriteLine("Oulu is warmer than Ivalo");
Console.WriteLine("Oulu is warmer than Ivalo");
} }
else if (temperatureInOulu < temperatureInIvalo) else if (temperatureInOulu < temperatureInIvalo)
{ {
Console.WriteLine("Ivalo is warmer than Oulu");
Console.WriteLine("Ivalo is warmer than Oulu");
} }
else else
{ {
Console.WriteLine
Console.WriteLine ("Oulu and Ivalo have the same temperature");
("Oulu and Ivalo have the same temperature");
} }
```
__if __ statements are executed in order This outputs `Oulu is warmer than Ivalo`.
The first statement with a __true __ condition is executed
If no statement holds a true value, __else __ is executed
This outputs "Oulu is warmer than Ivalo" ## Not operator `!`
# ! -Operator
Console.WriteLine(!true); // Outputs false
* The `!` ("not") operator flips the boolean value.
```csharp
Console.WriteLine(!true); // Outputs false
bool itsColdOutside = true; bool itsColdOutside = true;
if (!itsColdOutside) // same as checking if (itsColdOutside == false)
if(!itsColdOutside)
{ {
Console.WriteLine("It's warm outside.");
Console.WriteLine("It's warm outside.");
} }
```
The ! -operator flips the boolean value __ __ ## The `switch` statement
# The switch statement <div class='columns' markdown='1'>
<div markdown='1'>
The __switch __ statement compares the parameter value (here: the __path __ variable) with the value of each __case__ * The `switch` statement compares the given expression (in this example, the `path` variable) with the value of each `case`
* Only the matching code is executed
* If no match is found, the default code block is executed
* This example outputs `Here's the catalogue!`
* `break` ends each case (the code will not compile if omitted!)
Only the matching code is executed </div>
<div markdown='1'>
If no match is found, the default code block is executed
This outputs "Here's the catalogue!"
break ends the case and exits the switch: the code will not compile if omitted
```csharp
string path = "/browse"; string path = "/browse";
switch (path) switch (path)
{ {
case "/browse":
case "/browse": Console.WriteLine("Here's the catalogue!");
break;
Console.WriteLine("Here's the catalogue!"); case "/contact":
Console.WriteLine("Here's our contact info.");
break; break;
default:
case "/contact": Console.WriteLine("Given path doesn't exist!");
break;
Console.WriteLine("Here's our contact info.");
break;
default:
Console.WriteLine("No such path!");
break;
} }
```
# Scope of Variables </div>
</div>
Variables declared inside of blocks are called __local variables__ ; they are only accessible inside of that block.
int a = 0; ## Scope of Variables
if(a < 10) * Variables declared inside of blocks `{}` are ***local*** to that ***scope***; they are only accessible inside of that block
```csharp
if (true)
{
int b = 1; // b is only accessible in this block
b = 2; // This works
}
{ b = 3; // This throws an error
```
* Similarly, classes are only defined inside their `namespace`, and have to be imported to be accessible elsewhere
* ***Note:*** Some languages have `global` variables that are accessible everywhere — as an object-oriented language, C# doesn't have such feature.
// Variable 'b' is only accessible inside of this if block ## Logical Operators
int b = 1; * `&&`, `||` and `!` are the logical AND, OR and NOT operators
* These are useful when writing complex `if` statements
b = 2; // This works <div class='columns' markdown='1'>
<div markdown='1'>
```csharp
int a = 1;
int b = 3;
int c = 5;
if (a < 10)
{
if (b < 10)
{
if (c < 10)
{
Console.WriteLine
("All are smaller than 10!")
}
}
} }
```
b = 3; // This throws an error </div>
<div markdown='1'>
# Assignment Operators
We have used the assignment operator ' __='__ for assigning values for variables:
int x;
x = 25;
__Notice __ the difference between '=' and the conditional '=='!
'=' is used for assigning values for variables, '== is used for comparing values
# Logical Operators
'&&', '||' and '!' mean the logical AND, OR and NOT operators
For example,
```csharp
int a = 1; int a = 1;
int b = 3; int b = 3;
int c = 5; int c = 5;
if (a < 10 && b < 10 && c < 10)
{
Console.WriteLine
("All are smaller than 10!")
}
```
* Same functionality achieved in fewer lines!
Console.WriteLine(a < b && a < c); </div>
</div>
outputs "True" ### Common logical operators
| Operator | Name | Example | Description | | Operator | Name | Usage | Description |
| :-: | :-: | :-: | :-: | |:---------|:-----|:-----------|:-----------------------------------------------------------------------------------------|
| && | AND | a && b | Returns true if __both__ variables are true.b is not evaluated if a == false. | | `&&` | AND | `a && b` | Returns `true` if *__both__* variables are true.<br>`b` is not evaluated if `a == false`. |
| || | OR | a || b | Returns true if __one or both__ variables are true. b is not evaluated if a == true. | | `\|\|` | OR | `a \|\| b` | Returns `true` if *__one or both__* variables are true.<br>`b` is not evaluated if `a == true`. |
| ! | NOT | !a | __Negates__ boolean value (true becomes false and vice versa) | | `!` | NOT | `!a` | Negates the boolean value.<br>(`true` becomes `false` and vice versa). |
| ^ | XOR | a ^ b | Exclusive OR: returns true if __only __ a == true __OR only__ b == true. |
| & | Logical AND | a & b | Returns true if __both__ variables are true. Both variables are always evaluated. |
| | | Logical OR | a | b | Returns true if __one or both__ variables are true.Both variables are always evaluated. |
--- ### Less common logical operators
Tässä on kaikki aritmeettiset operaattorit | Operator | Name | Usage | Description |
|:---------|:------------|:---------|:-----------------------------------------------------------------------------------------|
| `^` | XOR | `a ^ b` &nbsp; | Exclusive OR ("*joko tai*"): returns `true` if *__only either of__* `a` or `b` are `true`, but not both! |
| `&` | Logical AND | `a & b` | Returns `true` if __both__ variables are `true`.<br>Both variables are always evaluated. |
| `\|` | Logical OR | `a \| b` | Returns `true` if __one or both__ variables are `true`.<br>Both variables are always evaluated. |
# Operators - Example ### Logical operators: An Example
This outputs "Strong password!"
```csharp
int uppercaseLetters = 2; int uppercaseLetters = 2;
uppercaseLetters += 4; // is now 6 uppercaseLetters += 4; // is now 6
int specialCharacters = 2; int specialCharacters = 2;
specialCharacters *= 2; // is now 4 specialCharacters *= 2; // is now 4
if (uppercaseLetters >= 6 && specialCharacters >= 2) if (uppercaseLetters >= 6 && specialCharacters >= 2)
{ {
Console.WriteLine("Strong password!");
Console.WriteLine("Strong password!");
} }
else else
{ {
Console.WriteLine("Weak password...");
Console.WriteLine("Weak-ass password...");
} }
```
This outputs `Strong password!`
# Exercise 1: ## Extra: Single statement `if`
<!-- _class: "extra invert" -->
Create a console application which asks the user which weekday it is and assigns the answer to a string variable. * If a code block following a statement only has one line of code, it is possible to write the block ***without*** curly brackets:
```csharp
int baa = 49;
if (baa > 20)
Console.WriteLine("Baa");
else
Console.WriteLine("Not baa!");
if (baa > 20) Console.WriteLine("Baa");
else Console.WriteLine("Not baa!");
```
* You may encounter code like this — however, we highly recommend to refrain from using this syntax as it is highly prone to [errors](https://www.leadingagile.com/2018/01/the-goto-fail-bug-as-a-coaching-tool/).
* Can you guess how this syntax can lead to bugs?
Using a switch-case, calculate the days remaining until next Monday. ## Exercise 1
<!--_class: "exercise invert" -->
If the result is more than 3, print "Have a nice week!". Otherwise, print "Have a nice weekend!".
# Exercise 2:
Create a console application which lets the user input a note.
If the length of the note is less than 30, the program prints the current time and the note, separated with a tab. Otherwise, the date and the note are printed to a separate line.
Tip: Use DateTime.Now.ToString() for current time. Use .Length after your message variable to get the length of the message.
# One More Thing...
If a code block following a statement only has one line of code, you can write the code without using curly brackets:
int baa = 49;
if (baa > 20)
Console.WriteLine("Baa");
else
Console.WriteLine("Not baa!"); 1) Create a console application that asks the user which weekday it is and assigns the answer to a string variable.
2) Using a switch-case expression, calculate the days remaining until next Monday.
3) If the result is more than 3, print `Have a nice week!`. Otherwise, print `Have a nice weekend!`.
if (baa > 20) Console.WriteLine("Baa"); ## Exercise 2
<!--_class: "exercise invert" -->
else Console.WriteLine("Not baa!"); 1) Create a console application that lets the user input a note as a string.
2) If the length of the note is less than 30, the program prints the current time and the note separated by a tab. Otherwise, the date and the note are printed to a separate line.
You may see code where this is done. However, we highly recommend you not to use this syntax as it is highly prone to [errors](https://www.leadingagile.com/2018/01/the-goto-fail-bug-as-a-coaching-tool/) . ***Tip:*** Use `DateTime.Now.ToString()` for current time. Use `.Length` after your message variable to get the length of the message.
# Get Help
All the basics covering the syntax in C# are covered here: ## Reading
[https://www.tutorialspoint.com/csharp/index.](https://www.tutorialspoint.com/csharp/index.htm) [htm](https://www.tutorialspoint.com/csharp/index.htm) [l](https://www.tutorialspoint.com/csharp/index.htm) Basics covering the syntax in C# are covered here:
[https://www.w3schools.com/cs/default.asp](https://www.w3schools.com/cs/default.asp) * [Learn .NET: C# Documentation](https://learn.microsoft.com/en-us/dotnet/csharp/tour-of-csharp/)
* [Tutorialspoint: C# tutorial](https://www.tutorialspoint.com/csharp/index.htm)
* [W3Schools: C# tutorial](https://www.w3schools.com/cs/default.asp)
# Assignments ## Assignments
<!--_class: "exercise invert" -->
[Assignments about this topic can be found here](https://gitea.buutti.com/education/academy-assignments/src/branch/master/C%23%20Basics/3.%20Conditionals) [Assignments about this topic can be found here](https://gitea.buutti.com/education/academy-assignments/src/branch/master/C%23%20Basics/3.%20Conditionals)

@ -13,10 +13,10 @@
/* 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 ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe */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="ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe" lang="en-US" class="invert" data-marpit-pagination="1" style="--paginate:true;--class:invert;--heading-divider:5;--theme:ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe;" data-marpit-pagination-total="25"> /* @theme 9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm */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="9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm" lang="en-US" class="invert" data-marpit-pagination="1" style="--paginate:true;--class:invert;--heading-divider:5;--theme:9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm;" data-marpit-pagination-total="25">
<h1 id="classes-and-objects">Classes And Objects</h1> <h1 id="classes-and-objects">Classes And Objects</h1>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="2" data-marpit-fragments="7" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe" lang="en-US" class="invert" data-marpit-pagination="2" style="--paginate:true;--class:invert;--heading-divider:5;--theme:ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe;" data-marpit-pagination-total="25"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="2" data-marpit-fragments="7" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm" lang="en-US" class="invert" data-marpit-pagination="2" style="--paginate:true;--class:invert;--heading-divider:5;--theme:9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm;" data-marpit-pagination-total="25">
<h2 id="overview">Overview</h2> <h2 id="overview">Overview</h2>
<ul> <ul>
<li data-marpit-fragment="1">Namespaces</li> <li data-marpit-fragment="1">Namespaces</li>
@ -28,7 +28,7 @@
<li data-marpit-fragment="7">Value &amp; Reference Types</li> <li data-marpit-fragment="7">Value &amp; Reference Types</li>
</ul> </ul>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="3" data-marpit-fragments="5" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe" lang="en-US" class="invert" data-marpit-pagination="3" style="--paginate:true;--class:invert;--heading-divider:5;--theme:ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe;" data-marpit-pagination-total="25"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="3" data-marpit-fragments="5" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm" lang="en-US" class="invert" data-marpit-pagination="3" style="--paginate:true;--class:invert;--heading-divider:5;--theme:9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm;" data-marpit-pagination-total="25">
<h2 id="namespaces">Namespaces</h2> <h2 id="namespaces">Namespaces</h2>
<ul> <ul>
<li data-marpit-fragment="1">Using namespaces helps you to organize the scope of your project, so that... <li data-marpit-fragment="1">Using namespaces helps you to organize the scope of your project, so that...
@ -44,7 +44,7 @@
<li data-marpit-fragment="5">By default, when creating a new project and adding classes in Visual Studio, everything is contained within a namespace named after your project name</li> <li data-marpit-fragment="5">By default, when creating a new project and adding classes in Visual Studio, everything is contained within a namespace named after your project name</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="2" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe" lang="en-US" class="invert" data-marpit-pagination="4" style="--paginate:true;--class:invert;--heading-divider:5;--theme:ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe;" data-marpit-pagination-total="25"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="4" data-marpit-fragments="2" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm" lang="en-US" class="invert" data-marpit-pagination="4" style="--paginate:true;--class:invert;--heading-divider:5;--theme:9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm;" data-marpit-pagination-total="25">
<ul> <ul>
<li data-marpit-fragment="1">If you need a class, interface, etc to be accessible from anywhere, create it within that namespace and set it as <code>public</code></li> <li data-marpit-fragment="1">If you need a class, interface, etc to be accessible from anywhere, create it within that namespace and set it as <code>public</code></li>
</ul> </ul>
@ -74,7 +74,7 @@
<li data-marpit-fragment="2">Wait, what's a class, though?</li> <li data-marpit-fragment="2">Wait, what's a class, though?</li>
</ul> </ul>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="5" data-marpit-fragments="4" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe" lang="en-US" class="invert" data-marpit-pagination="5" style="--paginate:true;--class:invert;--heading-divider:5;--theme:ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe;" data-marpit-pagination-total="25"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="5" data-marpit-fragments="4" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm" lang="en-US" class="invert" data-marpit-pagination="5" style="--paginate:true;--class:invert;--heading-divider:5;--theme:9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm;" data-marpit-pagination-total="25">
<h2 id="classes">Classes</h2> <h2 id="classes">Classes</h2>
<ul> <ul>
<li data-marpit-fragment="1"><a href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/classes">Classes</a> in C# are blueprints for specific kinds of collections of data &amp; functionality</li> <li data-marpit-fragment="1"><a href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/classes">Classes</a> in C# are blueprints for specific kinds of collections of data &amp; functionality</li>
@ -91,7 +91,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="6" data-marpit-fragments="6" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe" lang="en-US" class="invert" data-marpit-pagination="6" style="--paginate:true;--class:invert;--heading-divider:5;--theme:ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe;" data-marpit-pagination-total="25"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="6" data-marpit-fragments="6" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm" lang="en-US" class="invert" data-marpit-pagination="6" style="--paginate:true;--class:invert;--heading-divider:5;--theme:9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm;" data-marpit-pagination-total="25">
<h3 id="why-classes">Why classes?</h3> <h3 id="why-classes">Why classes?</h3>
<ul> <ul>
<li data-marpit-fragment="1">Classes are a core feature of the <em><strong>object-oriented programming</strong></em> paradigm popularized by Java in the 90s <li data-marpit-fragment="1">Classes are a core feature of the <em><strong>object-oriented programming</strong></em> paradigm popularized by Java in the 90s
@ -105,7 +105,7 @@
<li data-marpit-fragment="6">Shorter files can also mean that it's hard to find the actual functionality from hundreds of short files full of intermediate classes and class factories</li> <li data-marpit-fragment="6">Shorter files can also mean that it's hard to find the actual functionality from hundreds of short files full of intermediate classes and class factories</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="3" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe" lang="en-US" class="invert" data-marpit-pagination="7" style="--paginate:true;--class:invert;--heading-divider:5;--theme:ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe;" data-marpit-pagination-total="25"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="7" data-marpit-fragments="3" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm" lang="en-US" class="invert" data-marpit-pagination="7" style="--paginate:true;--class:invert;--heading-divider:5;--theme:9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm;" data-marpit-pagination-total="25">
<h3 id="creating-a-class">Creating a class</h3> <h3 id="creating-a-class">Creating a class</h3>
<ul> <ul>
<li data-marpit-fragment="1">You can create a new class by writing<pre is="marp-pre" data-auto-scaling="downscale-only"><code class="language-csharp"><span class="hljs-keyword">class</span> <span class="hljs-title">Student</span> <li data-marpit-fragment="1">You can create a new class by writing<pre is="marp-pre" data-auto-scaling="downscale-only"><code class="language-csharp"><span class="hljs-keyword">class</span> <span class="hljs-title">Student</span>
@ -124,7 +124,7 @@
<li data-marpit-fragment="3">These variables declared directly inside classes are called <em><strong>fields</strong></em>.</li> <li data-marpit-fragment="3">These variables declared directly inside classes are called <em><strong>fields</strong></em>.</li>
</ul> </ul>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="8" data-marpit-fragments="4" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe" lang="en-US" class="invert" data-marpit-pagination="8" style="--paginate:true;--class:invert;--heading-divider:5;--theme:ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe;" data-marpit-pagination-total="25"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="8" data-marpit-fragments="4" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm" lang="en-US" class="invert" data-marpit-pagination="8" style="--paginate:true;--class:invert;--heading-divider:5;--theme:9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm;" data-marpit-pagination-total="25">
<h3 id="instancing-a-class">Instancing a class</h3> <h3 id="instancing-a-class">Instancing a class</h3>
<div class='columns' markdown='1'> <div class='columns' markdown='1'>
<div markdown='1'> <div markdown='1'>
@ -157,7 +157,7 @@
</div> </div>
</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="ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe" lang="en-US" class="invert" data-marpit-pagination="9" style="--paginate:true;--class:invert;--heading-divider:5;--theme:ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe;" data-marpit-pagination-total="25"> </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="9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm" lang="en-US" class="invert" data-marpit-pagination="9" style="--paginate:true;--class:invert;--heading-divider:5;--theme:9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm;" data-marpit-pagination-total="25">
<h3 id="object-variables">Object variables</h3> <h3 id="object-variables">Object variables</h3>
<ul> <ul>
<li data-marpit-fragment="1">Variables inside of an object can be accessed with the <code>.</code> operator</li> <li data-marpit-fragment="1">Variables inside of an object can be accessed with the <code>.</code> operator</li>
@ -188,7 +188,7 @@ Student class:
</div> </div>
</div> </div>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="10" data-marpit-fragments="1" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe" lang="en-US" class="invert" data-marpit-pagination="10" style="--paginate:true;--class:invert;--heading-divider:5;--theme:ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe;" data-marpit-pagination-total="25"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="10" data-marpit-fragments="1" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm" lang="en-US" class="invert" data-marpit-pagination="10" style="--paginate:true;--class:invert;--heading-divider:5;--theme:9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm;" data-marpit-pagination-total="25">
<p>Let's fix the previous example by changing the <em><strong>access specifier</strong></em> of the variable <code>id</code> to <code>public</code>:</p> <p>Let's fix the previous example by changing the <em><strong>access specifier</strong></em> of the variable <code>id</code> to <code>public</code>:</p>
<div class='columns' markdown='1'> <div class='columns' markdown='1'>
<div markdown='1'> <div markdown='1'>
@ -215,34 +215,46 @@ Student class:
<li data-marpit-fragment="1">The value of the variable <code>id</code> of the object <code>student</code> is now <code>12345678</code></li> <li data-marpit-fragment="1">The value of the variable <code>id</code> of the object <code>student</code> is now <code>12345678</code></li>
</ul> </ul>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="11" data-marpit-fragments="4" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe" lang="en-US" class="invert" data-marpit-pagination="11" style="--paginate:true;--class:invert;--heading-divider:5;--theme:ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe;" data-marpit-pagination-total="25"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="11" data-marpit-fragments="7" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm" lang="en-US" class="invert" data-marpit-pagination="11" style="--paginate:true;--class:invert;--heading-divider:5;--theme:9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm;" data-marpit-pagination-total="25">
<h2 id="access-specifiers">Access Specifiers</h2> <h2 id="access-modifiers">Access modifiers</h2>
<div class='columns21' markdown='1'>
<div markdown='1'>
<ul> <ul>
<li data-marpit-fragment="1"><em><strong>Access specifiers</strong></em> can be used to get additional level of protection inside classes</li> <li data-marpit-fragment="1"><a href="https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers">Access modifiers</a> can be used to get additional level of protection inside classes</li>
<li data-marpit-fragment="2">Variables specified with <code>private</code> are accessible only inside the containing class <li data-marpit-fragment="2"><code>private</code>: accessible only inside the containing class
<ul> <ul>
<li data-marpit-fragment="3">This is the <em><strong>default</strong></em> access!</li> <li data-marpit-fragment="3">This is the default, but we can make it more explicit by writing it out</li>
</ul> </ul>
</li> </li>
<li data-marpit-fragment="4">Variables specified with <code>public</code> are accessible outside of the class<pre is="marp-pre" data-auto-scaling="downscale-only"><code class="language-csharp"><span class="hljs-keyword">class</span> <span class="hljs-title">Student</span> <li data-marpit-fragment="4"><code>public</code>: accessible everywhere in the namespace</li>
<li data-marpit-fragment="5">Less common, but good to know:
<ul>
<li data-marpit-fragment="6"><code>protected</code>: like <code>private</code>, but also accessible by the <em>inheritors</em> of the class</li>
<li data-marpit-fragment="7"><code>virtual</code>: accessible and <em>overridable</em> by inheritors</li>
</ul>
</li>
</ul>
</div>
<div markdown='1'>
<pre is="marp-pre" data-auto-scaling="downscale-only"><code class="language-csharp"><span class="hljs-keyword">class</span> <span class="hljs-title">Student</span>
{ {
<span class="hljs-built_in">int</span> id; <span class="hljs-comment">// Accessible only inside the class</span> <span class="hljs-built_in">int</span> id;
<span class="hljs-keyword">private</span> <span class="hljs-built_in">string</span> name; <span class="hljs-comment">// Accessible only inside the class</span> <span class="hljs-keyword">private</span> <span class="hljs-built_in">string</span> name;
<span class="hljs-keyword">public</span> <span class="hljs-built_in">string</span> address; <span class="hljs-comment">// Accessible everywhere within the namespace</span> <span class="hljs-keyword">public</span> <span class="hljs-built_in">string</span> address;
} }
</code></pre> </code></pre>
</li> </div>
</ul> </div>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="12" data-marpit-fragments="1" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe" lang="en-US" class="invert" data-marpit-pagination="12" style="--paginate:true;--class:invert;--heading-divider:5;--theme:ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe;" data-marpit-pagination-total="25"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="12" data-marpit-fragments="1" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm" lang="en-US" class="invert" data-marpit-pagination="12" style="--paginate:true;--class:invert;--heading-divider:5;--theme:9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm;" data-marpit-pagination-total="25">
<ul> <ul>
<li data-marpit-fragment="1"> <li data-marpit-fragment="1">
<p>Continuing on the class in the previous example: if we follow the <code>student</code> variable with a dot, Visual Studio IntelliSense will only suggest the <code>address</code> variable, because it was the only <code>public</code> variable of the <code>Student</code> class!</p> <p>Continuing on the class in the previous example: if we follow the <code>student</code> variable with <code>.</code>, Visual Studio IntelliSense will only suggest the <code>address</code> variable, because it was the only <code>public</code> variable of the <code>Student</code> class!</p>
<p><img src="imgs/7%20Classes%20and%20Objects_3.png" alt="" /></p> <p><img src="imgs/7%20Classes%20and%20Objects_3.png" alt="" /></p>
</li> </li>
</ul> </ul>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="13" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe" lang="en-US" class="invert" data-marpit-pagination="13" style="--paginate:true;--class:invert;--heading-divider:5;--theme:ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe;" data-marpit-pagination-total="25"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="13" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm" lang="en-US" class="invert" data-marpit-pagination="13" style="--paginate:true;--class:invert;--heading-divider:5;--theme:9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm;" data-marpit-pagination-total="25">
<h2 id="classes-an-example">Classes: An example</h2> <h2 id="classes-an-example">Classes: An example</h2>
<div class='columns12' markdown='1'> <div class='columns12' markdown='1'>
<div markdown='1'> <div markdown='1'>
@ -274,7 +286,7 @@ Student class:
</div> </div>
</div> </div>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="14" data-marpit-fragments="3" data-paginate="true" data-class="exercise invert" data-heading-divider="5" data-theme="ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe" lang="en-US" class="exercise invert" data-marpit-pagination="14" style="--paginate:true;--class:exercise invert;--heading-divider:5;--theme:ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe;" data-marpit-pagination-total="25"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="14" data-marpit-fragments="3" data-paginate="true" data-class="exercise invert" data-heading-divider="5" data-theme="9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm" lang="en-US" class="exercise invert" data-marpit-pagination="14" style="--paginate:true;--class:exercise invert;--heading-divider:5;--theme:9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm;" data-marpit-pagination-total="25">
<h2 id="exercise-1">Exercise 1</h2> <h2 id="exercise-1">Exercise 1</h2>
<ol> <ol>
@ -283,7 +295,7 @@ Student class:
<li data-marpit-fragment="3">Print all stored usernames every time a new user is created</li> <li data-marpit-fragment="3">Print all stored usernames every time a new user is created</li>
</ol> </ol>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="15" data-marpit-fragments="2" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe" lang="en-US" class="invert" data-marpit-pagination="15" style="--paginate:true;--class:invert;--heading-divider:5;--theme:ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe;" data-marpit-pagination-total="25"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="15" data-marpit-fragments="2" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm" lang="en-US" class="invert" data-marpit-pagination="15" style="--paginate:true;--class:invert;--heading-divider:5;--theme:9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm;" data-marpit-pagination-total="25">
<h2 id="class-methods">Class methods</h2> <h2 id="class-methods">Class methods</h2>
<ul> <ul>
<li data-marpit-fragment="1">As mentioned in <a href="6-methods">Lecture 6</a>, we can create methods inside of our classes:</li> <li data-marpit-fragment="1">As mentioned in <a href="6-methods">Lecture 6</a>, we can create methods inside of our classes:</li>
@ -320,7 +332,7 @@ Student class:
<li data-marpit-fragment="2">If the method is public, it can be called from outside of the class</li> <li data-marpit-fragment="2">If the method is public, it can be called from outside of the class</li>
</ul> </ul>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="16" data-marpit-fragments="3" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe" lang="en-US" class="invert" data-marpit-pagination="16" style="--paginate:true;--class:invert;--heading-divider:5;--theme:ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe;" data-marpit-pagination-total="25"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="16" data-marpit-fragments="3" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm" lang="en-US" class="invert" data-marpit-pagination="16" style="--paginate:true;--class:invert;--heading-divider:5;--theme:9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm;" data-marpit-pagination-total="25">
<h2 id="constructors">Constructors</h2> <h2 id="constructors">Constructors</h2>
<ul> <ul>
<li data-marpit-fragment="1">Constructors are class methods which are called once the object is initialized</li> <li data-marpit-fragment="1">Constructors are class methods which are called once the object is initialized</li>
@ -354,7 +366,7 @@ Student class:
<li data-marpit-fragment="3"><em><strong>Note:</strong></em> In Visual Studio, just write <code>ctor</code> and press tab twice to quickly create a constructor!</li> <li data-marpit-fragment="3"><em><strong>Note:</strong></em> In Visual Studio, just write <code>ctor</code> and press tab twice to quickly create a constructor!</li>
</ul> </ul>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="17" data-marpit-fragments="1" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe" lang="en-US" class="invert" data-marpit-pagination="17" style="--paginate:true;--class:invert;--heading-divider:5;--theme:ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe;" data-marpit-pagination-total="25"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="17" data-marpit-fragments="1" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm" lang="en-US" class="invert" data-marpit-pagination="17" style="--paginate:true;--class:invert;--heading-divider:5;--theme:9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm;" data-marpit-pagination-total="25">
<h2 id="constructors-with-parameters">Constructors with parameters</h2> <h2 id="constructors-with-parameters">Constructors with parameters</h2>
<ul> <ul>
<li data-marpit-fragment="1">You can pass in parameters to the constructor at initialization:</li> <li data-marpit-fragment="1">You can pass in parameters to the constructor at initialization:</li>
@ -390,7 +402,7 @@ Student class:
</div> </div>
</div> </div>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="18" data-marpit-fragments="5" data-paginate="true" data-class="exercise invert" data-heading-divider="5" data-theme="ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe" lang="en-US" class="exercise invert" data-marpit-pagination="18" style="--paginate:true;--class:exercise invert;--heading-divider:5;--theme:ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe;" data-marpit-pagination-total="25"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="18" data-marpit-fragments="5" data-paginate="true" data-class="exercise invert" data-heading-divider="5" data-theme="9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm" lang="en-US" class="exercise invert" data-marpit-pagination="18" style="--paginate:true;--class:exercise invert;--heading-divider:5;--theme:9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm;" data-marpit-pagination-total="25">
<h2 id="exercise-2">Exercise 2</h2> <h2 id="exercise-2">Exercise 2</h2>
<ol> <ol>
@ -404,7 +416,7 @@ Student class:
<li data-marpit-fragment="5">Call their greet methods from the main program</li> <li data-marpit-fragment="5">Call their greet methods from the main program</li>
</ol> </ol>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="19" data-marpit-fragments="4" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe" lang="en-US" class="invert" data-marpit-pagination="19" style="--paginate:true;--class:invert;--heading-divider:5;--theme:ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe;" data-marpit-pagination-total="25"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="19" data-marpit-fragments="4" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm" lang="en-US" class="invert" data-marpit-pagination="19" style="--paginate:true;--class:invert;--heading-divider:5;--theme:9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm;" data-marpit-pagination-total="25">
<h2 id="properties">Properties</h2> <h2 id="properties">Properties</h2>
<ul> <ul>
<li data-marpit-fragment="1">When working with C#, you will eventually see <a href="https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties">properties</a> being used at some point</li> <li data-marpit-fragment="1">When working with C#, you will eventually see <a href="https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties">properties</a> being used at some point</li>
@ -441,7 +453,7 @@ Student class:
} }
</code></pre> </code></pre>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="20" data-marpit-fragments="2" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe" lang="en-US" class="invert" data-marpit-pagination="20" style="--paginate:true;--class:invert;--heading-divider:5;--theme:ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe;" data-marpit-pagination-total="25"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="20" data-marpit-fragments="2" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm" lang="en-US" class="invert" data-marpit-pagination="20" style="--paginate:true;--class:invert;--heading-divider:5;--theme:9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm;" data-marpit-pagination-total="25">
<h3 id="auto-properties">Auto Properties</h3> <h3 id="auto-properties">Auto Properties</h3>
<ul> <ul>
<li data-marpit-fragment="1">Auto properties are a shorthand version of the same:</li> <li data-marpit-fragment="1">Auto properties are a shorthand version of the same:</li>
@ -472,7 +484,7 @@ Student class:
<li data-marpit-fragment="2"><em><strong>Note:</strong></em> In Visual Studio, just write <code>prop</code> and press tab twice to quickly create an auto property</li> <li data-marpit-fragment="2"><em><strong>Note:</strong></em> In Visual Studio, just write <code>prop</code> and press tab twice to quickly create an auto property</li>
</ul> </ul>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="21" data-marpit-fragments="7" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe" lang="en-US" class="invert" data-marpit-pagination="21" style="--paginate:true;--class:invert;--heading-divider:5;--theme:ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe;" data-marpit-pagination-total="25"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="21" data-marpit-fragments="7" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm" lang="en-US" class="invert" data-marpit-pagination="21" style="--paginate:true;--class:invert;--heading-divider:5;--theme:9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm;" data-marpit-pagination-total="25">
<h3 id="why-properties">Why properties?</h3> <h3 id="why-properties">Why properties?</h3>
<ul> <ul>
<li data-marpit-fragment="1">Why use properties if we could just use public fields?</li> <li data-marpit-fragment="1">Why use properties if we could just use public fields?</li>
@ -493,7 +505,7 @@ Student class:
</li> </li>
</ul> </ul>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="22" data-marpit-fragments="4" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe" lang="en-US" class="invert" data-marpit-pagination="22" style="--paginate:true;--class:invert;--heading-divider:5;--theme:ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe;" data-marpit-pagination-total="25"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="22" data-marpit-fragments="4" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm" lang="en-US" class="invert" data-marpit-pagination="22" style="--paginate:true;--class:invert;--heading-divider:5;--theme:9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm;" data-marpit-pagination-total="25">
<h2 id="value-and-reference-types">Value and Reference Types</h2> <h2 id="value-and-reference-types">Value and Reference Types</h2>
<ul> <ul>
<li data-marpit-fragment="1">See the example below. If this program is executed, what will be the output?</li> <li data-marpit-fragment="1">See the example below. If this program is executed, what will be the output?</li>
@ -522,7 +534,7 @@ Student class:
</div> </div>
</div> </div>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="23" data-marpit-fragments="4" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe" lang="en-US" class="invert" data-marpit-pagination="23" style="--paginate:true;--class:invert;--heading-divider:5;--theme:ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe;" data-marpit-pagination-total="25"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="23" data-marpit-fragments="4" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm" lang="en-US" class="invert" data-marpit-pagination="23" style="--paginate:true;--class:invert;--heading-divider:5;--theme:9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm;" data-marpit-pagination-total="25">
<ul> <ul>
<li data-marpit-fragment="1">Another example: When this program is executed, what will be the output?</li> <li data-marpit-fragment="1">Another example: When this program is executed, what will be the output?</li>
<li data-marpit-fragment="2"><code>string</code>, <code>Array</code> and Classes are of the *<strong>reference type</strong> <li data-marpit-fragment="2"><code>string</code>, <code>Array</code> and Classes are of the *<strong>reference type</strong>
@ -558,10 +570,10 @@ Student class:
</div> </div>
</div> </div>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="24" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe" lang="en-US" class="invert" data-marpit-pagination="24" style="--paginate:true;--class:invert;--heading-divider:5;--theme:ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe;" data-marpit-pagination-total="25"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="24" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm" lang="en-US" class="invert" data-marpit-pagination="24" style="--paginate:true;--class:invert;--heading-divider:5;--theme:9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm;" data-marpit-pagination-total="25">
<p><img src="imgs/7%20Classes%20and%20Objects_6.png" alt="" /></p> <p><img src="imgs/7%20Classes%20and%20Objects_6.png" alt="" /></p>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="25" data-marpit-fragments="4" data-paginate="true" data-class="exercise invert" data-heading-divider="5" data-theme="ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe" lang="en-US" class="exercise invert" data-marpit-pagination="25" style="--paginate:true;--class:exercise invert;--heading-divider:5;--theme:ms8msnm58am9pdu7xroo9cceafwjshqw5mpy0cz5gqe;" data-marpit-pagination-total="25"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="25" data-marpit-fragments="4" data-paginate="true" data-class="exercise invert" data-heading-divider="5" data-theme="9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm" lang="en-US" class="exercise invert" data-marpit-pagination="25" style="--paginate:true;--class:exercise invert;--heading-divider:5;--theme:9mp64rqthc45fzsyh9lzvi0j0f2d35s76theih2ughm;" data-marpit-pagination-total="25">
<h2 id="exercise-3">Exercise 3</h2> <h2 id="exercise-3">Exercise 3</h2>
<ol> <ol>

@ -220,24 +220,37 @@ class Program
* The value of the variable `id` of the object `student` is now `12345678` * The value of the variable `id` of the object `student` is now `12345678`
## Access Specifiers ## Access modifiers
* *__Access specifiers__* can be used to get additional level of protection inside classes <div class='columns21' markdown='1'>
* Variables specified with `private` are accessible only inside the containing class <div markdown='1'>
* This is the ***default*** access!
* Variables specified with `public` are accessible outside of the class * [Access modifiers](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers) can be used to get additional level of protection inside classes
```csharp * `private`: accessible only inside the containing class
class Student * This is the default, but we can make it more explicit by writing it out
{ * `public`: accessible everywhere in the namespace
int id; // Accessible only inside the class * Less common, but good to know:
private string name; // Accessible only inside the class * `protected`: like `private`, but also accessible by the *inheritors* of the class
public string address; // Accessible everywhere within the namespace * `virtual`: accessible and *overridable* by inheritors
}
``` </div>
<div markdown='1'>
```csharp
class Student
{
int id;
private string name;
public string address;
}
```
</div>
</div>
--- ---
* Continuing on the class in the previous example: if we follow the `student` variable with a dot, Visual Studio IntelliSense will only suggest the `address` variable, because it was the only `public` variable of the `Student` class! * Continuing on the class in the previous example: if we follow the `student` variable with `.`, Visual Studio IntelliSense will only suggest the `address` variable, because it was the only `public` variable of the `Student` class!
![](imgs/7%20Classes%20and%20Objects_3.png) ![](imgs/7%20Classes%20and%20Objects_3.png)

Loading…
Cancel
Save