如何在命名空间中使用语句更好? [英] How it is better having using statements inside namespace?

查看:44
本文介绍了如何在命名空间中使用语句更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解如何在命名空间内使用语句更好。

#Use statements in namespace



 命名空间 Demo_Console.Account 
{
使用系统;

public class 数学
{
public static string Min( int a, int b)
{
return 你好我是自定义;
}


}
}







#在名称空间外使用语句

 使用系统; 
命名空间 Demo_Console.Account
{


public class 数学
{
public static string Min( int a, int b)
{
return < span class =code-string>你好我是自定义;
}

public static string Print()
{
return (Math.Min( 10 10 ));
}
}
}









这有什么不同?



我的尝试:



使用命名空间内部或外部的语句

解决方案

c# - 'using'语句应该在命名空间的内部还是外部? [ ^ ]

除了解决方案1中提到的内容之外,此更改的性能差异也很大(或非常小)。因为编译器在编译之后,编译器负责何时如何,因为优化。命名空间的概念可以追溯到C ++的时代,其中C中的命名空间非常复杂,并且您将拥有struct或union等的命名空间.C ++引入了可以按需创建的命名空间。 C#使用相同的概念,并允许您将类型映射到特定的命名空间,或查找其中的对象。



同样,在C#中,首先检查类型在本地,然后检查任何名称空间以包括该类型。因此,命名空间(或类)中的命名空间只意味着此包引用仅在类范围内可用。同一文件中的其他类或命名空间不引用它,因此它们将使用程序员提供的类型; 查看解决方案1提供的SO线程上的示例



Scott Hanselman在他的博客文章中非常巧妙地解释了这一点,返回基础 - 使用指令的命名空间是否会影响程序集加载? - Scott Hanselman [ ^ ]

I am trying to understand how using statement inside namespace is better.
#Using statements inside namespace

namespace Demo_Console.Account
{
    using System;

    public class Math
    {
        public static string Min(int a, int b)
        {
            return "Hello I am custom";
        }

        
    }
}




#Using statements outside namespace

using System;
namespace Demo_Console.Account
{
    

    public class Math
    {
        public static string Min(int a, int b)
        {
            return "Hello I am custom";
        }

        public static string Print()
        {
            return (Math.Min(10, 10));
        }
    }
}





How does this make difference??

What I have tried:

using statements inside or outside namespace

解决方案

Clearly explained in this c# - Should 'using' statements be inside or outside the namespace?[^]


Apart from what was mentioned in Solution 1, there is significantly no (or very less) difference in performance for this change as well. Because, one way or the other, compiler takes care of "when" and "how" after compilation, because of optimization. The concept of namespace span back to the days of C++, where the namespaces in C were quite "complex", and you would have a namespace of struct, or union etc. C++ introduced the namespaces, that can be created on demand. C# uses the same concept, and allows you to map the types to a particular namespace, or find the objects within.

Likewise, in C#, the types are first checked up locally, and then any namespaces are checked to include the type. So, a namespace within the namespace (or class) would only mean that this package reference is only available within the scope of the class. The other classes, or namespaces within the same file do not reference it, and thus they would use the types provided by programmer; see the example on the SO thread that Solution 1 provided.

Scott Hanselman explained this pretty much neatly in his blog post, Back to Basics - Do namespace using directives affect Assembly Loading? - Scott Hanselman[^]


这篇关于如何在命名空间中使用语句更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆