Csharp/CSharp Tutorial/LINQ/var — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 15:31, 26 мая 2010

An Anonymous Type Assigned to a Variable Declared with the var Keyword

using System;
public class MainClass {
    public static void Main() {
        var unnamedTypeVar = new { firstArg = 1, secondArg = "Joe" };
        Console.WriteLine(unnamedTypeVar.firstArg + ". " + unnamedTypeVar.secondArg);
    }
}

An Example of Collection Initialization

using System.Collections.Generic;
using System;
public class MainClass {
    public static void Main() {
        List<string> presidents = new List<string> { "AAAA", "BBBBBB", "CCCCCCCC" };
        foreach (string president in presidents) {
            Console.WriteLine(president);
        }
    }
}

Explicit Range Variable

using System;
using System.Collections;
using System.Linq;
using System.ruponentModel;
    class MainClass
    {
        static void Main()
        {
            ArrayList list = new ArrayList { "First", "Second", "Third" };
            var strings = from string entry in list
                          select entry.Substring(0, 3);

            foreach (string start in strings)
            {
                Console.WriteLine(start);
            }
        }
    }

Instantiating and Initializing an Anonymous Type Using Object Initialization

using System;
public class MainClass {
    public static void Main() {
        var address = new {
            address = "First Street",
            city = "Vancouver",
            state = "GA",
            postalCode = "99999"
        };
        Console.WriteLine("address = {0} : city = {1} : state = {2} : zip = {3}",
         address.address, address.city, address.state, address.postalCode);
        Console.WriteLine("{0}", address.GetType().ToString());
    }
}

Reflecting result from Linq

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Linq;
using System.Linq;
    class Program
    {
        static void Main(string[] args)
        {
            string[] currentVideoGames = {"q", "2", 
                                  "this is a test", "aaa",
                                  "eee", "System"};
            var subset = from game in currentVideoGames 
                                 where game.Length > 6
                                 orderby game
                                 select game;
            foreach (var s in subset)
                Console.WriteLine("Item: {0}", s);
            Console.WriteLine("resultSet is of type: {0}", subset.GetType().Name);
            Console.WriteLine("resultSet location: {0}", subset.GetType().Assembly);
        
        }
    }

Using var

using System;
using System.Collections.Generic;
using System.Diagnostics;
class MyPC {
    public Int32 Id { get; set; }
    public Int64 Memory { get; set; }
    public String Name { get; set; }
}
class LanguageFeatures {
    static void Main(string[] args) {
        var processes = new List<MyPC>();
        foreach (var process in Process.GetProcesses()) {
            var data = new MyPC();
            data.Id = process.Id;
            data.Name = process.ProcessName;
            data.Memory = process.WorkingSet64;
            processes.Add(data);
        }
        Console.Write(processes);
    }
}