C# “命名空间”,但像“类型”一样使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15007727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:46:05  来源:igfitidea点击:

'namespace' but is used like a 'type'

c#classnamespaces

提问by TheAce

This is my program the class uses it is called Time2 I have the reference added to TimeTest I keep getting the Error 'Time2' is a 'namespace' but is used like a 'type'

这是我的程序,该类使用它称为 Time2 我已将引用添加到 TimeTest 我不断收到错误“Time2”是“命名空间”,但用作“类型”

Could someone please tell me what this error is and how to fix it?

有人可以告诉我这个错误是什么以及如何解决它吗?

namespace TimeTest
{
  class TimeTest
  {
    static void Main(string[] args)
    {
        Time2 t1 = new Time2();
    }
  }
}

采纳答案by Jon Skeet

I suspect you've got the same problem at least twice.

我怀疑你至少遇到过同样的问题两次

Here:

这里:

namespace TimeTest
{
    class TimeTest
    {
}

... you're declaring a type with the same name as the namespace it's in. Don't do that.

...您正在声明一个与它所在的命名空间同名的类型。不要那样做。

Now you apparently have the same problem with Time2. I suspectif you add:

现在你显然有同样的问题Time2。我怀疑你是否添加:

using Time2;

to your list of usingdirectives, your code will compile. But please, please, please fix the bigger problem: the problematic choice of names. (Follow the link above to find out more details of whyit's a bad idea.)

到您的using指令列表,您的代码将被编译。但是拜托,拜托,请解决更大的问题:名字的选择有问题。(点击上面的链接,了解更多关于为什么这是一个坏主意的细节。)

(Additionally, unless you're really interested in writing time-based types, I'd advise you not to do so... and I say that as someone who doesdo exactly that. Use the built-in capabilities, or a third party library such as, um, mine. Working with dates and times correctly is surprisingly hairy. :)

(此外,除非你真的对编写基于时间的类型感兴趣,否则我建议你不要这样做......我说这是一个确实这样做的人。使用内置功能,或者第三个派对图书馆,比如,嗯,我的。正确处理日期和时间是令人惊讶的毛茸茸的。:)

回答by shoba

namespace TestApplication // Remove .Controller
{
    public class HomeController : Controller
    {
       public ActionResult Index()
        {
            return View();
        }
    }
}

Remove the controller word from namepsace

从命名空间中删除控制器字

回答by Muhammad Usama Arshad

Please check that your class and namespace name is the same...

请检查您的类和命名空间名称是否相同...

It happens when the namespace and class name are the same. do one thing write the full name of the namespace when you want to use the namespace.

当命名空间和类名相同时会发生这种情况。当你想使用命名空间时,做一件事写命名空间的全名。

using Student.Models.Db;

namespace Student.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            List<Student> student = null;
            return View();
        }
    }

回答by Nelcon Croos

if the error is

如果错误是

Line 26:
Line 27: @foreach (Customers customer in Model) Line 28: { Line 29:

第 26 行:
第 27 行:@foreach(模型中的客户客户)第 28 行:{ 第 29 行:

give the full name space
like @foreach (Start.Models.customer customer in Model)

给出全名空间,
如@foreach(模型中的Start.Models.customer customer)