C#类库和控制台应用程序
我创建了这个类库。我遇到了让库使用控制台应用程序的问题。这只是我正在做的一项基本任务,但我不知道我哪里做错了。这是我的档案: 类库:C#类库和控制台应用程序,c#,visual-studio,C#,Visual Studio,我创建了这个类库。我遇到了让库使用控制台应用程序的问题。这只是我正在做的一项基本任务,但我不知道我哪里做错了。这是我的档案: 类库: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Test2 { public class Test { public static
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test2
{
public class Test
{
public static string GetData(String prompt)
{
Console.WriteLine(prompt);
return Console.ReadLine();
}
public static int GetInt()
{
return Convert.ToInt32(GetData("Enter an interger"));
}
public static void put(String output)
{
Console.WriteLine(output);
}
public static void put(String output, String heading)
{
put(heading + '\n' + output);
}
public static void Main(String[] args)
{
int x = GetInt();
int y = GetInt();
if (x > 0 && y > 0) put(String.Format("Sum:{0}", x + y));
}
}
}
控制台应用程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Test2;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Test myTest = new Test();
Test.GetData(prompt);
}
}
}
int x = Test.GetInt();
int y = Test.GetInt();
if (x > 0 && y > 0) Test.put(String.Format("Sum:{0}", x + y));
Console.Read();
我想我的主要问题是调用在控制台应用程序中运行的方法。无论我读多少书,我似乎都不知道自己做错了什么
如果您在类库中定义的main方法中的功能是您为整个应用程序所要实现的,那么请考虑将该功能移到控制台应用程序中的主方法。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Test2;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Test myTest = new Test();
Test.GetData(prompt);
}
}
}
int x = Test.GetInt();
int y = Test.GetInt();
if (x > 0 && y > 0) Test.put(String.Format("Sum:{0}", x + y));
Console.Read();
我建议在这里阅读静态类和静态类成员:
当决定创建一个静态类时,我总是从上面的链接返回此引用:
静态类可以用作一个方便的容器,用于
只对输入参数进行操作而不必获取
或设置任何内部实例字段
我也会考虑更改你的测试类的名字。即使在像这样的小测试应用程序中,我发现使用定义良好的有意义的名称也是有用的。这是一个很好的实践,因此您可以在更大的应用程序中自动执行
最后,祝你学习顺利 现在的问题是什么?我的主要问题是试图让我在类库中创建的方法在控制台应用程序中启动。