generic-collections

Can't change struct's members value inside generic collections

怎甘沉沦 提交于 2019-11-28 01:06:24
Imagine this struct : struct Person { public string FirstName { get; set; } public string LastName { get; set; } } And following code : var list = new List<Person>(); list.Add(new Person { FirstName = "F1", LastName = "L1" }); list.Add(new Person { FirstName = "F2", LastName = "L2" }); list.Add(new Person { FirstName = "F3", LastName = "L3" }); // Can't modify the expression because it's not a variable list[1].FirstName = "F22"; When I want to change Property 's value it gives me the following error: Can't modify the expression because it's not a variable While, when I tried to change it

Case-INsensitive Dictionary with string key-type in C#

妖精的绣舞 提交于 2019-11-27 11:16:03
If I have a Dictionary<String,...> is it possible to make methods like ContainsKey case-insensitive? This seemed related, but I didn't understand it properly: c# Dictionary: making the Key case-insensitive through declarations This seemed related, but I didn't understand it properly: c# Dictionary: making the Key case-insensitive through declarations It is indeed related. The solution is to tell the dictionary instance not to use the standard string compare method (which is case sensitive) but rather to use a case insensitive one. This is done using the appropriate constructor : var dict = new

Can't change struct's members value inside generic collections

这一生的挚爱 提交于 2019-11-26 21:49:54
问题 Imagine this struct : struct Person { public string FirstName { get; set; } public string LastName { get; set; } } And following code : var list = new List<Person>(); list.Add(new Person { FirstName = "F1", LastName = "L1" }); list.Add(new Person { FirstName = "F2", LastName = "L2" }); list.Add(new Person { FirstName = "F3", LastName = "L3" }); // Can't modify the expression because it's not a variable list[1].FirstName = "F22"; When I want to change Property 's value it gives me the

Lists with wildcards cause Generic voodoo error

大憨熊 提交于 2019-11-26 20:20:41
Does anyone know why the following code does not compile? Neither add() nor addAll() works as expected. Removing the "? extends" part makes everything work, but then I would not be able to add subclasses of Foo. List<? extends Foo> list1 = new ArrayList<Foo>(); List<? extends Foo> list2 = new ArrayList<Foo>(); /* Won't compile */ list2.add( new Foo() ); //error 1 list1.addAll(list2); //error 2 error 1: IntelliJ says: add(capture<? extends Foo>) in List cannot be applied to add(Foo) The compiler says: cannot find symbol symbol : method addAll(java.util.List<capture#692 of ? extends Foo>)

Case-INsensitive Dictionary with string key-type in C#

自古美人都是妖i 提交于 2019-11-26 12:07:42
问题 If I have a Dictionary<String,...> is it possible to make methods like ContainsKey case-insensitive? This seemed related, but I didn\'t understand it properly: c# Dictionary: making the Key case-insensitive through declarations 回答1: This seemed related, but I didn't understand it properly: c# Dictionary: making the Key case-insensitive through declarations It is indeed related. The solution is to tell the dictionary instance not to use the standard string compare method (which is case

Lists with wildcards cause Generic voodoo error

荒凉一梦 提交于 2019-11-26 09:00:32
问题 Does anyone know why the following code does not compile? Neither add() nor addAll() works as expected. Removing the \"? extends\" part makes everything work, but then I would not be able to add subclasses of Foo. List<? extends Foo> list1 = new ArrayList<Foo>(); List<? extends Foo> list2 = new ArrayList<Foo>(); /* Won\'t compile */ list2.add( new Foo() ); //error 1 list1.addAll(list2); //error 2 error 1: IntelliJ says: add(capture<? extends Foo>) in List cannot be applied to add(Foo) The