0

I'm trying to add an element to an array.

public SoftwareInfoDTO[] GetAllInfo(IPrincipal principal)
    {
        Checks.Checks.CheckPrincipal(principal);

        using (var context = new Context(ConnectionString))
        {
            var allInfo = context.Softwares;
            SoftwareInfoDTO[] softInfoArray = new SoftwareInfoDTO[] {};

            foreach (var elem in allInfo)
            {
                SoftwareInfoDTO softInfo = new SoftwareInfoDTO
                {
                    Id = elem.Id,
                    Name = elem.Name
                };
                softInfoArray.???  <---- NO IDEA
            }

            return softInfoArray;
        }
    }

My idea was to call a method like "insert" to Add the element to my array but I didn't find something useful.

Then I implemented the code write down here, this code works but I don't like it very much, can I use only the array without the support of a List ?

Thanks a lot.

public SoftwareInfoDTO[] GetAllInfo(IPrincipal principal)
    {
        Checks.Checks.CheckPrincipal(principal);

        using (var context = new Context(ConnectionString))
        {
            var allInfo = context.Softwares;

            IList<SoftwareInfoDTO> softInfoArray = new List<SoftwareInfoDTO>();
            foreach (var elem in allInfo)
            {
                SoftwareInfoDTO softInfo = new SoftwareInfoDTO
                {
                    Id = elem.Id,
                    Name = elem.Name
                };
                softInfoArray.Add(softInfo);
            }

            return softInfoArray.ToArray();
        }
    }
Pickeroll
  • 757
  • 2
  • 7
  • 18

2 Answers2

3

Arrays don't implement IList, thus making the methods such as .Add() unavailable. An Array has a fixed size, so if you know the exact size, then you could insert objects in the specified indexes. Realistically, this will rarely happen.

If I had to implement this, I would be using Linq and iterate through the initial collection using Select in order to create a new object for each member of the collection, leaving you with an IEnumerable<T> (T in your case would be SoftwareInfoDTO) which you can easily convert to an Array.

Ex:

return allInfo.Select(elem => 
  new SoftwareInfoDTO
            {
                Id = elem.Id,
                Name = elem.Name
            }).ToArray();
チーズパン
  • 2,672
  • 8
  • 38
  • 58
  • 1
    I'd point out that as a general rule you don't wanna deal with arrays at all in C# unless you're doing some special low-level stuff or are dealing with old/external APIs. Prefer to expose generic interfaces. In my experiences, 9/10 times when a method returns an array or a List, it should be returning an IEnumerable instead, because the client doesn't need to, nor is indeed "allowed" to mutate the collection. – sara Apr 26 '16 at 11:38
0

When using arrays you have to set the size of the array on initiation.

Here is an answer about why you would want to use List<T> https://stackoverflow.com/a/434765/2195005

Community
  • 1
  • 1
soumer
  • 568
  • 5
  • 20