C# 30.09.2023

This commit is contained in:
2023-10-01 09:00:42 +00:00
parent 8007485316
commit 2a35336a4c
3 changed files with 227 additions and 0 deletions

View File

@ -350,3 +350,87 @@ int Sum1(int a, int b) {
int Sum2(int a, int b) => a + b;
```
### Рекурсия
```C#
int Factorial(int n) {
if (n == 0) return 1;
return n * Factorial(n - 1);
}
```
### Функции внути функций
```C#
int Sum2Arr(int[] arr1, int[] arr2) {
int Sum(int[] arr) {
int result = 0;
foreach (int i in arr) {
result += i;
}
return result;
}
return Sum(arr1) + Sum(arr2);
}
```
## Enum
Enum используется для хранения состояния
```C#
enum Operation {
Add,
Sub
}
Operation foo = Operation.Add;
```
### Тип констант перечисления
```C#
enum Time: byte {
Morning,
Afternoon,
Evening,
Night
}
```
Тип обязательно должен быть целочисленным. По умолчанию int
### Задание значения для Enum
```C#
enum DayTime {
Morning = 3,
Afternoon // 4
}
```
## OOP
OOP - зло, C# - OOP -> C# - зло
```C#
namespace Foo {
internal class Enemy {
public int hp;
public int armor;
public int damage;
public void Move() {
Console.WriteLine("I am moving");
}
public void Attack() {
Console.WriteLine("I am attacking");
}
}
}
```