【问题标题】:Namespace error while defining a BitVector32 collection in C#在 C# 中定义 BitVector32 集合时出现命名空间错误
【发布时间】:2020-11-23 06:52:31
【问题描述】:

我在 Visual Studio 2019 中编写了以下代码,但它给了我一个错误,说 BitVector32 是一个命名空间,但在此处用作类型,而 CreateMask() 方法不是存在于 BitVector32 命名空间中

using System;
using System.Collections.Specialized;

namespace BitVector32
{
    class Program
    {
        static void Main(string[] args)
        {
            basicVector();
        }

        public static void basicVector()
        {

            BitVector32 b = new BitVector32(0);

            int myBit1 = BitVector32.CreateMask();
            int myBit2 = BitVector32.CreateMask(myBit1);
            int myBit3 = BitVector32.CreateMask(myBit2);
            int myBit4 = BitVector32.CreateMask(myBit3);
            int myBit5 = BitVector32.CreateMask(myBit4);

        }

    }
}

我在https://docs.microsoft.com/en-us/dotnet/api/system.collections.specialized.bitvector32?view=netcore-3.1 引用了 Microsoft 文档并做了同样的事情,但它给出了上述错误

【问题讨论】:

  • 您会在此处看到程序的第 4 行,在那里您将名称 BitVector32 重用为命名空间。这就是编译器在 System.Collections.Specialized 中收集同名之前的发现 - 尝试在命名项目/命名空间时更具创意。

标签: c# collections namespaces visual-studio-2019 bitvector


【解决方案1】:

这是因为顶部的命名空间是 BitVector32。将命名空间更改为 BitVector32 以外的其他名称:

using System;
using System.Collections.Specialized;

namespace SomethingOtherThanBitVector32
{
    class Program
    {
        static void Main(string[] args)
        {
            basicVeector();
        }

        public static void basicVeector()
        {

            BitVector32 b = new BitVector32(0);

            int myBit1 = BitVector32.CreateMask();
            int myBit2 = BitVector32.CreateMask(myBit1);
            int myBit3 = BitVector32.CreateMask(myBit2);
            int myBit4 = BitVector32.CreateMask(myBit3);
            int myBit5 = BitVector32.CreateMask(myBit4);

        }

    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-09
    • 1970-01-01
    • 1970-01-01
    • 2015-02-28
    • 2019-02-21
    • 2011-11-26
    • 1970-01-01
    相关资源
    最近更新 更多