Click here to Skip to main content
15,391,695 members
Home / Discussions / C#
   

C#

 
AnswerRe: C# EPPlus How to hide range of columns Pin
Victor Nijegorodov24-Jan-22 3:50
MemberVictor Nijegorodov24-Jan-22 3:50 
QuestionHow would you describe what an IEnumerable is to a bright student of C# Pin
BillWoodruff22-Jan-22 2:34
mveBillWoodruff22-Jan-22 2:34 
AnswerRe: How would you describe what an IEnumerable is to a bright student of C# Pin
jschell23-Jan-22 7:25
Memberjschell23-Jan-22 7:25 
GeneralRe: How would you describe what an IEnumerable is to a bright student of C# Pin
Richard Deeming23-Jan-22 21:59
mveRichard Deeming23-Jan-22 21:59 
GeneralRe: How would you describe what an IEnumerable is to a bright student of C# Pin
BillWoodruff25-Jan-22 3:23
mveBillWoodruff25-Jan-22 3:23 
GeneralRe: How would you describe what an IEnumerable is to a bright student of C# Pin
jschell30-Jan-22 6:37
Memberjschell30-Jan-22 6:37 
AnswerRe: How would you describe what an IEnumerable is to a bright student of C# Pin
#realJSOP24-Jan-22 23:44
mva#realJSOP24-Jan-22 23:44 
AnswerRe: How would you describe what an IEnumerable is to a bright student of C# Pin
endo funk26-Jan-22 5:31
Memberendo funk26-Jan-22 5:31 
Is there any real reason to use an IEnumerable ?
Yes; first off IEnumerable was Microsoft implementation of functional programming algebras for a List. It is essentially a monadic data type that encapsulates List; monadic data types are typically designed to conform to a fairly standard set of functional algebras i.e. providing a common API for computations.

One major difference with Microsoft’s implementation; is that Microsoft decided to depart from the more familiar naming conventions used in functional programming circles, for example:
- Select is more common referred to as a Functor; and its method is more typically named Map.
- SelectMany is more commonly referred to as a Monad, and its method is more typically named FlatMap.
Microsoft's choice was to align their monadic implementation with SQL for obvious reasons; they had an existing group of programmers who would be already quite accustomed to SQL; so adopting that style of API would simplify adoption.

Category Theory
Function programming algebras are directly tied to Category Theory; hence there are specific tests (axioms) that can be performed to verify that these common computations conform to the axioms of a Functor; for example, it must preserve:
- Identity morphisms
- Commutativity re composition of morphisms
Similarly said there are rules governing the other algebras like SelectMany; which as I mentioned is a monad, etc…

The benefit of IEnumerable is the same as for any other functionally conforming type; it allows the use of a common set of algebras (common API) on the type that is encapsulated by IEnumerable. In Functional Programming; IEnumerable is not the only monadic data types that are used; there are also for example:
- Identity Monad; this is the most trivial of the monadic types; encapsulating a single value.
- Maybe Monad; a type that encapsulates an optional value; similar to the Option type in C#
- List Monad; a type that encapsulates a list of values; similar to IEnumerable type in C#
- Either Monad; a type that encapsulates a value with one of two possible outcomes.
- Validate Monad; a type like Either that is used to write data validators; because it has been designed to accumulate errors with e.g. an input form with multiple input validations that could be in error at the same time.
- etc.
This is by no means a comprehensive list of monadic data types… both for functional programming and on the large, but its also not comprehensive in terms of the C# types that have some level of implementation of functional programming algebras.

What's so useful about deferred evaluation ?
Deferred evaluation is more commonly referred to as lazy evaluation; why is it useful in Linq is simple. it allows a more expressive use of the api without incurring additional computation cost, for example:
- Multiple Where statements in a single Linq computation without the cost of performing each Where computation separately -- it's combined into a single computation.

Allow you to only incur a computational cost when its needed.
For example; some lazy evaluations may be built up during a computation block; and then never executed because of e.g. a branched operation like a cancellation; where this lazy evaluation becomes unnecessary; hence it would be preferable to not incur any computational cost until its actually needed, and most certainly not at each step (e.g. for each dot chained method call).

Infinite Lists
The other less obvious use of lazy evaluation is infinite lists; lists that are computationally far too expensive and/or impossible to compute on the whole… that is where method calls like Take, Skip come into play, for example:
- I can choose to Skip the 1st 3 entries in an infinite list of primes, and Take the next 4; without incurring the computation cost of trying to 1st populate an infinite list of primes.

Isn't deferred evaluation dangerous because you have no idea of what might have changed in the time between assembling the IEnumerable and iterating/evaluating it ?
No not at all; because the final computation would still work on essentially a snapshot of the encapsulated value at the time the lazy evaluation was computed.

Plus its considered bad design to build any system that incorporates data races — a data race occurs when two computations access the same value concur­rently, and at least one of the accesses mutates the value. The problem is that it makes testing impossible, because you have no way of knowing the state of the encapsulated value; worse the mutation step may only be partially computed; leaving the value in a compromised state. It makes it impossible to test code with objects that are in an indeterminate state.

Why can't I test an IEnumerable to see if it's empty, or null; why do I have to trigger iteration with 'Count() to see how many items it has, and, why can I only use 'Any to see if there are no items in it without triggering iteration..
…because its a lazy evaluation — which is typically impossible (if not difficult) to predetermine in advance of actually computing the lazy evaluation. However Microsoft as of .Net 6 has made available a new Linq method called TryGetNonEnumeratedCount which attempts to determine the number of elements in a sequence without forcing an enumeration; failing which it reverts to forcing the compute.

Personally I would advise against this style of coding completely + in the functional programming context; we specifically avoid these types of method calls, because they are indeterminate; worse this method mutates an input parameter (out keyword) -- which makes testing that much more difficult. In functional programming; there is huge merit in trying to keep a majority of the codebase as pure as possible. In the C# context; it is quite possible to have the core of the codebase pure; with a very small cut out for mutation where its unavoidable, for example: UI. Leaving a codebase that is for greater part very easy to test, and hence less prone to errors.

Anyway that's a window into the rabbit hole that is functional programming and its basis in category theory; hopefully its helpful.

modified 26-Jan-22 12:01pm.

GeneralRe: How would you describe what an IEnumerable is to a bright student of C# Pin
Gerry Schmitz26-Jan-22 6:13
mveGerry Schmitz26-Jan-22 6:13 
GeneralRe: How would you describe what an IEnumerable is to a bright student of C# Pin
endo funk26-Jan-22 9:40
Memberendo funk26-Jan-22 9:40 
GeneralRe: How would you describe what an IEnumerable is to a bright student of C# Pin
Gerry Schmitz27-Jan-22 16:58
mveGerry Schmitz27-Jan-22 16:58 
GeneralRe: How would you describe what an IEnumerable is to a bright student of C# Pin
endo funk27-Jan-22 19:35
Memberendo funk27-Jan-22 19:35 
GeneralRe: How would you describe what an IEnumerable is to a bright student of C# Pin
Gerry Schmitz28-Jan-22 5:50
mveGerry Schmitz28-Jan-22 5:50 
GeneralRe: How would you describe what an IEnumerable is to a bright student of C# Pin
endo funk28-Jan-22 6:40
Memberendo funk28-Jan-22 6:40 
GeneralRe: How would you describe what an IEnumerable is to a bright student of C# Pin
Gerry Schmitz28-Jan-22 7:29
mveGerry Schmitz28-Jan-22 7:29 
GeneralRe: How would you describe what an IEnumerable is to a bright student of C# Pin
endo funk28-Jan-22 8:46
Memberendo funk28-Jan-22 8:46 
GeneralRe: How would you describe what an IEnumerable is to a bright student of C# Pin
BillWoodruff26-Jan-22 23:52
mveBillWoodruff26-Jan-22 23:52 
GeneralRe: How would you describe what an IEnumerable is to a bright student of C# Pin
BillWoodruff26-Jan-22 23:39
mveBillWoodruff26-Jan-22 23:39 
GeneralRe: How would you describe what an IEnumerable is to a bright student of C# Pin
endo funk27-Jan-22 6:44
Memberendo funk27-Jan-22 6:44 
GeneralRe: How would you describe what an IEnumerable is to a bright student of C# Pin
BillWoodruff27-Jan-22 23:44
mveBillWoodruff27-Jan-22 23:44 
GeneralRe: How would you describe what an IEnumerable is to a bright student of C# Pin
endo funk28-Jan-22 2:11
Memberendo funk28-Jan-22 2:11 
GeneralRe: How would you describe what an IEnumerable is to a bright student of C# Pin
jschell30-Jan-22 7:08
Memberjschell30-Jan-22 7:08 
GeneralRe: How would you describe what an IEnumerable is to a bright student of C# Pin
endo funk30-Jan-22 14:22
Memberendo funk30-Jan-22 14:22 
GeneralRe: How would you describe what an IEnumerable is to a bright student of C# Pin
jschell17-Feb-22 10:22
Memberjschell17-Feb-22 10:22 
GeneralRe: How would you describe what an IEnumerable is to a bright student of C# Pin
endo funk17-Feb-22 14:22
Memberendo funk17-Feb-22 14:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.