Black Binary Co.
Black Binary Co.

Reputation: 75

Show values of List<string[]> without using extra variables or using the foreach loop

I have fallen into a doubt and I don't know how to solve it, the case is:

I have created an "arrayed string" list like this:

List<string[]> definitions;

I have added to it values like this:

definitions.Add(new string[2] { "A", "Def.1" });
definitions.Add(new string[2] { "B", "Def.2" });

In order to show the values I do it like this:

foreach (string[] theDefinition in definitions)
{
Console.WriteLine(theDefinition[0] + "\tdef: " + theDefinition[1]);
}

So far this works fine, but how can I show the values without the foreach I mean something like this:

Console.WriteLine(definitions[0] ...)

What should I write in the 3 dots to show either the "A" or the "Def.1" from the list in index 0.

I guess overcoming this is by doing something like:

string[] temp = definitions[0]
Console.WriteLine(temp[0] + ", " + temp[1]);

How to achieve it just using the Console.WriteLine without using extra variables, is this possible? and how? Thank you in advance.

Upvotes: 2

Views: 3221

Answers (6)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112392

A better way would be to declare a definition type

public class Definition
{
    public string Name { get; set; }
    public string Value { get; set; }

    public override string ToString()
    {
        return Name + "\tdef: " + Value;   
    }
}

Now you can simplify your code like this

List<Definition> definitions = new List<Definition> {
    new Definition { Name = "A", Value = "Def.1" },
    new Definition { Name = "B", Value = "Def.2" },
};

foreach (Definition theDefinition in definitions)    
{    
    Console.WriteLine(theDefinition);    
}    

Of cause you can use a fluent version of it as proposed by Nikhil Agrawal, which is now even simpler.

definitions.ForEach(def => Console.WriteLine(def)); 

prints

A   def: Def.1
B   def: Def.2

And accessing the fields is more descriptive than using array indexes

Definition def = definitions[0];
Console.WriteLine(def.Name + ", " + def.Value);

// compared to
// Console.WriteLine(temp[0] + ", " + temp[1]);  

Upvotes: 2

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48568

One Line Answer instead of 3 Lines. No use of For or foreach Loop or Extra Variable when LINQ is here

definitions.ForEach(x => Console.WriteLine(x[0] + "\tdef: " + x[1]));

Upvotes: 1

Conrad Frix
Conrad Frix

Reputation: 52655

The other answers are correct of course but why not just use a Dictionary instead of List of a 2 dimensional string array.

        var definitions = new Dictionary<string, string> 
        {
            { "A", "Def.1" },
            { "B", "Def.2" }
        };

        foreach (var keypair in definitions)
        {
            Console.WriteLine("{0} \tdef: {1} ", keypair.Key, keypair.Value);
        }

Upvotes: 2

Daniel Bidulock
Daniel Bidulock

Reputation: 2354

for (var i = 0; i < definitions.Length; i++)
{
    Console.WriteLine(definitions[i][0] + "\tdef: " + definitions[i][1]);
}

Upvotes: 1

itsme86
itsme86

Reputation: 19496

You can access it like this: definitions[definition_index][string_index].

Try:

Console.WriteLine(definitions[0][0] + "\tdef: " + definitions[0][1]);

Upvotes: 1

Henrik
Henrik

Reputation: 23324

Console.WriteLine(definitions[0][0]  + "\tdef: " + definitions[0][1]);

Upvotes: 4

Related Questions