Skip to content
View in the app

A better way to browse. Learn more.

LCPDFR.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

property Length v. Method Count() in Class Array

Featured Replies

In the array class we have method Count() and property Length.
Afaic see both returns the initial value set from array-definition:
Ped[] arr = new Ped[10]; // set mem for 10 Peds in datastructure arr
arr.Length readout 10
arr.Count() readout 10
imo Count() should readout the actual number of inserted Peds in arr, and not the number of available slots in array
It does not look like there is a variable that keeps track of number of inserted elements in array
Is that meant to be like that?

See my plugin here:
https://www.youtube.com/watch?v=peqSXuTfIyY

Let me know if you find it interesting.
Best Regards.

  • Management Team

Correct, there is no property that keeps track of how many elements there are in an array. You could count them yourself or defer to a list if that is something that is important. As for Count(), that is probably the Linq extension method that you are seeing that works via Enumerable.Count<T>(IEnumerable<T>). Since array is also an IEnumerable, this method is also available. Internally, it will just call Length, though, so there is no benefit of using it. Depending on the underlying data type, it will have to iterate over all elements to figure out the length so it can be expensive to use.

Please do not PM me unless really necessary (knowing you helps). If you think you need my attention in a topic, tag me.

  • Author
6 hours ago, LMS said:

count them yourself

Yes, thats what i do now.
Thanks for clarifying!
Dynamically arrays are also not an option afaic understand, so if the number of elements will vary a List should be used, right?

See my plugin here:
https://www.youtube.com/watch?v=peqSXuTfIyY

Let me know if you find it interesting.
Best Regards.

  • Management Team
On 9/12/2023 at 3:45 PM, GTAbear said:

Yes, thats what i do now.
Thanks for clarifying!
Dynamically arrays are also not an option afaic understand, so if the number of elements will vary a List should be used, right?

 

Yes, a list is usually the better option when you need to add new elements. You can have a dynamically sized array, but when you exceed the current size, you would have to copy the entire old array which makes the insertion operation slow sometimes. While there might be cases where the retrieval performance of an array justifies a slow insert, but there might also be better data structures for that need and in general in modding, usually the number of elements is small enough that using a list is never a bad idea.

Please do not PM me unless really necessary (knowing you helps). If you think you need my attention in a topic, tag me.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.