如何创建多参数索引器 [英] How to create multi param indexer

查看:94
本文介绍了如何创建多参数索引器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我正在收集的数据表:


CodeId CodeGroup CodeSubGroup类型

1 K K.1 Shar1

2 K K.1 MD5

3 J J.2 Shar1


我想以两种方式获取数据:


代码代码;

Codegroup =" K"

codes = Collection [" K"]; //返回前两行


Codesubgroup =" K.1";

codes = Collection [Codegroup,Codesubgroup]; //返回前两行


如何在不创建所有类型列表的情况下为上述

语法创建集合类型和索引器(在集合内部) )

处理索引器。


有什么建议吗?


谢谢,

Brett

Here''s a table of data I''m putting into a collection:

CodeId CodeGroup CodeSubGroup Type
1 K K.1 Shar1
2 K K.1 MD5
3 J J.2 Shar1

I want to get the data in two ways:

Codes codes;
Codegroup = "K";
codes = Collection["K"]; //returns first two rows

Codesubgroup = "K.1";
codes = Collection[Codegroup, Codesubgroup]; //returns first two rows

How do I create the type of collection and indexers for the above
syntax without creating all types of list (inside the collection) to
handle the indexers.

Any suggestions?

Thanks,
Brett

推荐答案

取决于您需要的性能......


以下在2.0上正常工作,似乎符合您的规范;如果你需要更多索引(特别是大型列表),那么你可能需要

来查看树结构 - 但索引器API可能会保留

大致相似。


Marc


使用System;

使用System.Collections.Generic;

使用System.Collections.ObjectModel;


公共类代码

{

public readonly int Id;

public readonly string Group,SubGroup,Type;

public Code(int id,string group,string subGroup,string type){

Id = id;

Group = group;

SubGroup = subGroup;

Type = type;

}

}

公共类CodeCollection:ICollection<代码{

私有列表< Code_codes =新列表<代码>();

public IEnumerable< Codethis [string group,string subGroup]

{

get

{

return _codes.FindAll(delegate(Code code)

{

返回string.Equals(code.Group,group)&&

string.Equals(code.SubGroup,subGroup);

});

}

}

public IEnumerable< Codethis [string group]

{

get

{

返回_codes.FindAll(代表(代码)

{

返回string.Equals(code.Group,group);

});

}

}


public void添加(代码项目)

{

_codes.Add(item);

}


public void清除()

{

_codes.Clear();

}


公共布尔包含(代码项)

{

返回_codes.Contains(item);

}


public void CopyTo(Code [] array,int arrayIndex)

{

_codes.CopyTo(array,arrayIndex); < br $>
}


public int Count

{

get {return _codes.Count;}

}


公共布尔IsReadO nly

{

get {return false; }

}


public bool删除(代码项)

{

return _codes.Remove (项目);

}


public IEnumerator< CodeGetEnumerator()

{

return _codes .GetEnumerator();

}


System.Collections.IEnumerator

System.Collections.IEnumerable.GetEnumerator()

{

返回_codes.GetEnumerator();

}

}


class Program

{

static void Main()

{

CodeCollection col = new CodeCollection() ;

col.Add(新代码(1," K"," K.1"," Shar1"));

col.Add(new代码(2,K,K.1,MD5));

col.Add(新代码(3,J,J.2) ;,Shar1);


Console.WriteLine(" * K");

foreach(col [" K"]中的代码;])

{

Console.WriteLine(code.Type);

}

Console.WriteLine(" * J");

foreach(col [" J"]中的代码代码)

{

Console.WriteLine(code.Type);

}

Console.WriteLine(" * K:K .1");

foreach(代码代码在col [" K",K.1"]中)

{

Console.WriteLine(code.Type);

}

}

}
Depends on what performance you need...

The following works fine on 2.0 and appears to meet your spec; if you need
more indexing (particularly for larger lists) then you would probably want
to look at a tree structure - but the indexer API would probably stay
broadly similar.

Marc

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

public class Code
{
public readonly int Id;
public readonly string Group, SubGroup, Type;
public Code(int id, string group, string subGroup, string type) {
Id = id;
Group = group;
SubGroup = subGroup;
Type = type;
}
}
public class CodeCollection : ICollection<Code{
private List<Code_codes = new List<Code>();
public IEnumerable<Codethis[string group, string subGroup]
{
get
{
return _codes.FindAll(delegate(Code code)
{
return string.Equals(code.Group, group) &&
string.Equals(code.SubGroup, subGroup);
});
}
}
public IEnumerable<Codethis[string group]
{
get
{
return _codes.FindAll(delegate(Code code)
{
return string.Equals(code.Group, group);
});
}
}

public void Add(Code item)
{
_codes.Add(item);
}

public void Clear()
{
_codes.Clear();
}

public bool Contains(Code item)
{
return _codes.Contains(item);
}

public void CopyTo(Code[] array, int arrayIndex)
{
_codes.CopyTo(array, arrayIndex);
}

public int Count
{
get {return _codes.Count;}
}

public bool IsReadOnly
{
get { return false; }
}

public bool Remove(Code item)
{
return _codes.Remove(item);
}

public IEnumerator<CodeGetEnumerator()
{
return _codes.GetEnumerator();
}

System.Collections.IEnumerator
System.Collections.IEnumerable.GetEnumerator()
{
return _codes.GetEnumerator();
}
}

class Program
{
static void Main()
{
CodeCollection col = new CodeCollection();
col.Add(new Code(1, "K", "K.1", "Shar1"));
col.Add(new Code(2, "K", "K.1", "MD5"));
col.Add(new Code(3, "J", "J.2", "Shar1"));

Console.WriteLine("* K");
foreach (Code code in col["K"])
{
Console.WriteLine(code.Type);
}
Console.WriteLine("* J");
foreach (Code code in col["J"])
{
Console.WriteLine(code.Type);
}
Console.WriteLine("* K:K.1");
foreach (Code code in col["K","K.1"])
{
Console.WriteLine(code.Type);
}
}
}


实际上 - 在风格上,我不确定我是否喜欢索引器的想法

返回这种类型的实体 - 我希望这些是(重载)

函数,例如(保持一致性)FindAll(字符串组)和

FindAll(字符串组,字符串子组)[否则不变];我希望

索引器接受一个索引或一个合理的(1:1)键,然后返回一个

Code对象。


Marc

Actually - on a stylistic note, I''m not sure I like the idea of an indexer
returning this type of entity - I would prefer these to be (overloaded)
functions, such as (to keep consistency) FindAll(string group) and
FindAll(string group, string subGroup) [otherwise unchanged]; I would expect
the indexer to accept either an index or a sensible (1:1) key, and return a
Code object.

Marc


我喜欢这个设置Marc。你为什么用这种方式回报?


get

{

return _codes.FindAll(delegate(Code code)

{

返回string.Equals(code.Group,group)&&

string.Equals(code.SubGroup,

subGroup);

}

);

}


什么是这样的差异?

得到

{

_codes.FindAll(代表(代码)

return string.Equals(code.Group,group)&&

string.Equals(code.SubGroup,

subGroup);

}


另外,为什么你需要IEnumerable:

public IEnumerable< Codethis [string group,string subGroup]


有没有办法使用索引器语法this [indexOnThis]并避免返回

nulls?Dictionary<,使用tryGetValue(,out)和

一个out参数。我更喜欢索引器语法,但没有明确的

null检查我 必须做。虽然我相信你只是返回一个

的空列表,这可以解决问题并且很好。但是在

的情况下返回一个项目(某个对象在

列表中),是否应该返回一个空对象以避免null?那个

可能会导致代码出现问题,在该项目中寻找具有值的某些

属性,这是一个空对象

惯于。我想在那种情况下你应该总是有好的默认值

一个对象被新建了???


谢谢,

布雷特

I like this setup Marc. Why are you using the return this way?

get
{
return _codes.FindAll(delegate(Code code)
{
return string.Equals(code.Group, group) &&
string.Equals(code.SubGroup,
subGroup);
}
);
}

What is the difference in something like this?
get
{
_codes.FindAll(delegate(Code code)
return string.Equals(code.Group, group) &&
string.Equals(code.SubGroup,
subGroup);
}

Also, why do you need IEnumerable here:
public IEnumerable<Codethis[string group, string subGroup]

Is there a way to use the indexer syntax "this[indexOnThis]" and avoid
nulls being returned? The Dictionary<,uses a tryGetValue(, out) with
an out parameter. I prefer the indexer syntax but without the explicit
null check I must do. Although I believe you are just returning an
empty list, which would solve the problem and is nice. But in the
case that a single item is returned (some object that goes in the
list), should an empty object be returned to avoid the null? That
could cause problems for code down the road that is looking for certain
properties within that item to have values, which an empty object
won''t. I guess in that case you should always have good defaults when
an object is newed up???

Thanks,
Brett


这篇关于如何创建多参数索引器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆