Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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# 我已经为funion发送了两个通用参数,第一个是char,第二个是int,为什么第一个if cond是work?_C#_C# 4.0_C# 3.0 - Fatal编程技术网

C# 我已经为funion发送了两个通用参数,第一个是char,第二个是int,为什么第一个if cond是work?

C# 我已经为funion发送了两个通用参数,第一个是char,第二个是int,为什么第一个if cond是work?,c#,c#-4.0,c#-3.0,C#,C# 4.0,C# 3.0,我向func发送了两个通用参数,简单地询问它们是否为typeof int,我将其中一个作为char发送,第二个作为int发送,但仍然是if cindison是work exsample:结果是第一个if cond是work,我不明白为什么,因为第一个参数是char using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;

我向func发送了两个通用参数,简单地询问它们是否为typeof int,我将其中一个作为char发送,第二个作为int发送,但仍然是if cindison是work exsample:结果是第一个if cond是work,我不明白为什么,因为第一个参数是char

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp5
{
    class Program
    {

        public static T Adds<T>(T number1 , T number2 )
        {
            if ((number1 is int) && (number2 is int))
            {
                Console.WriteLine("int test");
            }
            else if (number1 is double )
            {
                Console.WriteLine("double test");
            }
            return number1;
        }

        public static void Main(string[] args)
        {

            Console.WriteLine(Adds('c',2));
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间控制台EAPP5
{
班级计划
{
公共静态T加法(T编号1,T编号2)
{
if((number1为int)和&(number2为int))
{
控制台写入线(“int测试”);
}
否则,如果(数字1为双精度)
{
控制台写入线(“双重测试”);
}
返回编号1;
}
公共静态void Main(字符串[]args)
{
控制台写入行(添加('c',2));
}
}
}

这就是您想要做的:

class Program
{
    public static T Adds<T>(object number1, object number2)
    {
        if ((number1 is int) && (number2 is int))
        {
            Console.WriteLine("int test");
        }
        else if (number1 is double)
        {
            Console.WriteLine("double test");
        }
        return (T)Convert.ChangeType(number1, typeof(T));
    }

    public static void Main(string[] args)
    {

        Console.WriteLine(Adds<int>('c', 2));
    }
}
类程序
{
公共静态T添加(对象编号1,对象编号2)
{
if((number1为int)和&(number2为int))
{
控制台写入线(“int测试”);
}
否则,如果(数字1为双精度)
{
控制台写入线(“双重测试”);
}
return(T)Convert.ChangeType(number1,typeof(T));
}
公共静态void Main(字符串[]args)
{
控制台写入行(添加('c',2));
}
}

您已经用一个泛型类型声明了该方法。如果希望参数具有单独的类型,则必须声明

public static T Adds<T,T2>(T number1 , T2 number2 )
公共静态T添加(T编号1,T2编号2)

您还没有告诉编译器t使用哪种类型。 编号1和编号2必须具有相同的类型。 由于char可隐式转换为int,因此使用int类型

如果调试代码,您将看到number1是int类型,值为99

如果您想打电话:

Console.WriteLine(Adds<char>('c',2));
Console.WriteLine(添加('c',2));

将引发编译错误。

这是因为编译器隐式地将其转换为int,使其同时为int参数

若您试图像下面这样显式地指定T类型,您将得到编译错误

 Console.WriteLine(Adds<char>('c',2));
Console.WriteLine(添加('c',2));
因为这两个参数都不能隐式转换为char 为了克服这个问题,您需要将第二个parmaeter转换为char

Console.WriteLine(Adds<char>('c',(char)2));
Console.WriteLine(添加('c',(char)2));
这是[隐式数字转换表(C#参考)][1]的参考

[1] :它还将有助于当前签名

 T Adds<T>(T number1, T number2)
将作为

 Add<int>((int)'c', 2);
 Add<double>((double)1, 3.14);
在这种情况下

Add('c', 2)
将被称为

// No conversion: T1 is char, T2 is int
Add<char, int>('c', 2)
//没有转换:T1是char,T2是int
加上('c',2)

由于
char
可以隐式转换为
int
char
实际上是
UInt16
int
Int32
)时,
Add(T number1,T number2)
可以作为
Add((int)'c',2)执行另一个演示:
Add(7,5.3)
等于
Add((double)7,5.3)
所以如果我真的只想检查它的int值,我该怎么办呢?如果你想分离参数类型:像
公共静态T1 Adds(T1 number1,t2number2)那样放置它
首先是tnx的快速回答,其次是我想发送2个T参数,函数不是对象。。如果我用Console.WriteLine(添加('c',2));我得到了同样的解决方案。。我想检查它是否有2个参数,因为如果我只检查1个参数,那么有趣的是工作,如果我发送到类型,那么发现它都是大量的,如果你能告诉我为什么?
Add('c', 2)
// No conversion: T1 is char, T2 is int
Add<char, int>('c', 2)