Nik*_*i99 4 .net c# parameters enums
考虑.NET函数签名:
Enum.GetName(Type type, object o);
Run Code Online (Sandbox Code Playgroud)
似乎完全不需要询问Type何时传递此信息object o
以下代码说明了这一点:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public enum Color
{
Black, White, Red, Orange, Yellow, Green, Blue, Purple, Pink,
DarkRed, DarkGreen, DarkBlue,
NeonGreen, NeonBlue
}
class Program
{
private static Random rand = new Random();
static void Main(string[] args)
{
Color color = getRandomColor();
PrintType(color);
Console.WriteLine("typeof = " + typeof(Color));
Console.ReadLine();
}
public static void PrintType(object o)
{
Type type = o.GetType();
Console.WriteLine("type = " + type);
}
private static Color getRandomColor()
{
var values = Enum.GetValues(typeof(Color));
Color randomColor = (Color)values.GetValue(rand.Next(values.Length));
return randomColor;
}
}
}
Run Code Online (Sandbox Code Playgroud)
输出是
type = ConsoleApplication1.Color
typeof = ConsoleApplication1.Color
Run Code Online (Sandbox Code Playgroud)
这意味着Enum.GetName()方法签名可能如下所示:
Enum.GetName(object o);
Run Code Online (Sandbox Code Playgroud)
Seb*_*zus 10
o不需要是类型Color.例:
Enum.GetName(typeof(Color), 3) // == Orange
Run Code Online (Sandbox Code Playgroud)