我只想在c#中获得List的2维数组.在我的想法中,下面应该有效,但它没有,List的1维数组有效.我将Unity3D与Mono一起使用,但我认为这是与语言相关的问题.
List<MyType>[,] twoDArrayofList;//don't work, it's type is List<MyType>
List<MyType>[] oneDArrayofList;//it's works, the type is List<MyType>
Run Code Online (Sandbox Code Playgroud)
谁知道什么是错的?谢谢.
//mDIco is a Dictionnary with string as keys and homemade class (cAsso) as values
IEnumerator iterdico = mDico.GetEnumerator();
iterdico.Reset();
while (iterdico.MoveNext())
{
var asso = iterdico.Current as cAsso;
if (asso != null)
{
//Code
}
}
Run Code Online (Sandbox Code Playgroud)
我认为这会起作用,但显然它没有.那么我怎么做才能访问包含在我的词典值中的类?
今天我了解到 rust 不支持 fn 参数的协变,只有它的返回类型是协变的。(参见 Rust 文档)
为什么我在 Rust 中了解到这个事实?因为我试图实现一个非常简单的游戏,我将逻辑、事件处理和绘图分离为三个不同的函数,但所有函数都在相同的玩家向量上运行。
如果这是不可能的,与 C# 版本相比,Rust 中的等效项是什么?
在 C# 中,这是非常简单的Fiddle 您可以定义一个类 X 必须实现的接口 Y,并定义一个相应的委托,该委托需要该接口 Y 的 IEnumerable 作为参数。现在您可以在需要的不同方法之间共享 X 的列表只有一个接口Y。
using System;
using System.Collections.Generic;
public interface Actionable{
void Do();
}
public interface Drawable{
void Draw();
}
public class Player: Drawable, Actionable{
public void Do(){
Console.WriteLine("Action");
}
public void Draw(){
Console.WriteLine("Draw");
}
}
public class Program
{
public delegate void DrawHandler(IEnumerable<Drawable> obj);
public delegate void LogicHandler(IEnumerable<Actionable> obj);
public static void gameloop(DrawHandler …Run Code Online (Sandbox Code Playgroud) 我有一个对象的引用.我知道它符合
IDictionary<string, T>
Run Code Online (Sandbox Code Playgroud)
对于某些类型T(它可能不符合普通的IDictionary,或者不符合IReadyOnlyDictionary).我所知道的T就是它从物体上下来.如何获取密钥并获取密钥的值?(我可以将值作为对象返回,而不是作为T.我也很好,从不学习T是什么.)
我想写的,但不能,是这样的:
public void SomeMethod(object reallyADict) { // reallyADict implements IDictionary<string, T>.
foreach (string key in reallyADict.Keys) {
object value = reallyADict[key];
// . . .
}
}
Run Code Online (Sandbox Code Playgroud)
**
根据请求,下面是一个示例类.
using System;
using System.Collections.Generic;
using System.Collections;
namespace My.Collections
{
public class WrappedDictionary: IDictionary<string, int>
{
public WrappedDictionary() {
this.InnerDictionary = new Dictionary<string, int>{ {"one", 1}, {"two", 2 }};
}
private Dictionary<string, int> InnerDictionary { get; set;}
private ICollection<KeyValuePair<string, int>> InnerCollection {
get {
return this.InnerDictionary;
}
} …Run Code Online (Sandbox Code Playgroud) 我有这种数据类型:
Dictionary<string, List<Dictionary<string, List<Dictionary<string, List<Dictionary<string, string>>>>>>>
Run Code Online (Sandbox Code Playgroud)
每当我试图到达它的最底部来计算一些东西时,我就会遇到一个错误,我无法在KeyValuePairs上使用foreach:
foreach statement cannot operate on variables of type 'KeyValuePair<string, List<Dictionary<string, List<Dictionary<string, string>>>>>' because 'KeyValuePair<string, List<Dictionary<string, List<Dictionary<string, string>>>>>' does not contain a public definition for 'GetEnumerator' [RestService]
Run Code Online (Sandbox Code Playgroud)
这是循环的一部分:
void GetMetricsCount(List<Dictionary<string, List<Dictionary<string, List<Dictionary<string, string>>>>>> device, Dictionary<string, string> results_holder)
{
// count all metrics
foreach (var type_element in device)
{
foreach (var group_element in type_element)
{
foreach (var wtf in group_element)
Run Code Online (Sandbox Code Playgroud)
现在真的很好,如果有更好的方法来处理C#中那种复杂的数据结构,我真的很感激,如果有人告诉我怎么样,因为这很糟糕!
更新:我很抱歉,但我在这里复制了一些代码.以下是对非匹配类型的挫折的答案:
// Get overview of metrics for last 24h
public Dictionary<string, Dictionary<string, …Run Code Online (Sandbox Code Playgroud) public static Dictionary<string, User> userList = new Dictionary<string, User>();
Run Code Online (Sandbox Code Playgroud)
这就是在另一个类中声明“userList”的方式。顺便说一句,该类的名称是与类型相同的“用户”,这可能会导致问题,但我不确定。我不知道如何诚实地完成这项工作。
这是完整的脚本:https : //pastebin.com/h56ukpgR 脚本中的某些内容尚无意义,因为我从另一个脚本中复制了一些内容。
但基本上我正在尝试检查静态字典中是否已经存在昵称,如果是,则通知用户并且不要做任何其他事情。
c# ×6
.net ×1
.net-core ×1
arrays ×1
covariance ×1
dictionary ×1
idictionary ×1
list ×1
rust ×1