/
spirzen
/
it-code-examples
Обзор
Документация
Войти
/
spirzen
/
it-code-examples
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
examples/csharp/csharp-505-25-002/main.cs
45 строк
1 KB
Spirzen
Code migration pack
09 июн 2026, 01:41
09 июн 2026, 01:41
cebc284
Код
Авторство
О чём код?
public class Warrior { // Поля (обычно private) private string _name; private static int _count = 0; public int Strength { get; set; } = 10; public int Agility { get; set; } = 10; // Автосвойство (компилятор сам создаст поле) public int Level { get; set; } = 1; // Свойство с логикой public string Name { get => _name; set { if (string.IsNullOrEmpty(value)) throw new ArgumentException("Имя не может быть пустым"); _name = value; } } // Вычисляемое свойство (только get) public int Damage => (Strength + Agility) + Level * 2; // Статическое свойство public static int Count => _count; // Конструктор public Warrior(string name, int level = 1) // опциональный параметр { Name = name; Level = level; _count++; } // Переопределение ToString() public override string ToString() => $"Воин: {Name} (ур. {Level})"; } // Использование var warrior = new Warrior("Артур", 5); warrior.Level = 10; Console.WriteLine(warrior.Damage);