c#连接(引用)一对*.cs文件
本文关键字:cs 文件 一对 连接 引用 | 更新日期: 2023-09-27 18:15:33
我有几个关于在两个或多个*.cs文件之间引用方法/变量的问题。我知道有类似的话题,但我还是不太明白是怎么回事。
我使用的是Visual Studio Community 2015。
问题就在这里。我有两个文件,它们是First.cs和Second.cs。它们被保存在硬盘上完全不同的已知位置。
Inside First.cs file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Forum
{
class First
{
static void Main(string[] args)
{
}
public int GiveMeNumber()
{
return 5;
}
}
}
Inside Second.cs文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Forum
{
class Second
{
int number = // method from file First.cs
}
}
我如何访问方法GiveMeNumber()从First.cs在Second.cs为int数分配?我如何告诉我的编译器这些文件在哪里?
谢谢你的帮助
就像Alex在评论中说的。
您可以创建解决方案/项目,添加现有项,并浏览到您的first.cs和second.cs。用公共关键字
标记这两个文件例如:namespace Forum
{
public class Second
{
int number = // method from file First.cs
}
}
那么这两个类可以在彼此内部使用。你可以输入
var first = new First();
var number = first.GiveMeNumber();
你可能想用另一种方式来做,因为我认为你有一个控制台应用,你的First类有一个main-method。
that help>?