Object Initializers

With C# object initializers, you can project into more complex types.


class MyItem
{
  public string item1;  
  public string item2;
}

and then project into it with object initializers:

 
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
class MyItem
{
    public string item1;
    public string item2;
}

class Program
{
    static void Main()
    {
        string[] names = { "Java", "C#", "Javascript" };

        IEnumerable<MyItem> temp =
        from n in names
        select new MyItem
        {
            item1 = n,
            item2 = n.Replace("a", "").Replace("e", "").Replace("i", "")
            .Replace("o", "").Replace("u", "")
        };
        
        foreach(MyItem item in temp){
           Console.WriteLine(item.item1);
           Console.WriteLine(item.item2);
        }
    }
}
  

The output:


Java
Jv
C#
C#
Javascript
Jvscrpt
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.