问题描述
嘿,我已经在COnsole应用程序中编写了一个代码,用户必须在其中输入颜色名称,当按下输入并写入一些forground颜色时,用户将输入颜色。
Hey i have Writen a Code in COnsole Application in which user have to Enter a Color name and when it press enter and write something forground color will be the enter color by user.
Console.WriteLine("Enter the Name of Color in Which do You Want to Print");
string str = Console.ReadLine();
Console.ForegroundColor = (ConsoleColor) str;
但它给出错误错误: - 无法转换类型'string'到'System.ConsoleColor'
任何想法如何将字符串转换为System.ConsoleColor
but it gives error Error:- Cannot convert type 'string' to 'System.ConsoleColor'
any idea how can i convert string to System.ConsoleColor
推荐答案
Console.ForegroundColor =(ConsoleColor)str;
Console.ForegroundColor = (ConsoleColor) str;
你可能很容易发现自己在阅读 [],你应该使用:
As you may easily find yourself reading the documentation[^], you should use:
(ConsoleColor) Enum.Parse(typeof(ConsoleColor), str);
:)
:)
Console.WriteLine("Enter the Name of Color in Which do You Want to Print");
string str = Console.ReadLine();
Console.ForegroundColor =(ConsoleColor) Enum.Parse(typeof(ConsoleColor), str,true);
Console.Read()
这篇关于将String转换为System.ConsoleColor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!