CSharp - Use Select operator to generate new types


Our lambda expression is instantiating a new, anonymous type.

The compiler will dynamically generate an anonymous type that will contain a string p and an int Length/

The selector method will return that newly instantiated object.

Because the type of the returned element is anonymous, we have no type name to reference it.

So we cannot assign the output sequence from Select to an IEnumerable of some known type.

Therefore, we assign the output sequence to a variable specified with the var keyword.


using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;

class Program//ww  w. j  a v  a2s.  c om
    static void Main(string[] args)
        string[] names = {"Python", "Java", "Javascript", "Bash", "C++", "Oracle"};
        var nameObjs = names.Select(p => new { p, p.Length });
        foreach (var item in nameObjs)



Related Topic