Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在c中调用该函数时,是否可以检查该函数中的参数数据类型而不产生编译时错误?_C#_Validation_Types - Fatal编程技术网

C# 在c中调用该函数时,是否可以检查该函数中的参数数据类型而不产生编译时错误?

C# 在c中调用该函数时,是否可以检查该函数中的参数数据类型而不产生编译时错误?,c#,validation,types,C#,Validation,Types,在c中调用该函数时,是否可以检查该函数中的参数数据类型而不产生编译时错误? 我不希望在像javascript这样的c语言中将错误的值传递给函数时出现编译时错误 以下是我的示例代码: class Program { static int containsThatItem(char[] arr1, char[] arr2) { Dictionary<char, bool> map = new Dictionary<char, bool>();

在c中调用该函数时,是否可以检查该函数中的参数数据类型而不产生编译时错误? 我不希望在像javascript这样的c语言中将错误的值传递给函数时出现编译时错误

以下是我的示例代码:

class Program
{
    static int containsThatItem(char[] arr1, char[] arr2)
    {
        Dictionary<char, bool> map = new Dictionary<char, bool>();

        foreach (var e in arr1)
        {
            map.Add(e, true);
        }

        foreach (var e in arr2)
        {
            if (!map.ContainsKey(e))
            {
                continue;
            }

            if (map[e])
            {
                Console.WriteLine("Item Found!");
            }
        }

        return 0;
    }

    static void Main(string[] args)
    {
        int[] arr1 = { 0, 1, 2, 3 };
        char[] arr2 = { 'a', 'b', 'c', 'd'};

        //Here I'm getting compile time error
        containsThatItem(arr1, arr2);   //All I want is to overcome this compile time error by adding validation of datatype inside the containsThatItem method.

        Console.ReadKey();
    }
}
可以将其作为对象传递

您可以使用GetType函数检查数据类型

这是一个代表我的解决方案的代码,它可能包含错误,但您应该能够根据您的逻辑修复它,我只是说明解决方案的想法

static int containsThatItem(object arr1, object arr2)
        {
            if (arr1.GetType() == typeof(int[]) && arr2.GetType() == typeof(char[]))
            {
                int[] Converted_arr1 = (int[]) arr1;
                char[] Converted_arr2 = (char[]) arr2;

                Dictionary<char, bool> map = new Dictionary<char, bool>();

                foreach (var e in Converted_arr1)
                {
                    map.Add(e, true);
                }

                foreach (var e in Converted_arr2)
                {
                    if (!map.ContainsKey(e))
                    {
                        continue;
                    }

                    if (map[e])
                    {
                        Console.WriteLine("Item Found!");
                    }
                }

            }

            return 0;
        }
        static void Main(string[] args)
        {
            int[] arr1 = { 0, 1, 2, 3 };
            char[] arr2 = { 'a', 'b', 'c', 'd' };

            //Here I'm getting compile time error
            containsThatItem(arr1, arr2);   //All I want is to overcome this compile time error by adding validation of datatype inside the containsThatItem method.

        }
可以使用对象或动态参数类型

要执行类型检查,可以使用或运算符

来自MSDN的is操作员摘要

使用is运算符检查类型是否可以通过引用转换、装箱转换或取消装箱转换进行转换

MSDN中操作员类型的简要总结

使用typeof运算符检查表达式结果的运行时类型是否与给定类型完全匹配


您正在将int[]作为参数传递,但预期的类型是char[]。您需要将数字更改为chars,并更改数组类型0=>0',int[]=>char[]。另外,无论如何,您仍然需要修改代码。正确的答案是:首先,使用适当的类型。如果从其他地方获取值/数组,则可以将它们转换为适当的类型。读取:。此外,如果要在containsThatItem方法中添加数据类型验证,则必须编写泛型方法。