Ошибка что не найдены переменные. Что делать?

Я сделал код:

using System;

class Program {
    public static void MainClass(string[] str) {
        Console.WriteLine("Введите свое имя: ");
        string name = Console.ReadLine();
        Warrior character = new Warrior (500, 100, true);
        Console.WriteLine($"HP у {name}: {character.hp}\nDEF у {name}: {character.def}\nAlive y {name}: {character.isAlive}");
    }
}

class Warrior {
    public Warrior (int hp, int def, int dps, bool isAlive) {
        this.hp = hp;
        this.def = def;
        this.isAlive = isAlive;
    }
}

При компиляции выводится ошибка:

main.cs(7,29): error CS1729: The type `Warrior' does not contain a constructor that takes `3' arguments
main.cs(13,12): (Location of the symbol related to previous error)
main.cs(8,60): error CS1061: Type `Warrior' does not contain a definition for `hp' and no extension method `hp' of type `Warrior' could be found. Are you missing an assembly reference?
main.cs(12,7): (Location of the symbol related to previous error)
main.cs(8,100): error CS1061: Type `Warrior' does not contain a definition for `def' and no extension method `def' of type `Warrior' could be found. Are you missing an assembly reference?
main.cs(12,7): (Location of the symbol related to previous error)
main.cs(8,145): error CS1061: Type `Warrior' does not contain a definition for `isAlive' and no extension method `isAlive' of type `Warrior' could be found. Are you missing an assembly reference?
main.cs(12,7): (Location of the symbol related to previous error)
main.cs(14,14): error CS1061: Type `Warrior' does not contain a definition for `hp' and no extension method `hp' of type `Warrior' could be found. Are you missing an assembly reference?
main.cs(12,7): (Location of the symbol related to previous error)
main.cs(15,14): error CS1061: Type `Warrior' does not contain a definition for `def' and no extension method `def' of type `Warrior' could be found. Are you missing an assembly reference?
main.cs(12,7): (Location of the symbol related to previous error)
main.cs(16,14): error CS1061: Type `Warrior' does not contain a definition for `isAlive' and no extension method `isAlive' of type `Warrior' could be found. Are you missing an assembly reference?
main.cs(12,7): (Location of the symbol related to previous error)
error CS5001: Program `a.out' does not contain a static `Main' method suitable for an entry point

Что делать?


Ответы (1 шт):

Автор решения: KoVadim

Перестать писать на питоне:)

В C#, как в многих языках, все нужно явно объявлять. Например, "поля".

using System;

class Program {
    public static void MainClass(string[] str) {
        Console.WriteLine("Введите свое имя: ");
        string name = Console.ReadLine();
        Warrior character = new Warrior (500, 100, true);
        Console.WriteLine($"HP у {name}: {character.hp}\nDEF у {name}: {character.def}\nAlive y {name}: {character.isAlive}");
    }
}

class Warrior {
    // третий параметр закомментирован, потому что при вызове используется только три
    // что делает этот третий - не понятно
    // кол-во и типы должны совпадать
    public Warrior (int hp, int def,/* int dps,*/ bool isAlive) {
        this.hp = hp;
        this.def = def;
        this.isAlive = isAlive;
    }
    // явно объявляем все.
    public int hp;
    public int def;
    //public int dps; // опять же, закомменировано, но как пример
    public bool isAlive;
}
→ Ссылка