可能重复:
如何在C#中克隆通用列表?
List<MyObject> a1 = new List<MyObject>();
var new1 = a1;
Run Code Online (Sandbox Code Playgroud)
现在,如果我改变,a1那么new1也将改变.
所以我的问题是如何正确克隆a1?
我需要使用list monad转换器.我读过,有潜在问题ListT IO的Control.Monad.List,因为IO是不可交换的,所以我期待在ListT做的权利.但我得到了一些意想不到的行为.
考虑这个简单的测试:
test = runListT $ do
x <- liftList [1..3]
liftIO $ print x
y <- liftList [6..8]
liftIO $ print (x,y)
Run Code Online (Sandbox Code Playgroud)
使用Control.Monad.List:
Main> test
1
(1,6)
(1,7)
(1,8)
2
(2,6)
(2,7)
(2,8)
3
(3,6)
(3,7)
(3,8)
[(),(),(),(),(),(),(),(),()]
Run Code Online (Sandbox Code Playgroud)
使用"ListT右对齐":
Main> test
1
(1,6)
Run Code Online (Sandbox Code Playgroud)
这是"ListT做得对"的问题,还是我只是错误使用它?有首选替代方案吗?
谢谢!
我的表单上有一个组合框,它绑定到一个通用的字符串列表,如下所示:
private List<string> mAllianceList = new List<string>();
private void FillAllianceList()
{
// Add alliance name to member alliance list
foreach (Village alliance in alliances)
{
mAllianceList.Add(alliance.AllianceName);
}
// Bind alliance combobox to alliance list
this.cboAlliances.DataSource = mAllianceList;
}
Run Code Online (Sandbox Code Playgroud)
然后,用户可以在组合框中添加或移除项目.
我在其他地方读到,通过简单地添加或删除通用列表中的项目,组合框的内容应该自动更新; 如果我对它使用Sort(),应该会发生同样的事情.
但由于某种原因,我无法做到这一点.我可以看到组合框的DataSource属性在添加/删除/排序项时正确更新,但组合框中显示的内容不是DataSource属性中的内容.
我肯定错过了什么或做错了什么.
提前致谢!
编辑:
我选择的答案解决了添加和删除的问题,但BindingList对象无法排序,这对我来说是必要的.我找到了一个解决方案,通过继承BindingList和添加排序功能来构建自定义类,但我想知道在我的情况下是否有更简单的解决方案.
关于如何轻松解决这个问题的任何建议?
关于Stackoverflow的第一个问题(.Net 2.0):
所以我试图返回一个List的XML,其中包含以下内容:
public XmlDocument GetEntityXml()
{
StringWriter stringWriter = new StringWriter();
XmlDocument xmlDoc = new XmlDocument();
XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);
XmlSerializer serializer = new XmlSerializer(typeof(List<T>));
List<T> parameters = GetAll();
serializer.Serialize(xmlWriter, parameters);
string xmlResult = stringWriter.ToString();
xmlDoc.LoadXml(xmlResult);
return xmlDoc;
}
Run Code Online (Sandbox Code Playgroud)
现在,这将用于我已经定义的多个实体.
说我想得到一个XML List<Cat>
XML将类似于:
<ArrayOfCat>
<Cat>
<Name>Tom</Name>
<Age>2</Age>
</Cat>
<Cat>
<Name>Bob</Name>
<Age>3</Age>
</Cat>
</ArrayOfCat>
Run Code Online (Sandbox Code Playgroud)
获取这些实体时,有没有办法让我一直得到相同的Root?
例:
<Entity>
<Cat>
<Name>Tom</Name>
<Age>2</Age>
</Cat>
<Cat>
<Name>Bob</Name>
<Age>3</Age>
</Cat>
</Entity>
Run Code Online (Sandbox Code Playgroud)
另请注意,我不打算将XML反序列化 List<Cat>
在什么情况下,System.Collections.Generic.List中的项目不能成功删除?
来自http://msdn.microsoft.com/en-us/library/cd666k3e.aspx:
如果项目已成功删除,则为true; 否则,错误.如果在List(Of T)中找不到项,则此方法也返回false.
他们用短语表达的方式让我觉得List(Of T)中找到的项目的删除操作可能实际上失败了,因此这个问题.
我像这样建立我的列表:
public static List<SearchFormula> SearchData(string searchString)
{
var searchResults = new List<SearchFormula>();
SqlDataReader drResults = FormulaUtility.SearchFormulas(searchString);
if ((drResults != null) && (drResults.HasRows))
{
while (drResults.Read())
{
searchResults.Add(new SearchFormula()
{
// id use the GetValue function
Title = drResults.GetString(1),
Description = drResults.GetString(2),
Url = drResults.GetString(3)
// total use the GetValue Function
});
}
}
return searchResults;
}
Run Code Online (Sandbox Code Playgroud)
使用此对象:
public class SearchFormula
{
public string Title { get; set; }
public string Description { get; set; }
public string Url { get; …Run Code Online (Sandbox Code Playgroud) 我有一组元素/键,我正在从两个不同的配置文件中读取.因此,键可以是相同的,但具有与它们中的每一个相关联的不同值.
我想按排序顺序列出它们.我能做什么 ?我尝试了SortedList类,但它不允许重复键.
我该怎么做?
例如,假设我有3个元素,键1,2,3.然后我得到一个具有键2(但值不同)的元素.然后我希望新密钥在现有密钥2之后但在3之前插入.如果我在找到一个带有密钥2的元素,那么它应该在最近添加的密钥2之后.
请注意,我使用的是.NET 2.0
我有这个通用列表,我想得到类型的字节大小,如果T是字符串或int等,我尝试了两种方式,如getByteSize()中所写,只是为了让你知道我只使用一种方式一段时间......
但是当我尝试编译时,它会出现错误,说"错误:找不到类型或命名空间名称'typeParameterType'(您是否缺少using指令或程序集引用?)"
public class iList<T> : List<T>
{
public int getByteSize ()
{
// way 1
Type typeParameterType = typeof(T);
return sizeof(typeParameterType);
// way 2
Type typeParameterType = this.GetType().GetGenericArguments()[0];
return sizeof(typeParameterType);
}
}
Run Code Online (Sandbox Code Playgroud)
并且想到我在这里做错了什么?
我有一个通用列表,正在被调用4次的方法中使用.此方法使用此通用列表的值在PDF中写入表.
我的问题是我需要在方法中反转这个通用列表,但我调用方法4次,所以每次调用方法时列表都被反转,我不想要...我该怎么做?有没有办法扭转列表而不改变原始?
这是在方法内:
t.List.Reverse();
foreach (string t1 in t.List)
{
//Some code
}
Run Code Online (Sandbox Code Playgroud) 我正在查看System.Collections.Generic.List <T>的开源代码.该AddRange(IEnumerable<T>)方法如下所示:
public void AddRange(IEnumerable<T> collection) {
Contract.Ensures(Count >= Contract.OldValue(Count));
InsertRange(_size, collection);
}
Run Code Online (Sandbox Code Playgroud)
这些InsertRange(int, IEnumerable<T>)方法看起来像这样:
public void InsertRange(int index, IEnumerable<T> collection) {
if (collection==null) {
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);
}
if ((uint)index > (uint)_size) {
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_Index);
}
Contract.EndContractBlock();
ICollection<T> c = collection as ICollection<T>;
if( c != null ) {
int count = c.Count;
if (count > 0) {
EnsureCapacity(_size + count);
if (index < _size) {
Array.Copy(_items, index, _items, index + count, _size - index); …Run Code Online (Sandbox Code Playgroud)