导入C#类

导入C#类,c#,import,C#,Import,我(非常)不熟悉C#,我需要知道如何导入类 在Java中,我可以说: package com.test; class Test {} 然后在另一节课上: package com.test2; import com.test.Test; 如何在C#中实现这一点?导入名称空间是在C#中通过using指令完成的: using com.test; 但是,目前无法导入类。然而,导入类是一项正在开发的新功能(将随Visual Studio 2015提供) 在C#中,名称空间是Java包的半等价物。要声

我(非常)不熟悉C#,我需要知道如何导入类

在Java中,我可以说:

package com.test;
class Test {}
然后在另一节课上:

package com.test2;
import com.test.Test;

如何在C#中实现这一点?

导入名称空间是在C#中通过
using
指令完成的:

using com.test;
但是,目前无法导入类。然而,导入类是一项正在开发的新功能(将随Visual Studio 2015提供)

在C#中,名称空间是Java包的半等价物。要声明名称空间,只需执行以下操作:

namespace com.test
{
    class Test {}
}

如果类是在单独的程序集中声明的(例如类库),仅使用指令添加
是不够的。您还必须添加对另一个程序集的引用。

我不是来自Java背景,但我来自Python、C/C++背景,在那里我总是使用“导入”或“包含”之类的东西,而且感觉总是很简单

但我想分享我学到的东西,这样我之后的其他人就不必经历同样的困惑。为了给出一个示例,我将演示如何创建一个SHA256实例

using System.Security.Cryptography; //this is the namespace, which is what you import; this will be near the top of documentation you read
using static System.Security.Cryptography.SHA256; //this is an additional class inside the namespace that you can use; this will be in the "derived" section of documentation

AlgorithmHash sha = SHA256.Create(); //I am not entirely sure why, but the declaration needs to be on the same line as the instantiation

我附上了HashAlgorithm文档以供参考:.

在C中,导入的是名称空间,而不是类。如果您正在使用的类位于同一命名空间中,则无需执行任何操作。如果它位于不同的命名空间中,并且您正在使用Visual Studio,则只需键入类的名称,然后使用智能标记下拉列表将相应的
using
添加到类文件的顶部。有关如何包含名称空间,请参阅现有的
using
语句。可能与@Jashaszun重复,我不理解,抱歉。它可能非常非常简单,我只是太笨了,无法理解。通过C#获取CLR,跳过前两章,然后阅读。这会花你一个下午的时间,你会因此得到1000%的好处。@Will谢谢。可以。那么如何在C#中声明包呢?using语句用于
IDisposable
模式。这被称为使用指令。这个答案可能与此相关,我不确定