我得到“堆栈溢出” c#方法的问题 [英] Im getting "stack overflow" issues with c# method

查看:93
本文介绍了我得到“堆栈溢出” c#方法的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在播放器类中遇到问题并且反复重复播放器......构建一遍又一遍,我在编码中创建了一个循环。我很新,所以它没有华而不实,我只是测试类交互,似乎无法解决这个问题。非常感谢您的提示。

Im getting issues with the "Player" class and its repeating "Player..constructed" over and over again somehow I have created a loop in my coding. Im pretty new so its nothing flashy, Im just testing the class interactions and cannot seem to fix this issue. Many thanks for tips.

static void Main(string[] args)
        {// test that dealer makes the wheel
            Dealer dealer = new Dealer();
            //test that dealer closes the betting
            dealer.closeBetting();
            //test that dealer can spin wheel
            dealer.spinWheel();
            //test that player is created
            Player player = new Player();
            Console.ReadKey();
        }
    }
    class Player
    {
        Player player;
        //creating a player
        public Player() 
        {
            Console.WriteLine("Player...constructed");
            player = new Player();
        }
        Bet bet;
        public void spinWheel() 
        {
            bet.spinWheel();
        }
    }

推荐答案

永远不要在构造函数中创建同一个类的实例。

Never create an instance of the same class in the constructor.
public Player()
{
    Console.WriteLine("Player...constructed");
    player = new Player();
}





您将进入无限循环,最终导致内存溢出。



You are going into an infinite loop which is eventually causing the memory to overflow.


这篇关于我得到“堆栈溢出” c#方法的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆