[英]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.