C#中 无法将类型“System.ConsoleColor”隐式转换为“ConsoleApplication5.Program.Color”
namespaceColor{classProgram{staticvoidMain(string[]args){Colorcc=Color.Red;System.Con...
namespace Color
{
class Program
{
static void Main(string[] args)
{
Color cc = Color.Red;
System.Console.WriteLine("The " + cc.GetType() + " type is drived from " + cc.GetType().BaseType);
methodl(cc);
Color cs = Color.Red;
System.Console.WriteLine("Does cc equals cs: " + cc.Equals (cs));
switch (cs)
{
case ConsoleColor.Red:
System.Console.WriteLine ("the color is " + cs);
break;
case ConsoleColor.Green:
System.Console.WriteLine ("the color is " + cs);
break;
case (using ConsoleProgramColor)ConsoleColor.Blue:
System.Console.WriteLine ("the color is " + cs);
break;
default:
System.Console.WriteLine ("should never come here");
break ;
}
}
public enum Color {
Red,
Green,
Blue
}
public static void methodl(int i)
{System.Console.WriteLine(i);}
public static void methodl(Color c)
{System.Console.WriteLine(c);}
}
} 展开
{
class Program
{
static void Main(string[] args)
{
Color cc = Color.Red;
System.Console.WriteLine("The " + cc.GetType() + " type is drived from " + cc.GetType().BaseType);
methodl(cc);
Color cs = Color.Red;
System.Console.WriteLine("Does cc equals cs: " + cc.Equals (cs));
switch (cs)
{
case ConsoleColor.Red:
System.Console.WriteLine ("the color is " + cs);
break;
case ConsoleColor.Green:
System.Console.WriteLine ("the color is " + cs);
break;
case (using ConsoleProgramColor)ConsoleColor.Blue:
System.Console.WriteLine ("the color is " + cs);
break;
default:
System.Console.WriteLine ("should never come here");
break ;
}
}
public enum Color {
Red,
Green,
Blue
}
public static void methodl(int i)
{System.Console.WriteLine(i);}
public static void methodl(Color c)
{System.Console.WriteLine(c);}
}
} 展开
1个回答
展开全部
你在前面声明的是:
Color cs = Color.Red;
cs 是个 Color 类型的变量。
而在后面判断却是:
switch (cs)
{
case ConsoleColor.Red:
一个 System.ConsoleColor 类型的。
如果前面那个 Color 是 System.Drawing.Color,那么还可以强制转换,但是你这个 Color 是你自己枚举的一个类型,根本无法转换成 System.ConsoleColor。
要么前面声明的时候就声明成 ConsoleColor,要么后面判断就都统一判断 Color。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询