0

I'd like to know if it's possible to create an 2D Array in the c# (not the format string) which contains tow columns. The first is Date (format string) and the second is Price (format double).

Here is my code

double [][]=Array[2][];

string[]=Date;

double[]=Prices;

Array[0]=Date;

Array[1]=Prices;
1
  • It is always good practice to create your own classes than mix types in arrays for transmitting compound data. Depending on your needs, a struct instead of a class may also work for you. Commented Jul 14, 2015 at 13:42

3 Answers 3

4

It is not possible to put string in a double array. I suggest to use another type, for example a Tuple<double[], string[]>.

Tuple<double[], string[]> t = new Tuple<double[], string[]>(Prices,  Data);

double[] prices = t.Item1;
string[] data = t.Item2;

But you could better create your own type, reflecting these properties.

public class YourClass
{
    public double[] Prices {get;set;}
    public string[] Data {get;set;}
}

And use it like this:

YourClass c = new YourClass() { Prices = prices, Data = data };

But maybe you want to combine the items:

public class PriceInfo
{
    public double Price {get;set;}
    public string Data {get;set;}
}

And just create a list of them:

List<PriceInfo> list = new List<PriceInfo>();
list.Add(new PriceInfo() { Price = 1d, Data = "abc" });
Sign up to request clarification or add additional context in comments.

3 Comments

I personally would avoid a tuple like the plague for this kind of thing. Super unreadable. Creating your own type (like you suggest) is much more readable.
@MatthewWatson: Agree. Added.
Indeed; tuples grant zero transparency in terms of data types. And seeing something.Item7 in a code is truly horrifying.
0

It is possible to have both types in an array, but it is not reconmended. You would have to declare an array of type object and assign your values to the array. But you would also have to do type casting anytime you reference those values.

object[][] array;

I suggest creating an object to contain your values and then have an array of that.

public class Container
{
     public string Date { get; set; }
     public double[] Prices { get; set; }
}

Container[] array;
array[0].Date = "7/14/15";
array[0].Prices[0] = 100.00;

Comments

0

Here's an alternative take on this.

It looks like you want to be able to look up prices for a given date. If that is the case, one way to solve is is by using a dictionary that maps dates to arrays of doubles:

var pricesByDate = new Dictionary<DateTime, double[]>();

They you can use to dictionary to find the prices for a particular date like so:

var prices = pricesByDate[myDateToLookU[];

A simple console app which demonstrates follows:

using System;
using System.Collections.Generic;

namespace Demo
{
    public class Program
    {
        private static void Main()
        {
            var pricesByDate = new Dictionary<DateTime, double[]>();

            var date1 = new DateTime(2015, 01, 01);
            var date2 = new DateTime(2015, 01, 02);
            var date3 = new DateTime(2015, 01, 03);

            pricesByDate[date1] = new[] { 1.01, 1.02, 1.03 };
            pricesByDate[date2] = new[] { 2.01, 2.02, 2.03, 2.04 };
            pricesByDate[date3] = new[] { 3.01, 3.02 };

            Console.WriteLine("Prices for {0}:", date2);

            var pricesForDate2 = pricesByDate[date2];

            foreach (double price in pricesForDate2)
                Console.WriteLine(price);

            Console.WriteLine("\nAll Dates and prices:");

            foreach (var entry in pricesByDate)
            {
                Console.Write("Prices for {0}: ", entry.Key.ToShortDateString());

                foreach (double price in entry.Value)
                    Console.Write(price + " ");

                Console.WriteLine();
            }
        }
    }
}

I'd probably use a List<double> rather than a double[] for this, because it allows you to add and remove values after it's been created.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.