generic-list

Convert an enum to List<string>

孤街浪徒 提交于 2019-11-27 04:17:08
问题 How do I convert the following Enum to a List of strings? [Flags] public enum DataSourceTypes { None = 0, Grid = 1, ExcelFile = 2, ODBC = 4 }; I couldn't find this exact question, this Enum to List is the closest but I specifically want List<string> 回答1: Use Enum 's static method, GetNames. It returns a string[] , like so: Enum.GetNames(typeof(DataSourceTypes)) If you want to create a method that does only this for only one type of enum , and also converts that array to a List , you can write

c# foreach (property in object)… Is there a simple way of doing this?

情到浓时终转凉″ 提交于 2019-11-27 03:11:29
I have a class containing several properties (all are strings if it makes any difference). I also have a list, which contains many different instances of the class. While creating some unit tests for my classes I decided I wanted to loop through each object in the list and then loop through each property of that object... I thought doing this would be as simple as... foreach (Object obj in theList) { foreach (Property theProperties in obj) { do some stufff!!; } } But this didnt work! :( I get this error... "foreach statement cannot operate on variables of type 'Application.Object' because

WCF: Serializing and Deserializing generic collections

大兔子大兔子 提交于 2019-11-27 03:09:53
问题 I have a class Team that holds a generic list: [DataContract(Name = "TeamDTO", IsReference = true)] public class Team { [DataMember] private IList<Person> members = new List<Person>(); public Team() { Init(); } private void Init() { members = new List<Person>(); } [System.Runtime.Serialization.OnDeserializing] protected void OnDeserializing(StreamingContext ctx) { Log("OnDeserializing of Team called"); Init(); if (members != null) Log(members.ToString()); } [System.Runtime.Serialization

How to cast or convert List of objects to queue of objects

谁说胖子不能爱 提交于 2019-11-27 02:38:32
问题 How can one convert a list of objects to a queue thereby maintaining the same order? 回答1: Queue has a constructor that takes in an ICollection . You can pass your list into the queue to initialize it with the same elements: var queue = new Queue<T>(list); // where 'T' is the lists data type. 回答2: What do you mean by "the same order?" If you do this: var queue = new Queue<object>(list); Then the queue will be enumerated over in the same order as the list, which means that a call to Dequeue

XML Serialization of List<T> - XML Root

白昼怎懂夜的黑 提交于 2019-11-26 22:46:45
问题 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)

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

风格不统一 提交于 2019-11-26 22:32:17
问题 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

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

Generic list of generic objects

大憨熊 提交于 2019-11-26 20:24:33
问题 Let's say I have an object that represents a field of data, that object needs the following properties: Name, Type, Value, Length. Here is the object: class Field<T> { public string Name { get; set; } public Type Type { get { return typeof(T); } } public int Length { get; set; } public T Value { get; set; } } I have used generics, because I want to force the user of the code to only be able to assign a Value of certain Type. Now the problem is when I want to create a list of fields. If I

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>)

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

大憨熊 提交于 2019-11-26 19:13:59
问题 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; 回答1: 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