ienumerable

Does enumerating a BitArray cause lots of boxing/unboxing?

System.BitArray only implements the non-generic IEnumerable, which returns an Object for the IEnumerator.Current property. Does running a foreach over a BitArray - eg foreach (bool b in bitArray) { // ... } box and unbox each and every bit value? Looking at the bitarray enumerator in reflector, it looks like it does a fresh bitma...

Why does this generic cast fail?

I have the following inheritance: internal abstract class TeraRow{} internal class xRow : TeraRow {} // xRow is a child of TeraRow public IEnumerable<TeraRow> Compare(MappedTables which, DateTime selectionStart , DateTime selectionEnd, string pwd) { IEnumerable<xRow> result=CompareX(); return (IEnumerable<TeraRow>)result; /...

C#: A good and efficient implementation of IEnumerable<T>.HasDuplicates

Does anyone have a good and efficient extension method for finding if a sequence of items has any duplicates? Guess I could put return subjects.Distinct().Count() == subjects.Count() into an extension method, but kind of feels that there should be a better way. That method would have to count elements twice and sort out all the distict...

Trying to figure out how to allow a user to edit a collection with a DataGrid

I'm working on a plug-in for a 3D modeling program to assist in architectural design. I have a Building class which needs to contain a collection of Floors. My Floor class has properties such as Elevation, Height, ProgramType (residential, retail, etc), and ID. Each building has a FloorList property which is the collection of all the ...

IEnumberable to something that I can get a Count from?

I've got this: private IEnumerable _myList; I need to get a count off of that object. I was previously typing _myList to an array and getting the length, but now we are using this same bit of code with a different kind of object. It's still a Collection type (it's a strongly typed Subsonic Collection object), and everything works gr...

A method that returns an IEnumerable can get its output from another method with the same return type?

Behold the C# code: IEnumerable<int> innerMethod(int parameter) { foreach(var i in Enumerable.Range(0, parameter)) { yield return i; } } IEnumerable<int> outerMethod(int parameter) { foreach(var i in Enumerable.Range(1, parameter)) { foreach(var j in innerMethod(i)) { ...

Why does IEnumerable<T> derive from IEnumerable?

Duplicate: Why IEnumerable<T> inherits from IEnumerable? It looks like IEnumerable, ICollection, IList and their generic equivalents are in separate branches. ...

IEnumerable<T> in C#

I am trying to get the following code to compile but am getting errors in VS2008. Anyone can tell me where I am going wrong? using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace dummy { public class NaturalNumbersSequence : IEnumerable<int> { public IEnumerator<int> GetEnumera...

Best way to test if a Type is a collection

I have a method that accepts a parameter obj of type System.Object Now I want to check if the actual type of obj is: A collection type (IEnumerable). Anything else. The first way I thought of is: if (obj is IEnumerable) // obj is a collection But System.String implements IEnumerable, and I don't want to treat string as a coll...

Is it possible to extend arrays in C#?

I'm used to add methods to external classes like IEnumerable. But can we extend Arrays in C#? I am planning to add a method to arrays that converts it to a IEnumerable even if it is multidimensional. Not related to http://stackoverflow.com/questions/628427/how-to-extend-arrays-in-c ...

How can I add an item to a IEnumerable<T> collection?

My question as title above. For example, IEnumerable<T> items = new T[]{new T("msg")}; items.ToList().Add(new T("msg2")); but after all it only has 1 item inside. Can we have a method like items.Add(item)? like the List<T> ...

C# List<T> vs IEnumerable<T> performance question

Hi suppose these 2 methods: private List<IObjectProvider> GetProviderForType(Type type) { List<IObjectProvider> returnValue = new List<IObjectProvider>(); foreach (KeyValuePair<Type, IObjectProvider> provider in _objectProviders) { if ((provider.Key.IsAssignableFrom(type) || ...

How to prevent a method caller from modifying a returned collection?

I have methods returning private collections to the caller and I want to prevent the caller from modifying the returned collections. private readonly Foo[] foos; public IEnumerable<Foo> GetFoos() { return this.foos; } At the moment the private collection is a fixed array, but in the future the collection might become a list if th...

How to I combine multiple IEnumerable list together.

I have a class (ClassA) that has a IEnumerable property. I then has another class (ClassB) that has the same property. They are sharing an interface (InterfaceA). The ClassB is basically a container class for multiple ClassA's. How to I implement the property for ClassB. interface InterfaceA { IEnumerable<int> MyInts { ...

WPF IEnumerable<T> vs IQueryable<T> as DataSource

Hi. I have a WPF ListView and I bind it to a IEnumerable<T> collection. Everything works fine, but when I bind it to the IQueryable<T> collection, there are no items in list anymore.. Why? Is it not observable or what? When I look at the definition: public interface IQueryable<T> : IEnumerable<T>, IQueryable, IEnumerable and public...

Implementing your own LINQ & IEnumerable<T>

In a project I am working on, there are really huge collections (1M-1B elements), and things are modified as collections mostly. It's a real-time app, and so the performance is paramount. For some of the operations, like Reverse, BinarySearch (possible?), etc will suffer more than others like Select, etc. Is it feasible to implement o...

LINQ: find all checked checkboxes in a GridView

Consider the current algorithm below that iterates through a GridView's rows to find whether the contained Checkbox is selected/checked. List<int> checkedIDs = new List<int>(); foreach (GridViewRow msgRow in messagesGrid.Rows) { CheckBox chk = (CheckBox)msgRow.FindControl("chkUpdateStatus"); if (chk.Checked){ //we want the GridV...

C#: How to implement IOrderedEnumerable<T>

I want to implement some various algorithms for practice, just to see how bad I really am and to get better :p Anyways, I thought I would try to use IEnumerable<T> and IOrderedEnumerable<T> and other .Net collection types just to be compatible (so that what I write can be used more easily later). But I can't find a way to return an ins...

Getting index + value for current in IEnumerable loop

Hello I have a loop, where I need to use the values from 2 IEnumerable's, and I need to get the current index and value for "labels", so I can print out labelname for each one. public static string CheckBoxList(this HtmlHelper htmlhelper, IEnumerable<string> values, IEnumerable<string> labels, string name, IDictionary<string, object> H...

Convert IEnumerable to DataTable

Is there a nice way to convert an IEnumerable to a DataTable? I could use reflection to get the properties and the values, but that seems a bit inefficient, is there something build-in? (I know the examples like: ObtainDataTableFromIEnumerable) EDIT: This question notified me of a problem handling null values. The code I wrote below h...