Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using arrays inside a list in C#

Tags:

arrays

c#

list

We are supposed to put log entries (title and entry) in a sort of journal. That means having a list with each containing an array with room for two strings.

Now. We've worked with arrays and lists but never together.

Considering this is a school assignment I'm only asking for guidance to point me in the right direction.

So creating a list that on each entry contains space for an array of size 2. How should I think about this concept baring in mind I've only used them separately before?

like image 470
Mattias Stahre Avatar asked Mar 04 '26 07:03

Mattias Stahre


2 Answers

A List<string[]> is a perfectly valid data structure, although a List<Tuple<string, string>> or a List<LogEntry> where LogEntry is a class with two string properties would be more idiomatic.

Note that there's not a declarative way to set the size of the underlying array (meaning that List<string[2]> is not a valid type.

like image 150
D Stanley Avatar answered Mar 05 '26 21:03

D Stanley


Since you have to have a log title and entry, have you considered wrapping those in a separate class rather than using an array of two strings?

I mean doing something like this:

public class LogItem
{
    public string Item;
    public string Entry;
}

Then instead of having a list of arrays, you would just use a list of LogItem:

var myList = new List<LogItem>();

var myNewLogEntry = new LogItem {Title = "Entry title", Entry = "Entry message"};

myList.Add(myNewLogEntry);
like image 21
Matthew Watson Avatar answered Mar 05 '26 22:03

Matthew Watson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!