C#3.0中,是有可能隐含的经营者添加到字符串类? [英] In c# 3.0, is it possible to add implicit operators to the string class?

查看:131
本文介绍了C#3.0中,是有可能隐含的经营者添加到字符串类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 公共静态类StringHelpers 
{
公共静态字符第一(这串P1 )
{
返回P1 [0];
}

公共静态隐运营商的Int32(这个字符串s)//这不起作用
{
返回Int32.Parse(S);
}
}



这样:

 字符串str =123; 
焦炭oneLetter = str.first(); // oneLetter ='1'

INT答案= str中; //无法隐式转换...


解决方案

没有,有没有这样的事情作为延期运营商(或属性等) - 唯一的扩展的方法



C#团队考虑它 - 有各种有趣的事情人们可以做(想象延伸构造函数) - 但它不是在C#3.0或4.0。请参见埃里克利珀对博客更多的信息(一如既往)。


something like

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);
    }
}

so :

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

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

解决方案

No, there's no such thing as extension operators (or properties etc) - only extension methods.

The C# team have considered it - there are various interesting things one could do (imagine extension constructors) - but it's not in C# 3.0 or 4.0. See Eric Lippert's blog for more information (as always).

这篇关于C#3.0中,是有可能隐含的经营者添加到字符串类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆