✅ GetLength vs .Length
the thing is .GetLength(0) doesn't need a -1 in the end while in .Length we do it like .Length-1
6 Replies
wdym, if you did a for with .Length you wouldn't use
< Length-1
oh
I feel I we use it? when we count length right?
int lngt=sentence.length-1 like this
but this is a different scenerio!
I will look to clear my confusion sorry I am newbie I sometimes...
both GetLength and Length return a "count", or a dimension, so it's the same principle
anyway again if you were to have a for on an array using Length it would be used the same as GetLength
for (int i = 0; i < ints.GetLength(0); i++)
=== for (int i = 0; i < array.Length; i++)
i don't know if this answers your questionty man I had a wrong understanding before previosly when I worked on a words counter
userInput.Length gives you the total number of characters in the userInput string.
In C#, strings are zero-indexed, meaning the first character is at index 0, and the last character is at index userInput.Length - 1.
So I just mixed this everywhere thinking it's needed at all place
it happens all the times to mix 1-based index and 0-based index code, you probably have to come with a system to not forget this
depending maybe on which you find the most intuitive
If you have no further questions, please use /close to mark the forum thread as answered
If you have no further questions, please use /close to mark the forum thread as answered