微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在c#3.0中,是否可以在字符串类中添加隐式运算符?

就像是
public static class StringHelpers
{
    public static char first(this string p1)
    {
        return p1[0];
    }

    public static implicit operator Int32(this string s) //this doesn't work
    {
        return Int32.Parse(s);
    }
}

所以:

string str = "123";
char oneLetter = str.first(); //oneLetter = '1'

int answer = str; // Cannot implicitly convert ...

解决方法

不,没有扩展操作符(或属性等) – 只有扩展方法.

C#团队已经考虑过了 – 可以做各种有趣的事情(想象扩展构造函数) – 但它不是在C#3.0或4.0中.有关详细信息,请参阅Eric Lippert’s blog(与往常一样).

原文地址:https://www.jb51.cc/csharp/244346.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐