Detect unmaterialized LINQ Queries [duplicate] - c#

This question already has answers here:
How to tell if an IEnumerable<T> is subject to deferred execution?
(6 answers)
Closed 9 years ago.
I have a Cache wrapper class that I use, which provides type safety and segmenting and other nice things.
I want to make it prevent me from shooting myself in the foot by caching an un-materialized LINQ query and only accept lists/collections...
Is there a way to detect if an IEnumerable is a LINQ query?
Maybe I'm answering my own question and should throw an exception when T is IEnumerable but not ICollection.

I would suggest just wrapping the IEnumerable<T> within your own collection, "materializing" it yourself.
This will provide full safety and more consistency, at the expense of potentially generating another collection instance and copying the references over.
You could always do a check for ICollection<T> and not regenerate, or similar, but there are still advantages to copying the contents into your own list. One major one is that you then control the one and only instance of the collection - you don't have to worry about another object adding or removing items (which may or may not be an issue, but is likely problematic for segmenting).

Related

Why use IEnumerable<T> when we can Iterate through collection using foreach() [duplicate]

This question already has answers here:
Can anyone explain IEnumerable and IEnumerator to me? [closed]
(16 answers)
Closed 7 days ago.
I want to know the exact place where we should use IEnumberable<T>
I know how IEnumerable<T> work and returns IEnumerator<T> and all that but the ultimate goal of IEnumerable<T> is to query the data from the collection isn't it? That is what we can already do using foreach() loop ? So when to use IEnumerable<T>? what is the actual practical scenario where the IEnumerable<T> is the only option to query the collection?
but the ultimate goal of IEnumerable<T> is to query the data from the collection isn't it?
No; the goal of IEnumerable<T> is to provide access to a sequence, which may or may not be a collection. The point being to abstract away what the underlying source is. It could be a raw collection, but it could be:
some LINQ (or similar) projection (collection.Where(...).Select(...) etc)
an open query to ADO.NET, redis, a socket, gRPC, a file or some other data provider that isn't readily countable, repeatable, etc - just: "a sequence"
an in-process data generator
some producer/consumer setup
etc
If you know you're always iterating a collection, then sure: feel free to use the concrete type, or ICollection<T>/IList<T> etc; but: not every sequence is a collection.

Why do enumerators need to be disposed? [duplicate]

This question already has answers here:
Why does IEnumerator<T> inherit from IDisposable while the non-generic IEnumerator does not?
(6 answers)
Do I need to consider disposing of any IEnumerable<T> I use?
(1 answer)
Closed 1 year ago.
According to the accepted answer to Enumerator disposal when not using using, foreach or manually calling Dispose() enumerators in C# must be disposed when finished with, whether you let foreach do this automatically, or do it yourself if you prefer to write out the equivalent logic by hand.
I wasn't expecting this; would have thought enumerators would be handled by the garbage collector. I'm guessing it's along the lines of:
Database queries are also enumerable, and those need to be disposed because they could be holding database connections, which are a scarcer resource than memory.
To correctly handle this case, the framework designers decided to just make enumerators implement IDisposable, so the contract is they should always be disposed; for in-memory collections like strings, arrays and lists, this will be a no-op, but it makes the overall design simpler than trying to make some kinds of enumerators implement a different interface.
Is this correct, or am I missing something?

Does ImmutableList have AsReadOnly like method? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
AsReadOnly() is a convenient method to get a read-only image (not expensive compared to immutable collections' copies) of a collection. I am wondering if ImmutableList has AsReadOnly like method? If no, any easy way to implement similarly?
Immutable collections are inherently read-only. You can easily check in the documentation that ImmutableList<T> already implements IReadOnlyList<T> and IReadOnlyCollection<T> interfaces.
Memory is not allocated when you access elements from immutable collection. On the other hand, when you add an element to some immutable data structure, a new immutable collection is created (and some memory is used). Many immutable collections' implementations do not copy all the data to a new collection but instead share some data from with the old one, so in most cases you should not be too concerned with memory usage/allocation time.
Some collections, e.g. ImmutableHashSet<T>, have a documentation which states that they are optimized in terms of number of memory allocations.
The idea behind sharing some data between immutable collection is not complicated. Wikipedia has an simple example (with a nice diagram) showing how memory can be saved in case of immutable singly-linked lists.
ImmutableList<T> is copied by reference so is perfectly safe to pass around without a performance penalty. Thus there is no need for an AsReadOnly method as it wouldn't make it any easier to copy.

SortedList<K,V> vs SortedDictionary<K,V> vs Dictionary<K,V> [duplicate]

This question already has answers here:
SortedList<>, SortedDictionary<> and Dictionary<>
(6 answers)
Closed 9 years ago.
I have a large collection of small objects, each has a unique string ident. I need to decide which class to use.
MSDN says about the first two
The two classes have similar object
models, and both have O(log n)
retrieval. Where the two classes
differ is in memory use and speed of
insertion and removal
Since I rarely insert, mostly just retrieve it seems both are good for me. What about the plain old Dictionary?
Plain-old dictionary is the best option if you're not interested in sorting (since it's O(1) retrieval). If you're not going to modify the list much you should use SortedList since it uses less memory.

I want to know more about LinkedList<T> [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
I'm learning c# , and I reached the LinkedList<T> type and I still need to know more, like when should I use it, how do I create one, how do I to use it. I just want information.
If any one knows a good article about this subject, or if you can show me some examples with explanation, such as how to create, how to add and remove, and how to deal with nodes and elements.
Thanks in advance. I really enjoy asking questions around here with all the pros answering and helping.
[EDIT] Changed reference to LinkedList<T> instead of "array linkedlist." I think this was what was meant based on the context.
You can find more information on LinkedList<T> at MSDN, including an example of how to create and use one. Wikipedia has a reasonable article on linked lists, including their history, usage, and some implementation details.
Linked is a collection to store sequences of values of the same type, which is a frequent task, e.g. to represent a queue of cars waiting at a stoplight.
There are many different collection types like linked list, array, map, set etc. Which one to use when depends on their properties, e.g.:
do you need some type of ordering?
is the container associative, storing key-value pairs like a dictionary?
can you store the same element twice?
performance measures - how fast is e.g. inserting, removing, finding an element? This is usually given in Big-O notation, telling you how the time required scales with the number of elements in the collection.
Memory footprint and layout. This also can affect performance, due to good/bad locality.
This collection class implements a doubly linked list. It allows you to quickly determine the immediate sibling for a specified item in the collection. Removing an item from the collection automatically resizes it so that it does not leave any gaps.
For more info on LinkedList class, check out LinkedList at MSDN.
Do you know what a standard Linked List is? It's like one of those (doubly linked) but using .NET Generics to allow you to easily store any Type inside of it.
Honestly, I don't use it, I prefer the more basic List or Dictionary.
For more info on Linked Lists, check out wikipedia. As for generics, there are tons of articles here and at MSDN.
linklist are collections.. they can be use as replacements for arrays.. they can dynamically grow in size and has special helper methods that can help the development or the problem solving be faster.. try to view its methods and properties to understand more.
linklist is a generic collection.. meaning can used to declare type safety declarations..

Categories