What's up this this string method - Lengh?

[code]string theString = "C# Programming for Beginners";
Console.WriteLine("default: {0}", theString); Console.WriteLine("lowercase: {0}", theString.ToLower());

Console.WriteLine("uppercase: {0}", theString.ToUpper());

Console.WriteLine("replace: {0}", theString.Replace("#", "sharp"));

Console.WriteLine("length: {0}", theString.Length);

Console.WriteLine(""for" occurs at character {0}", theString.IndexOf("for"));[/code]

In the code above, why does the Length method not get two parentheses like ToLower and ToUpper?



  • Ah - sorry, was having trouble understanding what you were asking.

    In C# when you are creating a class, you can expose to the outside world a few different types of items. The two most common items on a class are called "Methods" and "Properties"

    the .ToUpper is a method. Since methods have the ability to take in multiple parameters they are written out with parens so you have somewhere to put the parameters in. in the case of ToUpper, there are no parameters - but you still use the parens to tell the compiler that you are trying to access a method - why can't it just figure out what you mean without the parens? Well i spose microsoft didn't see the extra 2 characters to be a big enough deal to have a team write in the extra logic it would take to make them optional when parsing your code.

    The .Length is actually a property (of "System.Array" I believe). Now properties do not accept parameters - they only return or receive one single value. the ".Length" property is a read-only property so you can't say something like "array.Length = 10" which wouldn't really make sense anyways, but most properties have both "getters" and "setters" so like the .Text property of a text box allows you to both read and write to the text it contains.
  • Thanks Psightoplazm, that cleared it up for me. I need to memorize the different icons in MS visual studio.net so I'll know the difference next time. Again, thanks.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!