什么使特定的使用指令不必要? [英] What makes a particular using directive unnecessary?
问题描述
我有一个包含多个项目的解决方案。
项目A具有名称空间1,它使用特定的类X并具有使用指令。该项目编译。
项目B具有类2(在名称空间2中),它使用相同的类X但不编译,因为"名称X不存在于当前的背景"。如果我在第1课中包含using指令,我会得到"使用指令是不必要的",因为项目A和B有一些不同之处,需要使用指令,而不是。
$
我的解决方案显然存在多个问题,但首先我想了解是什么使得使用指令成为必要。我在哪里可以开始寻找?
项目A:
 使用Microsoft.WindowsAzure; // CloudConfigurationManager在这里
使用MyNamespace.MyClass; //常数在这里
 
 命名空间n1
{
     private static string resourceId = CloudConfigurationManager.GetSetting(" id");Console.WriteLine($" {resourceId} {Constants.Blah}(" blah")";
}
项目B:
使用Microsoft.WindowsAzure; //使用MyNamespace.MyClass指令不必要
; //指令不必要
命名空间测试
{
class Program
{
public static void Main(string [] args)
{
string resourceId = CloudConfigurationManager.GetSetting(" id"); //名称CloudConfigurationManager在当前上下文中不存在
Console.WriteLine($" {resourceId} {Constants.Blah}(" blah")" ;; //名称Constants在当前上下文中不存在
}
}
}
命名空间是l代码库的逻辑划分。例如,我们可能希望将特定类型分组到一个逻辑容器中。像通常一样,我们有一个Common调用,它包含我们解决方案中所有
其他项目中最常使用的实现。
现在,当我们想要使用特定类或它在该命名空间中的方法时,我们需要添加对该程序集的引用,然后在我们想要的类中使用
using namespace 重新使用。
以下链接可能有助于理解这些概念:
https://docs.microsoft.com/en-us/dotnet/csharp/programming -guide / namespaces /
这对阅读非常有帮助:
NameSpaces
- Albahari在Nutshell中的C#
I have a solution with multiple projects.
Project A has namespace 1 which uses a particular class X and has a using directive for it. That project compiles.
Project B has class 2 (in namespace 2) which uses the same class X but does not compile because "The name X does not exist in the current context". If I include the using directive from class 1 I get "Using directive is unnecessary"
So there is something different about project A and B, where one needs the using directive and one doesn't.
There are clearly multiple issues with my solution here but first I want to understand what makes a using directive necessary or not. Where can I start looking?
Project A:
using Microsoft.WindowsAzure; //CloudConfigurationManager is here
using MyNamespace.MyClass; //Constants is here
namespace n1
{
private static string resourceId = CloudConfigurationManager.GetSetting("id");Console.WriteLine($"{resourceId}{Constants.Blah}("blah")";
}
Project B:
using Microsoft.WindowsAzure; //directive unnecessary using MyNamespace.MyClass; //directive unnecessary namespace Testing { class Program { public static void Main(string[] args) { string resourceId = CloudConfigurationManager.GetSetting("id"); // The name CloudConfigurationManager does not exist in the current context Console.WriteLine($"{resourceId}{Constants.Blah}("blah")"; // The name Constants does not exist in the current context } } }
Namespaces are logical division of out code base. For Example, we might want to group our particular types in one logical container. Like normally we have a Common called which contains the implementations that are most often reused in all other projects in our solution.
Now when we want to use a particular class or it's methods that are in that namespace we would need to add reference to that assembly and then use the using namespace in the class where we want to re-use.
The following links might be helpful in grasping the concept of these:
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/namespaces/
and this would be quite helpful to read:
这篇关于什么使特定的使用指令不必要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!