小编Nik*_*i99的帖子

当使用object参数传递此信息时,为什么Enum.GetName()要求输入?

考虑.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(); …
Run Code Online (Sandbox Code Playgroud)

.net c# parameters enums

4
推荐指数
1
解决办法
133
查看次数

标签 统计

.net ×1

c# ×1

enums ×1

parameters ×1