加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 08:38:55 所属栏目:百科 来源:网络整理
导读:就像是 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 oneLet
就像是
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(与往常一样).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读