using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace csharp_重载
{
    class Program
    {
        static int a(int a1,int a2)
        {
            return a1-a2;
        }
        static short a(short b1, short b2)
        {
             return b1+b2;//无法将int 隐式转换为short 是什么意思 我不是定义b1 b2为short了吗 哪里来的int??
            //return (short)(b1+b2);
        }
        static void Main(string[] args)
        {
            //double re= Convert.ToDouble(a(15.1,12));
            double re=a(15.1,12.2);// 报错说无法double转换int  又是 哪里来的int??
            Console.WriteLine(re);
        }
    }
}错误1 无法将类型“int”隐式转换为“short”。存在一个显式转换(是否缺少强制转换?) 16
错误2 与“csharp_重载.Program.a(int, int)”最匹配的重载方法具有一些无效参数22
错误3 参数“1”: 无法从“double”转换为“int”22
错误4 参数“2”: 无法从“double”转换为“int”22 路过的大侠指点啊!!!

解决方案 »

  1.   

    C#中,未加声明和类型后缀的数字,无小数默认为int,有小数默认为double...取值范围较大的内置类型不允许隐式转换为取值范围较小的内置类型,必须显式转换...
      

  2.   

    不能将存储大小更大的非文本数值类型隐式转换为 short(有关整型的存储大小的信息,请参见 整型表(C# 参考))。例如,请看以下两个 short 变量 x 和 y:  复制代码 
            short x = 5, y = 12;
     以下赋值语句将产生一个编译错误,原因是赋值运算符右侧的算术表达式在默认情况下的计算结果为 int 类型。short z = x + y; // Error: no conversion from int to short 若要解决此问题,请使用强制转换:short z = ( short )(x + y); // OK: explicit conversion 但是,在目标变量具有相同或更大的存储大小时,使用下列语句是可能的:  复制代码 
    int m = x + y;
    long n = x + y;
     不存在从浮点型到 short 的隐式转换。例如,除非使用显式强制转换,否则以下语句将生成一个编译器错误:  复制代码 
            short x = 3.0;          // Error: no implicit conversion from double
    short y = (short)3.0;   // OK: explicit conversion
     
      

  3.   

    上面的代码错了 不是short 应该是float
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace csharp_重载
    {
        class Program
        {
            static int a(int a1,int a2)
            {
                return a1-a2;
            }
            static float a(float b1,float b2)
            {
                return b1 + b2;
                //return Convert.ToDouble(b1+b2);
            }
            static void Main(string[] args)
            {
                //double re= Convert.ToDouble(a(15.1,12));
                double re= Convert.ToDouble(a(15.1,12.2));
                Console.WriteLine(re);
            }
        }
    }
      

  4.   

    15.1是double,要声明为float在后面加个F,这样就可以了:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace csharp_重载
    {
        class Program
        {
            static int a(int a1,int a2)
            {
                return a1-a2;
            }
            static float a(float b1,float b2)
            {
                return b1 + b2;
                //return Convert.ToDouble(b1+b2);
            }
            static void Main(string[] args)
            {
                //double re= Convert.ToDouble(a(15.1,12));
                double re= Convert.ToDouble(a(15.1F,12.2F));
                Console.WriteLine(re);
            }
        }
    }
      

  5.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace csharp_重载
    {
        class Program
        {
            static int a(int a1,int a2)
            {
                return a1-a2;
            }
            static short a(short b1, short b2)
            {
                 return b1+b2;//无法将int 隐式转换为short 是什么意思 我不是定义b1 b2为short了吗 哪里来的int??
                //return (short)(b1+b2);
            }
            static void Main(string[] args)
            {
                //double re= Convert.ToDouble(a(15.1,12));
                double re=a(15.1f,12.2f);// 报错说无法double转换int  又是 哪里来的int??
                Console.WriteLine(re);
            }
        }
    }