c# – 字段’xxx’永远不会被赋值,并且将始终具有其默认值null

c# – 字段’xxx’永远不会被赋值,并且将始终具有其默认值null,第1张

概述我的错误:    字段’StockManagement.LargeItems1.largeS’永远不会被赋值,并且将始终具有其默认值null 我的代码: namespace StockManagement{ class LargeItems1 { private Stack<string> largeS; public LargeItems1( 我的错误:
   字段’StockManagement.LargeItems1.largeS’永远不会被赋值,并且将始终具有其默认值null
我的代码:

namespace StockManagement{    class LargeItems1    {        private Stack<string> largeS;         public LargeItems1()        {            Stack<string> largeS = new Stack<string>();        }        public voID LargeItemsAdd()        {            string usersinput2,tempValue;            int tempValueI = 0;            bool attempt = false;//,whichOne = false;            Console.Writeline("Would you like to remove or add an item to the storage area \n Reply add OR remove");            string usersinput = Console.Readline();            usersinput = usersinput.Tolower();            if (usersinput.Contains("remove"))            {                LargeItemsRemove(largeS);                return;            }            else if (usersinput.Contains("add"))            {                Console.Writeline("Please input (numerically) how many IDs you'd like to add");                tempValue = Console.Readline();                attempt = int.TryParse(tempValue,out tempValueI);                while (!attempt)                {                    Console.Writeline("Please input (numerically) how many IDs you'd like to add,you dID not input a numeric value last time");                    tempValue = Console.Readline();                    attempt = int.TryParse(tempValue,out tempValueI);                }                for (int i = 0; i < tempValueI; i++)                {                    Console.Writeline("Please input the ID's (one at a time) of the item you would like to add");                    usersinput2 = Console.Readline();                    if (largeS.Contains(usersinput2))                    {                        Console.Writeline("That ID has already been stored");                    }                    else                    {                        largeS.Push(usersinput2);                    }                }                foreach (var item in largeS)                {                    Console.Writeline("Current (large) item ID's: " + item);                }            }        }        public voID LargeItemsRemove(Stack<string> largeS)        {            if (largeS.Contains(null))            {                Console.Writeline("No IDs stored");            }            else            {                string popped = largeS.Pop();                foreach (var item in largeS)                {                    Console.Writeline("Pop: " + item);                }                Console.Writeline("Removed ID = " + popped);            }        }    }}

我不明白如何将我的值分配给实例.我很感激可以提供任何帮助!

解决方法 更改构造函数以初始化字段而不是局部变量:

public LargeItems1(){    this.largeS = new Stack<string>();}
总结

以上是内存溢出为你收集整理的c# – 字段’xxx’永远不会被赋值,并且将始终具有其默认值null全部内容,希望文章能够帮你解决c# – 字段’xxx’永远不会被赋值,并且将始终具有其默认值null所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1223826.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存