Introduction to C # Lists

How can I do list checks in C #?

+44
c # linq
Sep 25 '08 at 1:03
source share
5 answers

A List Consrehension is a type of notation set in which a programmer can describe the properties that members of a set must match. It is usually used to create a set based on other existing sets or sets by applying some type of combination, transformation, or reduction to existing sets.

Consider the following problem: you have a sequence of 10 numbers from 0 to 9, and you need to extract all the even numbers from this sequence. In a language such as C # version 1.1, you were pretty much limited to the following code to solve this problem:

ArrayList evens = new ArrayList(); ArrayList numbers = Range(10); int size = numbers.Count; int i = 0; while (i < size) { if (i % 2 == 0) { evens.Add(i); } i++; } 

The code above does not show the implementation of the Range function, which is available in the full code list below. With the advent of C # 3.0 and the .NET Framework 3.5 in CY programming, Linq-based list notation is now available. The above C # 1.1 code can be ported to C # 3.0 as follows:

 IEnumerable<int> numbers = Enumerable.Range(0, 10); var evens = from num in numbers where num % 2 == 0 select num; 

And, from a technical point of view, the above C # 3.0 code can be written as single-line by moving the call to Enumarable. Go to the Linq expression that generates the evens sequence. In C # List Consrehension, I reduce the given numbers by applying a function (modulo 2) to this sequence. This creates a sequence of events in a much more concise manner and does not allow for the use of loop syntax. Now you may ask yourself: is this pure syntactic sugar? I don’t know, but I definitely research and maybe even ask the question myself. I suspect that this is not just syntactic sugar, but some true optimizations that can be done using basic monads.

A complete list of codes is available here .

+45
Jun 05 '09 at 3:00
source share

This was discovered when I was looking for how to make a list in C # ...

When someone talks about understanding lists, I immediately think about Python. The code below creates a list that looks like this:

 [0,2,4,6,8,10,12,14,16,18] 

The Python path is as follows:

 list = [2*number for number in range(0,10)] 

In C #:

 var list2 = from number in Enumerable.Range(0, 10) select 2*number; 

Both methods are evaluated lazily.

+17
Mar 26 '09 at 1:43
source share

@Ian P

  return (from user in users where user.Valid select user.Name).ToArray(); 
+6
Sep 25 '08 at 1:17
source share

You can use LINQ to create list-like expressions. Here the site explains this a bit:

Matching lists in C # with LINQ

Matching Lists in C # with LINQ - Part 2

+4
Sep 25 '08 at 1:11
source share

Although this is not a tutorial, here is some code that illustrates the concept:

 public List<string> ValidUsers(List<User> users) { List<string> names = new List<string>(); foreach(User user in users) { if(user.Valid) { names.Add(user.Name); } } return names; } 
-four
Sep 25 '08 at 1:08
source share



All Articles