导入 C# Class
Import C# Class
我是 C# 的(非常)新手,我需要知道如何导入 classes。
在 Java 中,我只能说:
package com.test;
class Test {}
然后在其他一些 class:
package com.test2;
import com.test.Test;
我如何在 C# 中执行此操作?
在 C# 中使用 using
指令导入命名空间:
using com.test;
但是,目前无法导入 class。导入 classes,但是 是 一项正在 introduced in C# 6 的新功能(将随 Visual Studio 2015 一起提供)。
在 C# 中,命名空间是 Java 包的半等价物。要声明命名空间,你只需要做这样的事情:
namespace com.test
{
class Test {}
}
如果 class 在单独的程序集中声明(例如 class 库),仅添加 using
指令是不够的。您还必须添加对其他程序集的引用。
我不是来自 Java 背景,但我来自 Python、C/C++ 背景,我总是使用诸如“导入”或“#”之类的东西include”,总觉得挺简单的。
但我想分享我所学到的,让我之后的其他人不必经历同样的困惑。举个例子,我将展示如何创建一个 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 文档以供参考:https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.hashalgorithm?view=net-5.0。
我是 C# 的(非常)新手,我需要知道如何导入 classes。
在 Java 中,我只能说:
package com.test;
class Test {}
然后在其他一些 class:
package com.test2;
import com.test.Test;
我如何在 C# 中执行此操作?
在 C# 中使用 using
指令导入命名空间:
using com.test;
但是,目前无法导入 class。导入 classes,但是 是 一项正在 introduced in C# 6 的新功能(将随 Visual Studio 2015 一起提供)。
在 C# 中,命名空间是 Java 包的半等价物。要声明命名空间,你只需要做这样的事情:
namespace com.test
{
class Test {}
}
如果 class 在单独的程序集中声明(例如 class 库),仅添加 using
指令是不够的。您还必须添加对其他程序集的引用。
我不是来自 Java 背景,但我来自 Python、C/C++ 背景,我总是使用诸如“导入”或“#”之类的东西include”,总觉得挺简单的。
但我想分享我所学到的,让我之后的其他人不必经历同样的困惑。举个例子,我将展示如何创建一个 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 文档以供参考:https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.hashalgorithm?view=net-5.0。