简体   繁体   English

字符串ToUpper()函数与ToString()

[英]string ToUpper() function with ToString()

I have used a string in C# where i am using C# in Visual studio 2008. I wanted to convert it to uppercase. 我在C#中使用了一个字符串,我在Visual Studio 2008中使用C#。我想将它转换为大写。

string lowerString = txtCheck.Text;
string upperString = lowerString.ToUpper();

Normally this is how i should have used, But the thing is i didn't get any error when i used it like this 通常这是我应该如何使用,但事情是我没有得到任何错误,当我这样使用它

string upperString = lowerString.ToUpper().Tostring();

Now i am confused that ToUpper() is also a function, then how can i use the second syntax where i again use ToUpper().Tostring(); 现在我很困惑ToUpper()也是一个函数,那我怎么能用第二种语法再次使用ToUpper()。Tostring(); . I mean It would mean Function1().Function2() . 我的意思是它意味着Function1()。Function2()

No, you're calling ToString on the object returned by ToUpper . 不,你在ToUpper的对象上调用ToString This is pointless, but it's not a compilation error. 这没有意义,但这不是编译错误。 If you did: 如果你这样做:

lowerString.ToUpper.ToString();

that will indeed give you an error, since you can't call a method (ToString) on a method group. 这确实会给你一个错误,因为你不能在方法组上调用方法(ToString)。

ToUpper() is a function that takes a string and returns another string, so you're OK just doing: ToUpper()是一个接受字符串并返回另一个字符串的函数,所以你可以这样做:

string upperString = txtCheck.Text.ToUpper();

No need to call ToString() at all. 根本不需要调用ToString()。

Think of it as: 把它想象成:

string upperString = (lowerString   .ToUpper())   .ToString();

In other words, the thing that get returned from lowerString.ToUpper() is having ToString() applied to it. 换句话说,从lowerString.ToUpper()返回的东西是应用了ToString() That's redundant since it's already a string but it's by no means an error. 这是多余的,因为它已经是一个字符串,但它绝不是一个错误。

It's no different to some other languages where the equivalent would be: 它与其他语言没有区别,相当于:

upperString = toString (toUpper (lowerString));

In fact you can do all sorts of weird things like: 事实上,你可以做各种奇怪的事情,如:

string upper = lower.ToUpper().ToLower().ToUpper().ToString().ToString();

although that monstrosity should never get past a code review :-) 虽然那个怪物永远不会超过代码审查:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 延迟执行linq c#降低toupper tostring - Deferred execution linq c# Tolower toupper tostring 如何在String.ToUpper()中忽略撇号? - How To ignore apostrophe in String.ToUpper()? 如何在重写的ToString函数中返回复合字符串? - How do I return a compound string in an overridden ToString function? 使用 String.ToUpper() 时程序退出; 在一个有空格的字符串上 - Program exiting when using String.ToUpper(); on a string that has spaces in it 就C#的性能ToUpper或SQL中的Upper函数而言,哪个更好 - Which is better in terms of performance ToUpper of C# or Upper function in SQL 使用ToUpper函数时,列表元素不会更改 - List elements doesn't change when using ToUpper function 使用InvariantCultureIgnoreCase而不是ToUpper进行不区分大小写的字符串比较 - Using InvariantCultureIgnoreCase instead of ToUpper for case-insensitive string comparisons 没有toUpper / toLower的最有效的大写/小写字符串的方法 - Most efficient way to uppercase/lowercase a string without toUpper/toLower 替换string.ToUpper()与StringComparison或类似,完全保留行为 - Alternative to string.ToUpper() with StringComparison or similar, that fully preserve behavior RegEx.IsMatch()与String.ToUpper()。包含()性能 - RegEx.IsMatch() vs. String.ToUpper().Contains() performance
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM