简体   繁体   English

C#转换帮助和PHP

[英]C# Conversion Help & PHP

I have another problem converting an array from php to c#. 我有另一个问题,将数组从php转换为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#: 等于-1,但是在c#中何时执行:

waddlesById[waddleId].Count - 1

equals 0. This is my construct function: 等于0。这是我的构造函数:

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. 使用.Count将自动创建条目。 Use waddlesById.ContainsKey(waddleId) instead: http://msdn.microsoft.com/en-us/library/htszx2dy.aspx 使用waddlesById.ContainsKey(waddleId)代替: http : //msdn.microsoft.com/zh-cn/library/htszx2dy.aspx

I think you are confusing Lists and Arrays in C#. 我认为您在C#中混淆了列表和数组。 @Simon Witehead is right, you probably need to rethink your code in C# idioms. @Simon Witehead是正确的,您可能需要重新考虑C#习惯用法中的代码。 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. 同时,它使我想起了LookUp类型(我相信那不是众所周知的类型),尽管我试图(希望)以某种有用的方式回答您的问题。

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: 我认为,对于LookUp示例,它提供了一种执行此类任务的更C#惯用方式:

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. 说了这么多(并且对PHP不太了解,所以我无法判断该语言是否缺少动态大小的数组或列表并进行比较),您可能需要重新考虑代码:如果要使用列表,则不需要您无需先调整大小。 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. 然后,您可能希望更加“面向对象”并将“ Racing Waddle”封装在一个类中,并使用字典(或LookUp)通过它们的id对其进行索引等等。

There is a similar example on MSDN using LookUp type , that might help too. 在MSDN上,有一个使用LookUp type的类似示例 ,也可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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