C# 4.0 可选参数 - 如何指定“Guid”类型的可选参数?
这是我的方法:
public void SomeQuery(string email = "", Guid userId = Guid.Empty)
{
// do some query
}
userId
给了我一个错误,因为它必须是一个编译时常量,这是我理解的。但即使我声明一个 const:
private const emptyGuid = Guid.Empty;
然后将方法签名更改为:
public void SomeQuery(string email = "", Guid userId = emptyGuid)
{
// do some query
}
仍然没有爱。
我缺少什么?
Here's my method:
public void SomeQuery(string email = "", Guid userId = Guid.Empty)
{
// do some query
}
userId
is giving me an error as it must be a compile-time constant, which i understand. But even when i declare a const:
private const emptyGuid = Guid.Empty;
then change the method signature to:
public void SomeQuery(string email = "", Guid userId = emptyGuid)
{
// do some query
}
still no love.
What am i missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否尝试过将其设置为
Guid
的新实例 ala:应该可以解决问题。
Have you tried setting it to a new instance of
Guid
ala:Should do the trick.
更好的解决方案不是使用不需要 Guid 的版本来重载该方法吗?这将解决问题,并且在我看来将是更好的设计。当然,可能还有其他我不知道的限制需要这种设计。
Wouldn't a better solution be to overload the method with a version that doesn't require the Guid? This would solve the issue, and would be a better design in my opinion. Of course, there may be other constraints that I am unaware of necessitating this design.
也许它会有所帮助(使用运算符 ?? 和 Guid 可空类型)
maybe it would help (using operator ?? and Guid nullable type)