C# 中的默认非索引器属性

发布于 2024-12-06 09:59:37 字数 577 浏览 0 评论 0原文

如何在 C# 中创建默认的非索引器属性?

我的意思是我可以看到我可以创建索引器默认属性,如 此 MSDN 页面。

这允许我做类似的事情

Widgets widgets = new Widgets();
Widget result = widgets[1];

,但是如果我想实现类似 Nullable 所做的事情怎么办?

你可以在哪里采取

Nullable<decimal> nullDec = 1.23m;
decimal result = nullDec.Value;

OR

decimal result = (decimal)nullDec;

我认为这只是 nullDec.Value 的默认属性实现???

How can you create a default - non indexer - property in C#?

What I mean by this is I can see that I can create indexer default properties as illustrated on this MSDN page.

This allows me to do things like

Widgets widgets = new Widgets();
Widget result = widgets[1];

But what if I want to achieve something like what Nullable<T> does?

Where you can take

Nullable<decimal> nullDec = 1.23m;
decimal result = nullDec.Value;

OR

decimal result = (decimal)nullDec;

Which I assume is simply a default property implementation to nullDec.Value???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

破晓 2024-12-13 09:59:37

Nullable 在编译器中具有特殊处理,但您可以通过添加隐式或显式静态转换运算符来完成大部分操作。

例如,对于类型 Foo 您可以添加一个运算符:

public static implicit operator string(Foo value)
{
    return "abc";
}
public static implicit operator Foo(int value)
{
    ...
}

允许:

Foo foo = ..
string s = foo; // uses string(Foo value)

int i = 123;
Foo foo = i; // uses Foo(int value)

Nullable<T> has special handling in the compiler, but you can do most of that by adding implicit or explicit static conversion operators.

For example, for type Foo you can add an operator:

public static implicit operator string(Foo value)
{
    return "abc";
}
public static implicit operator Foo(int value)
{
    ...
}

allowing:

Foo foo = ..
string s = foo; // uses string(Foo value)

and

int i = 123;
Foo foo = i; // uses Foo(int value)
淡莣 2024-12-13 09:59:37

如果您检查 Nullable{T} 的代码,您将看到显式强制转换实现如下所示:

public static explicit operator T(Nullable<T> value)
{
    return &value.Value;
}

所以是的,您是对的。

If you inspect the code of Nullable{T} you will see that the explicit cast implementation is like this:

public static explicit operator T(Nullable<T> value)
{
    return &value.Value;
}

So yes you are right.

我们的影子 2024-12-13 09:59:37

Nullable 的实现方式是提供 显式转换运算符T

所以也许您正在寻找类似的东西:

public static explicit operator Widget(Widgets widgets)
{
     // Argument checks here.
     return widgets[0];     
} 

这可以让您这样做:

Widgets widgets = ..
Widget firstWidget = (Widget)widgets;

对我来说,这确实看起来像一个真正狡猾且不直观的API,所以我根本不建议这样做。为什么不只坚持使用标准索引器呢?

The way Nullable<T> does it is by providing an explicit conversion operator to T.

So perhaps you are looking for something like:

public static explicit operator Widget(Widgets widgets)
{
     // Argument checks here.
     return widgets[0];     
} 

which would let you do:

Widgets widgets = ..
Widget firstWidget = (Widget)widgets;

This does look like a really dodgy and unintuitive API to me so I don't recommend doing this at all. Why not just stick to standard indexers?

醉生梦死 2024-12-13 09:59:37

不太确定我是否正确理解你的要求。在返回类型上实现强制转换运算符来实现您想要的效果还不够吗?

如果这不是您想要的,请解释一下。

Not very sure if I correct understant what you're asking for. Wouldn't be enough to implement cast operators on returning type to achieve what you want?

If it's not what you intend, please explain.

左秋 2024-12-13 09:59:37

此类功能是编译器语法糖(在 IL 代码中,它们都转换为相同的低级代码),因此基本上在不修改 C# 编译器源代码的情况下不可能做到这一点。

Such kind of features are compiler syntactic sugar (in IL code they all are converted to same low level code), so basically it won't be possible to do this without modifying the C# compiler source.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文