简体   繁体   中英

C# Conversion Help & PHP

I have another problem converting an array from php to c#.

public $array = array(103 => array('', ''), 102 => array('', ''), 101 => array('', '', ''), 100 => array('', '', '', ''));

This is what I have:

public Dictionary<int, List<string>> waddlesById = new Dictionary<int, List<string>>();

problem is whenever I do this:

sizeof($this->array[$waddleId]) - 1

That equals -1, but when do it in c#:

waddlesById[waddleId].Count - 1

equals 0. This is my construct function:

string maxSeats = String.Empty;
            foreach (int waddleId in sledRacing)
            {
                switch (waddleId)
                {
                    case 103:
                    case 102:
                        maxSeats = ",";
                        break;

                    case 101:
                        maxSeats = ",,";
                        break;

                    case 100:
                        maxSeats = ",,,";
                        break;
                }
                if (waddlesById.ContainsKey(waddleId))
                {
                    waddlesById[waddleId].Add(maxSeats);
                }
                else
                {
                    List<string> newInner = new List<string>();
                    newInner.Add(maxSeats);
                    waddlesById.Add(waddleId, newInner);
                }
            }

Any help is appreciated

Using .Count will automatically create the entry. Use waddlesById.ContainsKey(waddleId) instead: http://msdn.microsoft.com/en-us/library/htszx2dy.aspx

I think you are confusing Lists and Arrays in C#. @Simon Witehead is right, you probably need to rethink your code in C# idioms. At the same time it reminded me the LookUp type (which I believe not that commonly known) and I though I'd try to answer you question in (hopefully) somewhat useful way.

Before that, let me clear one thing first: I assume you are trying to create an array with this code:

maxSeats = ",,,";

If that's correct, then here is how you create an array with three elements:

var myArray = new string[3];
// or similar to your PHP code
var myArray = new string[] { "", "", "" };

As for the LookUp example, I think, it provides a more C# idiomatic way of doing this kind of task:

var sledRacing = new[] { 100, 102, 103, 100, 100, 102, 101 };

var lu = sledRacing.ToLookup(
        k => k,
        k => k == 100 ? new string[3] : (k == 101 ? new string[2] : new string[1])
    );

foreach (var g in lu)
{
    Console.WriteLine(g.Key);
    foreach (var i in g)
    {
        Console.WriteLine("  - " + i.Length);
    }
}

This will produce the following output:

100
  - 3
  - 3
  - 3
102
  - 1
  - 1
103
  - 1
101
  - 2

Having said all that (and not knowing much about PHP, so I can't tell if the language lack dynamically sized array or lists and compare), you might need to rethink your code: If you want to use a List then you don't need to size it to start with. Then you might want to be more 'Object oriented' and encapsulate 'Racing Waddle' in a class and use the dictionary (or LookUp) to index them by their id and so on.

There is a similar example on MSDN using LookUp type , that might help too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM