StackOverflowException没有明显的原因..这真的很烦人

Ada*_*dam 0 c# stack-overflow

WHYYYYYYY?

以下是我的一半代码,因为我已经删除了大部分代码以及我在尝试解决此问题时丢失的每一根头发.无论我删除或更改什么,我都会收到StackOverflowException.

这是非常奇怪的,因为这个完全相同的代码更早.

请给我任何建议,因为我很无能为力......

我检查了这个,我不认为这是发生的事情:

  • 无限递归循环
  • 程序只是耗尽堆栈空间

­

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;

namespace ConsoleApplication10
{
class Program
{
    public int currentDialogueID = 0;

    List<NPC> NPCList = new List<NPC>() {
            new NPC(0, "Man", 1, true),
            new NPC(1, "Woman", 1, true),
            new NPC(2, "Troll", 3, true)
        };

    static void Main(string[] args)
    {
        Program d = new Program();
        d.Init();
        Console.ReadKey(false);
    }

    void Init()
    {
        getNPCByName("Man").NPCDialogue();
    }

    public NPC getNPCByName(string npcName)
    {
        IEnumerable<NPC> myNPCsName = from nn in NPCList
                                      where nn._name.ToLower() == npcName.ToLower()
                                      orderby nn._name ascending
                                      select nn;

        foreach(NPC nn2 in myNPCsName)
        {
            return nn2;
        }

        return null;
    }

    public NPC getNPCByID(int npcID)
    {
        IEnumerable<NPC> NPCsByID = from ni in NPCList
                                    where ni._npcID == npcID
                                    orderby ni._npcID ascending
                                    select ni;

        foreach(NPC ni2 in NPCsByID)
        {
            return ni2;
        }

        return null;
    }

    public string getNPCNameByID(int npcid)
    {
        return getNPCByID(npcid)._name;
    }
}

class NPC : Program
{
    public string _name = "null";
    public int _level;
    public int _npcID;
    public int _maxDamage;
    public bool _canFight = false;

    public NPC(int npcID = 0, string name = "null", int level = 0, bool canSpeak = false, bool canFight = false, int maxDamage = 0)
    {
        _level = level;
        _name = name;
        _npcID = npcID;
        _canFight = canFight;
        _maxDamage = maxDamage;
    }

    public void NPCDialogue()
    {
        currentDialogueID = _npcID;
        switch(_npcID)
        {
            case 0:
            NPCSpeak("Man test... ... ...");
            break;

            case 1:
            NPCSpeak("Woman test");
            break;

            case 2:
            NPCSpeak("I'm Elad the Troll, Ramzes your ear is that of an elf");
            break;

            default:
            return;
        }
    }

    public void NPCSpeak(string text, int npcID = 99999)
    {
        if(npcID == 99999)
            npcID = currentDialogueID;
        if(npcID != 99999)
            type(getNPCNameByID(npcID) + ": " + text);
    }

    public void type(string x)
    {
        Random rnd = new Random();
        char[] xx = x.ToCharArray();
        for(int i = 0; i < xx.Length; i++)
        {
            Console.Write(xx[i]);
            System.Threading.Thread.Sleep(rnd.Next(10, 120));
            if(xx[i] == ':' || (xx[i] == '.' && xx[i - 1] != '.' && xx[i + 1] != '.') || xx[i] == '!' || xx[i] == '\n' || xx[i] == '?')
            {
                System.Threading.Thread.Sleep(rnd.Next(400, 1500));
            }
        }
    }
}

class Item : Program
{
    public string _name = "null";
    public string _description = "How did you get this?";
    public bool _isWeapon = false;
    public int _maxDamage;
    public int _itemID = 0;

    public Item(int itemID = 0, string name = "null", string description = "null", bool isWeapon = false, int maxDamage = 0)
    {
        _itemID = itemID;
        _name = name;
        _description = description;
        _isWeapon = isWeapon;
        _maxDamage = maxDamage;
    }
}
}
Run Code Online (Sandbox Code Playgroud)

xan*_*tos 5

你有NPC是一个程序,但是每个程序都有一个3 NPC的列表...你看到了吗?

下次,你看一下Call Stack窗口,你会看到

ConsoleApplication5.exe!ConsoleApplication10.Program.Program() Line 13 + 0xffffffe6 bytes   C#
ConsoleApplication5.exe!ConsoleApplication10.NPC.NPC(int npcID, string name, int level, bool canSpeak, bool canFight, int maxDamage) Line 77 + 0x8 bytes    C#
ConsoleApplication5.exe!ConsoleApplication10.Program.Program() Line 15 + 0x40 bytes C#
ConsoleApplication5.exe!ConsoleApplication10.NPC.NPC(int npcID, string name, int level, bool canSpeak, bool canFight, int maxDamage) Line 77 + 0x8 bytes    C#
ConsoleApplication5.exe!ConsoleApplication10.Program.Program() Line 15 + 0x40 bytes C#
ConsoleApplication5.exe!ConsoleApplication10.NPC.NPC(int npcID, string name, int level, bool canSpeak, bool canFight, int maxDamage) Line 77 + 0x8 bytes    C#
ConsoleApplication5.exe!ConsoleApplication10.Program.Program() Line 15 + 0x40 bytes C#
ConsoleApplication5.exe!ConsoleApplication10.NPC.NPC(int npcID, string name, int level, bool canSpeak, bool canFight, int maxDamage) Line 77 + 0x8 bytes    C#
ConsoleApplication5.exe!ConsoleApplication10.Program.Program() Line 15 + 0x40 bytes C#
Run Code Online (Sandbox Code Playgroud)

然后您可以单击这些行并查看下一步的启动位置.