Home > Software design >  List object get one more item return than the original data set in Blazor WASM
List object get one more item return than the original data set in Blazor WASM

Time:04-25

I have a parent component file with one list object.

<h3>Create Movie</h3>

<MovieForm Movie="Movie" NotSelectedGenres="NotSelectedGenres" OnValidSubmit="SaveMovie"/>

@code {

    private Movie Movie = new Movie() ;

    private List<Genre> NotSelectedGenres = new List<Genre>()
                                                {
                                                    new Genre()
                                                        {
                                                            Id = 1, Name = "Action"
                                                        },
                                                    new Genre()
                                                        {
                                                            Id = 2, Name = "Comedy"
                                                        },
                                                    new Genre()
                                                        {
                                                            Id = 3, Name = "Drama"
                                                        }
                                                } ;

    private void SaveMovie()
        {
            Console.WriteLine("this works") ;
        }

}

then I insert child component named 'MovieForm' in it.

And then on debug mode, When I checked the length of 'NotSelectedGenres' it said total 4. (in child component)

    [Parameter]
    public List<Genre> SelectedGenres { get ; set ; } = new List<Genre>() ;

it has one more with value of 'null' than the original data set.

NotSelectedGenres
   _items    BlazorMoves.Shared.Entities.Genre[](4)
     0       BlazorMoves.Shared.Entities.Genre
     1       BlazorMoves.Shared.Entities.Genre
     2       BlazorMoves.Shared.Entities.Genre
     3       null
     length  4

Is this the right thing?

CodePudding user response:

As soon as you add the first item to a List in .NET, it internally allocates an array of size 4. The allocated size increases automatically as required.

the Count property should return the correct number of items in the list, while Capacity returns the current internal size allocated.

List<> docs

  • Related