【发布时间】:2017-04-03 12:31:40
【问题描述】:
今天我正在研究 c sharp 并且我正在尝试静态类,但它似乎对我不起作用,我很想知道解决方案。 我已经在网上浏览了一段时间,但似乎找不到答案。
这是我的代码:
class Count
{
public static int sum(int add1, int add2)
{
int result = add1 + add2;
return result;
}
}
class Program
{
static void Main(String[] args)
{
Console.WriteLine("Adding: \nPlease enter the first number");
int num1 = int.Parse(Console.ReadLine());
Console.WriteLine("Please enter the second number");
int num2 = int.Parse(Console.ReadLine());
Count add = new Count();
int total = add.sum(num1, num2);
Console.WriteLine("The sum is {0}.", total);
Console.ReadLine();
}
}
【问题讨论】:
-
使用
Count.sum而不是实例。 -
您会看到此错误,因为
sum是static。遵循 dcg 的搜索结果推荐。
标签: c#