简体   繁体   English

Visual Studio中的命名空间在哪里

[英]Where are the namespaces in visual studio

I am new to Visual Studio and C#. 我是Visual Studio和C#的新手。 I went through the tutorials on C# and all the sample programs I went through, were written inside namespaces. 我阅读了有关C#的教程,并且所经历的所有示例程序都是在名称空间中编写的。 But when I used a template on Visual Studio, all the predefined codes weren't inside namespaces. 但是,当我在Visual Studio上使用模板时,所有预定义的代码都不在名称空间内。 Shouldn't the classes in the programs be in namespaces? 程序中的类不应该在名称空间中吗? Why aren't they present by default? 为什么默认情况下不显示它们? Should I define them? 我应该定义它们吗?

Thanks! 谢谢!

[edit] Below is the code in one of the files that doesn't have a namespace. [edit]以下是其中一个没有名称空间的文件中的代码。

using System;   
using System.Web.Security;  

public partial class Login : System.Web.UI.Page  
{   
    protected void btnSubmit_Click( object sender, EventArgs e )  
    {  
        // Note: Add code to validate user name, password. This code is for illustrative purpose only.  
        // Do not use it in production environment.          
        FormsAuthentication.RedirectFromLoginPage( txtUserName.Text, false );  
    }  
}

There is a default namespace declared in your Project's properties. 在项目的属性中声明了一个默认的名称空间。 But it's a good practice to declare it explicitly. 但是,明确声明它是一个好习惯。 If your project has no folders, all classes are inside the same namespace so it is redundant. 如果您的项目没有文件夹,则所有类都在同一个命名空间中,因此它是多余的。

A namespace is designed for providing a way to keep one set of names separate from another. 命名空间旨在提供一种使一组名称彼此分离的方法。 The class names declared in one namespace does not conflict with the same class names declared in another. 在一个名称空间中声明的类名与在另一个名称空间中声明的相同类名不冲突。 Please refer https://msdn.microsoft.com/en-us/library/z2kcy19k.aspx for more details. 请参阅https://msdn.microsoft.com/en-us/library/z2kcy19k.aspx了解更多详细信息。

General 一般

Namespaces are basically just a way to group your classes and give your Project a structure. 命名空间基本上只是对类进行分组并为Project提供结构的一种方法。

Classnames must be unique for each namespace. 每个名称空间的类名必须唯一。

You can also sort of "encapsulate" your Namespaces. 您还可以“封装”您的命名空间。 For Example you can create some Namespaces SomeProject , SomeProject.Core and SomeProject.UI 例如,您可以创建一些命名空间SomeProjectSomeProject.CoreSomeProject.UI

If you wite using SomeProject. 如果您想using SomeProject. the Intellisense would now suggest Core and UI . Intellisense现在会建议CoreUI

Usasge Usasge

So to use a Class MyClass from SomeProject.Core you would have three possibilities to use it. 因此,要使用SomeProject.Core的Class MyClass ,可以使用三种方法。

  1. using Direcitve 使用Direcitve

using SomeProject.Core;

calling it with MyClass MyClass调用它

  1. Calling it with the entire Namespace 用整个命名空间调用它

SomeProject.Core.MyClass

  1. Explicite using Directive (rarely used, but maybe useful for longe namespaces) 明确使用指令(很少使用,但可能对long名称空间有用)

using AnyName = SomeProject.Core;

call MyClass with AnyName.MyClass AnyName.MyClass调用MyClass

So i guess thats it for the moment.. if i forgot anything just tell me in the comments 所以我暂时就这样。.如果我忘记了什么,只需在评论中告诉我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM