I suggest you ...

Incorporates "comprehension expressions" like python in C#

Comprehension expressions are powerful when you work with lists. They are very simple to use and with less code it's able to solve many difficult problems.

So, here is some examples of comprehension expressions:

var L = new {1, 2, 3, 4, 5};
for(var i = 0; i < L.length; i++)
... L[i] += 10
...
Result:
[11, 12, 13, 14, 15]

Now with comprehension expression:

var L = new {1, 2, 3, 4, 5};
L = {n + 10 for n in L}

Result:
[21, 22, 23, 24, 25]

Another sample using filter:

var M = new [,]
{
{1,2,3},
{4,5,6},
{7,8,9}
};

var columnValues = {row[1] for row in M if row[1] % 2 == 0] //Filter out odd items

Result:
[2, 8]

0 votes
Vote
Sign in
(thinking…)
Password icon
Signed in as (Sign out)
You have left! (?) (thinking…)
Carlos Marchi shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

2 comments

Sign in
(thinking…)
Password icon
Signed in as (Sign out)
Submitting...
  • jmarolf commented  ·   ·  Flag as inappropriate

    Equivalent C# program using Linq queries:

    using System;
    using System.Linq;

    class Program
    {
    static void Main(string[] args)
    {
    var L = new[] { 1, 2, 3, 4, 5 };
    for (int i = 0; i < L.Length; i++)
    {
    L[i] += 10;
    }

    Console.WriteLine(string.Join(", ", L));

    L = (from n in L
    select n + 10).ToArray();

    Console.WriteLine(string.Join(", ",L));

    var M = new int[][]
    {
    new []{1,2,3},
    new []{4,5,6},
    new []{7,8,9}
    };

    var columnValues = from row in M
    where row[1] % 2 == 0
    select row[1];
    Console.WriteLine(string.Join(", ",columnValues));
    }
    }

    While the syntax is different, I think "comprehension expressions" and linq queries fill the same role in their respective languages.

Feedback and Knowledge Base