有关函数调用的一道C#编程题!求解!

ly352203199109111517 2011-04-14 11:05:17
使用重载编写方法Add,用于实现将两个输入参数相加的结果返回。如果是整数,则将这俩个数相加,结果返回。如果是字符串,则将这两个字符串连接在一起返回。在Main方法中调用这两个不同形式的Add方法
...全文
144 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
ktei2008 2011-04-18
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 sihonglaoren 的回复:]

C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
……
[/Quote]

你这代码写的不是一般得烂,就这还教人家Generic……………………
Hamber_Bao 2011-04-15
  • 打赏
  • 举报
回复

class Program
{
static void Main()
{
//从Console接受用户数据
int num1 = int.Parse(Console.ReadLine());
int num2 = int.Parse(Console.ReadLine());
Demo demo = new Demo();
int result = demo.Add(num1,num2);
}
}

public class Demo
{
//Add方法重载
public int Add(int num1,int num2)
{
return num1 + num2;
}
public string Add(string str1,string str2)
{
return str1 + str2;
}
}
sihonglaoren 2011-04-14
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int i = 3, j = 4;
Console.WriteLine(Add<int>(i, j, (x1, x2) => x1 + x2));
string s1 = "Hello", s2 = " World!";
Console.WriteLine(Add<string>(s1, s2, (x1, x2) => x1 + x2));
}

static T Add<T>(T left, T right, Func<T, T, T> add)
{
return add(left, right);
}
}
}


lz你下面该学 Generic 了!我想你那么白痴肯定还不会,一起帮你写了。
Tim_Andy 2011-04-14
  • 打赏
  • 举报
回复
public class addhelper()
{
public int Add(int a, int b)
{
return a + b;
}

public string Add(string s1, string s2)
{
return s1 + s2;
}
}

---借用楼上的
pub static void main()
{
addhelper helper=new addhelper();
Consol.WriteLine(helper.Add(1,2).ToString());
Consol.WriteLine(helper.Add("3","4"));
}
sihonglaoren 2011-04-14
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int i = 3, j = 4;
Console.WriteLine(Add(i, j));
string s1 = "Hello", s2 = " World!";
Console.WriteLine(Add(s1, s2));
}

static string Add(string left, string right)
{
return left + right;
}

static int Add(int left, int right)
{
return left + right;
}
}
}


lz如果你下次再问这么简单的问题,真想锤你!
Peter200694013 2011-04-14
  • 打赏
  • 举报
回复
错了

这个
public int Add(int a, int b)
{
return a + b;
}

public string Add(string s1, string s2)
{
return s1 + s2;
}
Peter200694013 2011-04-14
  • 打赏
  • 举报
回复
public int Add(int a, int b)
{
return a+b;
}

public string Add(string s1, string s2)
{
retirm a + b;
}
  • 打赏
  • 举报
回复
我是初学者!希望能帮忙解决下!

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧