<lidata-marpit-fragment="1">Likewise, non-static class methods <strong><em>have to</em></strong> be called through an instance:<preis="marp-pre"data-auto-scaling="downscale-only"><codeclass="language-csharp"><spanclass="hljs-keyword">class</span><spanclass="hljs-title">MyAwesomeClass</span>
<lidata-marpit-fragment="1">Likewise, non-static class methods <strong><em>have to</em></strong> be called through an instance:<preis="marp-pre"data-auto-scaling="downscale-only"><codeclass="language-csharp"><spanclass="hljs-keyword">class</span><spanclass="hljs-title">MyAwesomeClass</span>
<h3id="static-members-an-example">Static members: An example</h3>
<h3id="static-members-an-example">Static members: An example</h3>
<ul>
<ul>
<lidata-marpit-fragment="1">In this example, a static field is used for keeping count on how many times the class has been instantiated:<preis="marp-pre"data-auto-scaling="downscale-only"><codeclass="language-csharp"><spanclass="hljs-keyword">class</span><spanclass="hljs-title">Person</span>
<lidata-marpit-fragment="1">In this example, a static field is used for keeping count on how many times the class has been instantiated:<preis="marp-pre"data-auto-scaling="downscale-only"><codeclass="language-csharp"><spanclass="hljs-keyword">class</span><spanclass="hljs-title">Person</span>
<lidata-marpit-fragment="1">Methods can also be static</li>
<lidata-marpit-fragment="1">Methods can also be static — for example, <code>Console.WriteLine</code></li>
<lidata-marpit-fragment="2">What happens when you try to call a non-static method from a static method?<preis="marp-pre"data-auto-scaling="downscale-only"><codeclass="language-csharp"><spanclass="hljs-keyword">class</span><spanclass="hljs-title">Program</span>
<lidata-marpit-fragment="2">What happens when you try to call a non-static method from a static method?<preis="marp-pre"data-auto-scaling="downscale-only"><codeclass="language-csharp"><spanclass="hljs-keyword">class</span><spanclass="hljs-title">Program</span>
You can give a parameter a default value by assigning it in the method declaration
This makes the parameter optional
double CircleArea(double radius = 0)
{
double area = Math.PI * radius * radius;
return area;
}
}
```
Console.WriteLine(CircleArea()); // This outputs 0
</div>
<divmarkdown='1'>
Console.WriteLine(CircleArea(2)); // This outputs 12,56637...
# Exercise 1
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
The return value of a method is __returned __ with the return keyword:
* The variables declared inside a method are *__local variables__* and only accessible inside the method
```csharp
void ScopeExample()
{
// Variable 'a' is only accessible inside of this method
int a = 10;
a = 20; // This works
}
a = 30; // This throws an error
```
a = 30; // This throws an error
* Contrastingly, variables declared outside the method are available inside, as seen in an earlier [example](#parameters-an-example)
# Exercise 2
## Exercise 2
<!--_class: "exercise invert" -->
Write a method that takes a string as a parameter and prints the total number of spaces in that 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.

## Exercise 3
<!--_class: "exercise invert" -->
<divclass='columns'markdown='1'>
<divmarkdown='1'>
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 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.
(Hint: You can iterate a string just like arrays and lists.)
</div>
<divmarkdown='1'>


# Exercise 3
</div>
</div>
Write a method that takes a string as a parameter and prints each unique letter in that string.
## Exercise 4
<!--_class: "exercise invert" -->
Write a new method that takes a string as a parameter and prints the number of each unique letter in the string.
* 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!

## Exercise 5
<!--_class: "exercise invert" -->
# Exercise 4
* 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.
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.
## Assignments
Remember to check that method accepts only numbers.
# Assignments
[Assignments about this topic can be found here](https://gitea.buutti.com/education/academy-assignments/src/branch/master/C%23%20Basics/6.%20Methods)
[Assignments about this topic can be found here](https://gitea.buutti.com/education/academy-assignments/src/branch/master/C%23%20Basics/6.%20Methods)