What namespace are my classes in when I don't put a namespace?

本小妞迷上赌 提交于 2019-12-23 18:45:34

问题


if I don't put a namespace in my classes (vs.net 2008), which namespace do my classes get built under?

update Its strange, when I add a namespace I can't seem to reference the classes methods from my user control.

If I explicitly set a namespace, and add a 'using ...' in my control, I still can't see it!

How can this be?

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
/// <summary>
/// Summary description for Globals
/// </summary>
public class Globals
{
    public Globals()
    { }


    public static string ConnectionString
    {
        get
        {
            return ConfigurationManager.ConnectionStrings["MyDb"].ConnectionString;
        }
    }

}

My control:

using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class MyStats : System.Web.UI.UserControl
{




    protected void Page_Load(object sender, EventArgs e)
    {


    //etc.

回答1:


If you do not provide a namespace then your type has no namespace. Remember that namespaces don't have any meaning post-compile. They simply get appended to the beginning of you type's name to create a longer name that has a greater probability of being unique.

Edit: I think that you may have two separate assemblies and one ought to be referencing the other but it is not. If two projects are in a single solution then the class viewer will show all types from all projects but that does not necessarily mean that ProjectA references ProjectB. In order to use the types from ProjectB in ProjectA you need to ensure that a project reference exists.




回答2:


as you said, that class will not have any namespace, It will be accessible without a using clause.

Namespaces is a feature for putting order on your classes, by classifying, mostly according to their funcionality

EDIT

According to your update, i think your "global" class maybe putting trouble because of the global:: clause... would you mind to change the name of the class just to see if it works?



来源:https://stackoverflow.com/questions/715398/what-namespace-are-my-classes-in-when-i-dont-put-a-namespace

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!