0

I'm implementing a network tool and I'm not sure what to use to store the set of ip addresses and ports to scan that the user is suppose to enter as parameters, namely: a Collection, a List or just Arrays. The user will enter the set of addresses and ports to scan through textboxes. Suggestions from experts and why.

internal  class Network
{
    internal enum Protocol { TCP, UDP };

    private string[] strIpAddresses;
    internal string GetIpAddress(int index = 0) { return strIpAddresses[index]; }
    internal void SetIpAddress(int index = 0, string ip = "127.0.0.1") { this.strIpAddresses[index] = ip; }


    private int[] intPorts;
    internal int GetPort(int index = 0) { return intPorts[index];  }
    internal void SetPort(int index = 0, int port = 80) { this.intPorts[index] = port; }



    internal Network(string[] ipAddr, int[] ports, int IP_ADDR_SIZE_C, int PORT_SIZE_C)
    {
        this.strIpAddresses = new string[IP_ADDR_SIZE_C];
        this.intPorts = new int[PORT_SIZE_C];

        this.strIpAddresses = ipAddr;
        this.intPorts = ports;
    }

Thanks in advance.

  • 1
    It is completely up to you. Lists and collections are _mostly_ backed by arrays anyway. What they give you is wrappers around common array functionality: re-allocation, bounds checking, add/remove logic, etc. We can't decide what is best for you based on what you've provided.. but you're only looking at a small allocation overhead to use a List.. and its not something you should worry about. – Simon Whitehead Apr 18 '14 at 05:08

4 Answers4

0

I am as new as you are probably. But something that came up was trie data structure.

http://en.wikipedia.org/wiki/Trie

If you have subnets, may it could be helpful. But you may need to find whatever implementation [How to create a trie in c# [http://geekyisawesome.blogspot.com/2010/07/c-trie.html]. I am not sure what is actually used inside networking tools.

Community
  • 1
  • 1
Dexters
  • 2,419
  • 6
  • 37
  • 57
  • 1
    it's not exactly what i'm looking for but it's pretty interesting, something that worth it to study of course, thanks. –  Apr 18 '14 at 05:09
0

You should probably use a List. It is easy to add/remove items from.

Collections are more advanced. They can do additional things when overriding AddItem and RemoveItem.

Arrays are static in length and must be recreated when needed to lengthen.

Mattias Åslund
  • 3,877
  • 2
  • 18
  • 17
0

First, List is a Collection.

Use Arrays, if you have a predetermined number of elements. They are faster than Lists.

Use List if you have dynamic number of elements and requires you to add, remove elements etc..

For example:

This is good:

int[] someArr = new int[5]{1,2,3,4,5}

but this is not:

List<int> someArr = new List<int>(){1,2,3,4,5}

Lists allow you to dynamically add or remove items easily. Its as simple as .Add(6) in the list.

But in a array, you have to do: someArr[someArr.length] = 6;.

Also, List gives you many extension methods backed by Linq, but arrays don't have it.

Given your requirement, I would say you to go with List and not Arrays as the former has more flexibility and is easy to use

Amit Joki
  • 58,320
  • 7
  • 77
  • 95
  • yes but, the set of ip addresses(in this case) may vary, same for ports, you can choose a different number of ports to be scanned, and in that case an array is not the best choice for what i'm seeing, despite they are faster, but not flexible –  Apr 18 '14 at 05:19
  • The extension methods you're referring to are on `IEnumerable`, which both `List` and arrays implement. – Preston Guillot Apr 18 '14 at 05:21
0

I could not get your perfect need from your code but I understood enough. So, following are the main points of comparison between array and list.

1) Use an array when the number of elements that need to be inserted into the array is known at compile time and it remains fixed throughout the execution of the program. You can use a collection when you do not know how many elements you will need to store. So basically a collection can grow in size dynamically. The data structure used to implement the collection depends on the collection type.

2) You can store elements of similar type only in an array. Unless of course you declare an array of type 'Object'. Which means, we can store an integer, string and custom object in a single Array. In a collection you can store any type of elements provided they are all objects. You can store basic data types in an array. You can only store objects in a collection.

I would suggest according to your need that you should go for list instead of array.