Errors CS0116 and CS0246

本文关键字:CS0246 and CS0116 Errors | 更新日期: 2023-09-27 18:14:09

我正在构建一个简单的类,它允许我计算一个类的房间的尺寸,我在让代码工作时遇到了麻烦。当我运行它时,这些是我收到的错误。

/p1/room.cs(1,7):错误CS0116:命名空间不能直接包含字段或方法等成员

/p1/room.cs(48,19): warning CS0108: 'Room.GetType()'隐藏继承的成员'object.GetType()'。如果要隐藏,请使用new关键字。

/p1/room.cs(1,1):错误CS0246:类型或命名空间名称'Using'无法找到(您是否缺少Using指令或程序集引用?)

我做了一些研究,发现似乎大多数时候上述两个错误都是指不匹配的括号,但在搜索我的room.cs文件后,我找不到任何错误。在比较头文件和其他类时,我发现我找不到任何差异。

这是我的房间。cs文件

Using System;
namespace p1
{
    public class Room
    {
       private string type;
       private double length;
       private double width;
       private double height;
       public Room()
       {
            type = "Default";
            length = 0.0;
            width = 0.0;
            height = 0.0;
       }
       public Room(string t, double l, double w, double h)
       {
            type = t;
            length = l;
            width = w;
            height = h;
       }

       public void SetType(string t)
       {
            type = t;
       }
       public void SetLength(double l)
       {
            length = l;
       }
       public void SetWidth(double w)
       {
            width = w;
       }
       public void SetHeight(double h)
       {
            height = h;
       }
       public string GetType()
       {
            return type;
       }
       public double GetLength()
       {
            return length;
       }
       public double GetWidth()
       {
            return width;
       }
       public double GetHeight()
       {
            return height;
       }

       public double GetArea()
       {
            return length*width;
       }
       public double GetVolume()
       {
            return length*width*height;
       }
       public void Display()
       {
            Console.WriteLine("Room Type: " + this.GetType());
            Console.WriteLine("Room Length: " + this.GetLength());
            Console.WriteLine("Room Width: " + this.GetWidth());
            Console.WriteLine("Room Height: " + this.GetHeight());
            Console.WriteLine("Room Area: " + this.GetArea().ToString("F 2") + " sq ft " );
            Console.WriteLine("Room Volume: " + this.GetVolume().ToString("F 2") + " cu ft ");
       }
   }
}

如果需要,我也可以发布program.cs文件,但这变得相当长,我不想让它不可读。

Errors CS0116 and CS0246

使用NameSpace的正确语法应该是using not using

using System;代替Using System;

使用

public new string GetType()
{
    return type;
}

替换警告"Use new keyword is hiding was intended "

public string GetType()
{
    return type;
}

除了vivek nuna已经说过的,你应该习惯c#的属性概念。这将使您的代码更简洁,并避免隐藏GetType():

的特定问题。
public class Room
{
   public string Type { get; set; } = "Default"; // with C#6 property initialization
   public double Length { get; set; }
   public double Width { get; set; }
   public double Height { get; set; }
   public Room() {} // no code here, Type is initalized, double is 0 by default
   public Room(string t, double l, double w, double h)
   {
        Type = t;
        Length = l;
        Width = w;
        Height = h;
   }
   public double GetArea()
   {
        return Length * Width;
   }
   public double GetVolume()
   {
        return Length * Width * Height;
   }
   public void Display()
   {
        Console.WriteLine("Room Type: " + Type);
        Console.WriteLine("Room Length: " + Length);
        Console.WriteLine("Room Width: " + Width);
        Console.WriteLine("Room Height: " + Height);
        Console.WriteLine("Room Area: " + GetArea().ToString("F 2") + " sq ft " );
        Console.WriteLine("Room Volume: " + GetVolume().ToString("F 2") + " cu ft ");
    }
}

现在可以从外部简单地访问属性:

Room r = new Room();
r.Height = 12;
Console.WriteLine(r.Height);

编译器会完成你自己在代码中完成的所有工作。它为每个属性以及getter和setter方法创建后备字段。你不需要做这些,可以专注于真正的工作。