[英]How to import files in C#?
I'm writing a simple C# program. 我正在编写一个简单的C#程序。 I have two files, ClassA.cs
and ClassB.cs
. 我有两个文件, ClassA.cs
和ClassB.cs
。
Each of these files implements its respective class. 这些文件中的每一个都实现其各自的类。
I want to create an object of ClassB inside a method of ClassA. 我想在ClassA的方法内创建ClassB的对象。
My code looks like this: 我的代码如下所示:
ClassA.cs ClassA.cs
using System;
public class ClassA {
public static void Main(string[] args) {
Console.WriteLine("Starting...");
ClassA classA = new ClassA();
}
public ClassA() {
ClassB classB = new ClassB();
}
}
ClassB.cs ClassB.cs
using System;
public class ClassB {
public ClassB()
{
Console.WriteLine("ClassB created.");
}
}
How can I compile this project now? 现在如何编译该项目? When I try to do csc ClassA.cs ClassB.cs
I get the error The type or namespace name 'ClassB' could not be found
. 当我尝试做csc ClassA.cs ClassB.cs
,出现错误csc ClassA.cs ClassB.cs
The type or namespace name 'ClassB' could not be found
。
I'm guessing I'm missing something like an import
statement from Java or include
from C++. 我猜想我缺少像Java的import
语句或C ++的include
类的东西。 What's the equivalent in C#? C#中的等效功能是什么?
Also, I'm not using an IDE. 另外,我没有使用IDE。 Only writing the files in a text editor and manually compiling / running in the console (windows). 仅在文本编辑器中写入文件,并在控制台(Windows)中手动编译/运行。
I created 2 files in the same folder ( c:\\code\\egcsc
): ClassA.cs
and ClassB.cs
, with the respective contents you have shown. 我在同一文件夹( c:\\code\\egcsc
)中创建了2个文件: ClassA.cs
和ClassB.cs
,其中包含各自显示的内容。
Then: 然后:
c:\code\egcsc>dir
Volume in drive C is (irrelevant)
Volume Serial Number is (irrelevant)
Directory of c:\code\egcsc
28/03/2018 12:26 <DIR> .
28/03/2018 12:26 <DIR> ..
28/03/2018 12:24 253 ClassA.cs
28/03/2018 12:25 126 ClassB.cs
2 File(s) 379 bytes
2 Dir(s) (irrelevant) bytes free
c:\code\egcsc>csc ClassA.cs ClassB.cs
Microsoft (R) Visual C# Compiler version 2.8.0.62716 (e205ae18)
Copyright (C) Microsoft Corporation. All rights reserved.
c:\code\egcsc>ClassA.exe
Starting...
ClassB created.
So; 所以; it worked fine; 工作正常; you need to double-check what you have. 您需要仔细检查所拥有的。 Note: using a tool like VSCode or Visual Studio (there are free versions) would make this a lot easier... 注意:使用VSCode或Visual Studio之类的工具(有免费版本)会使此操作变得更加容易...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.