【问题标题】:A value of type 'int' cannot be used as a default parameter because there are no standards conversions to type C#'int' 类型的值不能用作默认参数,因为没有标准转换为 C# 类型
【发布时间】:2013-12-13 00:18:46
【问题描述】:

“'int' 类型的值不能用作默认参数,因为没有标准转换为类型 'Reality.Game.Rooms.RoomActorType'”我在 C#.exe 中遇到错误。

错误的行:

 public RoomActor GetActorByReferenceId(int ReferenceId, RoomActorType ReferenceType =    1)
    {
        lock (this.mActors)
        {
            foreach (RoomActor actor in this.mActors.Values)
            {
                if ((actor.Type == ReferenceType) && (actor.ReferenceId == ReferenceId)) /* line of error */
                {
                    return actor;
                }
            }
        }
        return null;
   }

Here's Reality>Game>Rooms>RoomActorType.cs:

namespace Reality.Game.Rooms
{
using System;

public enum RoomActorType
  {
    AiBot = 2,
    UserCharacter = 1,
  }
}

谢谢!

【问题讨论】:

  • 在这里工作正常。你能显示编译器显示为错误的确切行吗?
  • 我们可以有 RoomActor 类吗?

标签: c#


【解决方案1】:
RoomActorType ReferenceType = 1)

应该是

RoomActorType ReferenceType = RoomActorType.UserCharacter)

或者如果你真的想使用 int 做:

RoomActorType ReferenceType = (RoomActorType)1)

你不能在不强制转换的情况下使用 int 作为枚举

【讨论】:

    【解决方案2】:

    更改您的方法签名:

    public RoomActor GetActorByReferenceId(int ReferenceId, RoomActorType ReferenceType = RoomActorType.UserCharacter)
    

    【讨论】:

    • 前 10 分钟不让我,否则我会。
    猜你喜欢
    • 2015-12-20
    • 2016-11-24
    • 1970-01-01
    • 1970-01-01
    • 2016-11-14
    • 2021-12-24
    • 1970-01-01
    • 2023-03-14
    • 2019-05-12
    相关资源
    最近更新 更多