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

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

概述就像是 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 就像是
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(与往常一样).

总结

以上是内存溢出为你收集整理的在c#3.0中,是否可以在字符串类中添加隐式运算符?全部内容,希望文章能够帮你解决在c#3.0中,是否可以在字符串类中添加隐式运算符?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/langs/1251119.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存