我的重载示例有什么错误? [英] What is the error of my overloading example?
问题描述
您好,
我的重载示例有什么错误?
命名空间 MethodOverloading
{
class 程序
{
public class TestOverloading
{
public void 添加(字符串 a,字符串 b)
{
Console.WriteLine( 添加两个字符串: + a + + b);
}
public void 添加( int a1, int a2)
{
Console.WriteLine( 添加两个整数: + a1 + a2);
}
}
静态 void Main( string [] args)
{
TestOverloading obj = new TestOverloading();
obj.Add( Ankit, Agarwal);
obj.Add( 10 , 5 );
Console.ReadLine();
}
}
}
输出即将到来: -
添加两个字符串:Ankit Agarwal
添加两个整数:105
我想添加两个整数值。
为什么输出不正确?
请帮帮我。
先谢谢。< br $>
Ankit Agarwal
软件工程师
将行更改为;
Console.WriteLine(添加两个整数:+(a1 + a2));
否则第一次添加发生在Adding Two Integer:和a1
之间,结果是另一个字符串
。然后当你将a2
添加到你再连接时。
希望这会有所帮助,
Fredrik
试试这个
int c = a1 + a2;
Console.WriteLine( 添加两个整数: + c);
Hello,
What is the error of my overloading example?
namespace MethodOverloading
{
class Program
{
public class TestOverloading
{
public void Add(string a, string b)
{
Console.WriteLine("Adding Two String :" + a + " " + b);
}
public void Add(int a1, int a2)
{
Console.WriteLine("Adding Two Integer :" + a1 + a2);
}
}
static void Main(string[] args)
{
TestOverloading obj = new TestOverloading();
obj.Add("Ankit", "Agarwal");
obj.Add(10,5);
Console.ReadLine();
}
}
}
Output is coming:-
Adding Two String :Ankit Agarwal
Adding Two Integer :105
I want to add two integer value.
why is not coming correct output?
Please help me.
Thanks in Advance.
Ankit Agarwal
Software Engineer
Change the line to;
Console.WriteLine("Adding Two Integer :" + (a1 + a2));
Otherwise the first addition that takes place is between "Adding Two Integer :" anda1
, the result of which is anotherstring
. Then when you adda2
to that you're concatenating again.
Hope this helps,
Fredrik
Try this
int c = a1 + a2; Console.WriteLine("Adding Two Integer :" + c);
这篇关于我的重载示例有什么错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!