generic-list

Why does undefined Generics types of Collections defaults to Object even if bound to other class?

放肆的年华 提交于 2019-12-11 03:17:22
问题 class Test<G extends String>{ public G test(){return null;} public List<G> tests(){return new ArrayList<>();} } public void doTest(Test t){ //works fine String str = t.test(); //Compile error: expected String found Object str = t.tests().iterator().next(); } I would like the last line to return a String instance instead of Object, as the type G was bound to subclass String. There is any other way than casting ? 回答1: No, the type as been erased. You're running into two different rules for type

Model binding postback data to a controller action parameter of type List<T>

荒凉一梦 提交于 2019-12-11 03:09:58
问题 I have a strong type view of type List<List<MyViewModelClass>> The outer list will always have two lists of List<MyViewModelClass> . For each of the two outer lists I want to display a group of checkboxes. Each set can have an arbitrary number of choices. My view model class looks similar to this: public class MyViewModelClass { public Area Area { get; set; } public bool IsGeneric { get; set; } public string Code { get; set; } public bool IsChecked { get; set; } } So the final view will look

.Net - When is List<T>.ForEach prefered over a standard foreach loop?

喜夏-厌秋 提交于 2019-12-10 21:25:02
问题 The generic list class has a .ForEach(Action<T> action) method. Now i've done some simple timings of how they both perform and it seems that the generic ForEach is the poorer performer. The (Snippet Compiler Friendly) code is below - public static class timer{ public static long foreachloop = 0; public static long Gforeachloop = 0;} public class something{ public List<string> myStrings = new List<string>(); public something() { for(int i = 1; i<=5000000;i++) { myStrings.Add(i.ToString()); } }

Generic List<T> as IEnumerable<object>

不问归期 提交于 2019-12-10 19:25:44
问题 I'm trying to do cast a List to an IEnumerable, so I can verify that different lists are not null or empty: Suppose myList is a List < T > . Then in the caller code I wanted: Validator.VerifyNotNullOrEmpty(myList as IEnumerable<object>, @"myList", @"ClassName.MethodName"); The valdiating code would be: public static void VerifyNotNullOrEmpty(IEnumerable<object> theIEnumerable, string theIEnumerableName, string theVerifyingPosition) { string errMsg = theVerifyingPosition + " " +

Proper way to construct linq queries to achieve fastest performance?

牧云@^-^@ 提交于 2019-12-09 15:36:58
问题 Similar question have been asked here but none fits to my need. I made test cases to see which is faster. But I feel my linq code is still slow. How do I construct the linq code for faster performance? Others said using double .Tolist() can cause slower operation, when I test it, it shows that it is faster than any other tests. Test: Preparation --------------------------------------------------------------- return Properties of UserInfo(userinf, true){ UserID = userinf.UserID; FirstName =

how to get byte size of type in generic list?

人盡茶涼 提交于 2019-12-08 16:19:56
问题 I have this generic list and I want to get the byte size of the type like if T is string or int etc., I tried both ways as written in getByteSize(), and just to let you know I am using only one way at a time ... but when I try to compile, it gives an error saying "Error: The type or namespace name 'typeParameterType' could not be found (are you missing a using directive or an assembly reference?)" public class iList<T> : List<T> { public int getByteSize () { // way 1 Type typeParameterType =

Replace a object in a list of objects

风流意气都作罢 提交于 2019-12-08 15:40:59
问题 In C#, if I have a List<T> , and I have an object of type T , how can I replace a specific item in the List<T> with the object of type T ? Here is what I have tried: List<CustomListItem> customListItems = new List<CustomListItem>(); CustomListItem customListItem1 = new CustomListItem() { name = "Item 1", date = DateTime.MinValue}; CustomListItem customListItem2 = new CustomListItem() { name = "Item 2", date = DateTime.MinValue }; CustomListItem customListItem3 = new CustomListItem() { name =

Two list synchronization

这一生的挚爱 提交于 2019-12-08 04:13:19
问题 I'm doing the synchronization between the two lists. IList<Event> GoogleEvents and Table<myEvent> DB.Events; On google side i'm using this String Summary, String Description, EventDateTime Start, EventDateTime End, Event.RemindersData Reminders; On db side like this my code is like this foreach (myEvent item in DB.Events) { if (item.GoogleID == "" || item.GoogleID == null)// i add event my db { //Add dbEvent to google and save id to googleid column } } foreach (Event item in myGoogleCalendar

Count items in List(Of structure) using predicate in .NET 2.0/VB.NET

人走茶凉 提交于 2019-12-08 03:49:48
问题 I need to count the items that meet a criteria in a List(Of Structure) in .NET 2.0. For example: Dim listcars as New List(Of car) Structure car Dim Name as String Dim year as Integer End structure Now I need to count all cars with name Toyota, etc.. How do I do it? 回答1: You want List.LongCount . Dim CarList As New List(Of Car) Dim Model As String = "Toyota" Dim ToyotaCount As Long = CarList.LongCount(Function(car) car.Name = Model) 回答2: Dim toyotas As Integer = carList.Count(Function(c) c

How make custom Thread Safe Generic List return the whole list in C#?

瘦欲@ 提交于 2019-12-07 06:20:14
问题 I am a threading noob and I am trying to write a custom thread safe generic list class in C# (.NET 3.5 SP1). I've read Why are thread safe collections so hard?. After reviewing the requirements of the class I think I only need to safely add to the list and return the list. The example shows pretty much all I want except it lacks the return list method therefore I've written my own public method like below: Update: based on suggestions given I've reviewed my requirements and therefore