generic-list

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

C# List<double> size vs double[] size

寵の児 提交于 2019-11-27 19:30:03
问题 So I just was testing the CLR Profiler from microsoft, and I did a little program that created a List with 1,000,000 doubles in it. I checked the heap, and turns out the List<> size was around 124KB (I don't remember exactly, but it was around that). This really rocked my world, how could it be 124KB if it had 1 million doubles in it? Anyway, after that I decided to check a double[1000000]. And to my surprise (well not really since this is what I expected the with the List<> =P), the array

XML Serialization of List<T> - XML Root

混江龙づ霸主 提交于 2019-11-27 19:09:51
First question on Stackoverflow (.Net 2.0): So I am trying to return an XML of a List with the following: 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; } Now this will be used for multiple Entities I have already defined. Say I would like

How to convert an ArrayList to a strongly typed generic list without using a foreach?

岁酱吖の 提交于 2019-11-27 17:57:13
See the code sample below. I need the ArrayList to be a generic List. I don't want to use foreach . ArrayList arrayList = GetArrayListOfInts(); List<int> intList = new List<int>(); //Can this foreach be condensed into one line? foreach (int number in arrayList) { intList.Add(number); } return intList; Try the following var list = arrayList.Cast<int>().ToList(); This will only work though using the C# 3.5 compiler because it takes advantage of certain extension methods defined in the 3.5 framework. This is inefficient (it makes an intermediate array unnecessarily) but is concise and will work

How to add item to the beginning of List<T>?

ぃ、小莉子 提交于 2019-11-27 17:11:11
I want to add a "Select One" option to a drop down list bound to a List<T> . Once I query for the List<T> , how do I add my initial Item , not part of the data source, as the FIRST element in that List<T> ? I have: // populate ti from data List<MyTypeItem> ti = MyTypeItem.GetTypeItems(); //create initial entry MyTypeItem initialItem = new MyTypeItem(); initialItem.TypeItem = "Select One"; initialItem.TypeItemID = 0; ti.Add(initialItem) <!-- want this at the TOP! // then DropDownList1.DataSource = ti; Use the Insert method: ti.Insert(0, initialItem); Update: a better idea, set the

C# serialize generic list<customObject> to file

情到浓时终转凉″ 提交于 2019-11-27 15:26:23
问题 i got a class which holds info about pictures, like filepath, hashvalue, bytes. in another class i got a generic list where i put objects from the class that holds picture info. that class looks like this: [Serializable()] class PicInfo : ISerializable { public string fileName { get; set; } public string completeFileName { get; set; } public string filePath { get; set; } public byte[] hashValue { get; set; } public PicInfo() { } public PicInfo(SerializationInfo info, StreamingContext ctxt) {

c# array vs generic list [duplicate]

こ雲淡風輕ζ 提交于 2019-11-27 14:22:59
问题 This question already has answers here : Array versus List<T>: When to use which? (15 answers) Closed 6 years ago . i basically want to know the differences or advantages in using a generic list instead of an array in the below mentioned scenario class Employee { private string _empName; public string EmpName { get{ return _empName; } set{ _empName = value; } } } 1. Employee[] emp 2. List<Employee> emp can anyone please tell me the advantages or disadvantages and which one to prefer? 回答1: One

Saving from List<T> to txt

强颜欢笑 提交于 2019-11-27 14:18:08
I want my program to read from two text files into one List<T> . The List<T> is sorting and cleaning duplicates. I want the List<T> to save (after sorting and cleaning) to a txt file. But when I looked in the result txt file, I found this message: System.Collections.Generic.List`1[System.String] Does anyone have an idea how I could fix this error? Here is my code: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace Uniqpass { class Program { static void Main(string[] args) { String pfad = "C:\\Dokumente und Einstellungen\\Bektas\

Fastest way to Remove Duplicate Value from a list<> by lambda

主宰稳场 提交于 2019-11-27 06:43:07
what is fastest way to remove duplicate values from a list. Assume List<long> longs = new List<long> { 1, 2, 3, 4, 3, 2, 5 }; So I am interesting in use lambda to remove duplicate and returned : {1, 2, 3, 4, 5} . What is your suggestion? The easiest way to get a new list would be: List<long> unique = longs.Distinct().ToList(); Is that good enough for you, or do you need to mutate the existing list? The latter is significantly more long-winded. Note that Distinct() isn't guaranteed to preserve the original order, but in the current implementation it will - and that's the most natural

Generic list by using reflection

≯℡__Kan透↙ 提交于 2019-11-27 06:26:41
问题 I have a scenario on which i got Class name as a string I just want to create a generic List of that class. I tried the following Type _type = Type.GetType(className); List<_type> list = new List<_type>(); But iam geting error . I can't gave that _type as list argument. If anybody knows please help me 回答1: Closest you can get: Type listType = typeof(List<>).MakeGenericType(_type); IList list = (IList) Activator.CreateInstance(listType); 回答2: Reflection takes place during runtime, as the type