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());
}
}
Month: February 2011
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);
}
}
Anonymous Types
using System;
using System.Collections.Generic;
using System.Diagnostics;
class MainClass{
static void DisplayProcesses(Func<Process, Boolean> match) {
var processes = new List<Object>();
foreach (var process in Process.GetProcesses()) {
if (match(process)) {
processes.Add(new {
process.Id, Name = process.ProcessName,
Memory = process.WorkingSet64
});
}
}
}
static void Main(string[] args) {
DisplayProcesses(process => process.WorkingSet64 >= 20 * 1024 * 1024);
}
}
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);
}
}
Union does appends one sequence to another with duplicates removed:
using System;
using System.Collections.Generic;
using System.Linq;
public class MainClass {
public static void Main() {
int[] seq1 = { 1, 2, 3 };
int[] seq2 = { 3, 4, 5 };
IEnumerable<int> concat = seq1.Concat(seq2); // { 1, 2, 3, 3, 4, 5 }
IEnumerable<int> union = seq1.Union(seq2); // { 1, 2, 3, 4, 5 }
}
}
Use Linq to union two arrays
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
public class MainClass{
public static void Main(){
int[] numbers = {1, 1, 3, 3};
int[] numbers2 = {1, 2, 3, 4};
Console.Write(numbers.Union(numbers2));
}
}
prints the unique elements of two integer arrays
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class MainClass {
public static void Main() {
int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 };
int[] numbersB = { 1, 3, 5, 7, 8 };
var uniqueNumbers = numbersA.Union(numbersB);
Console.WriteLine("Unique numbers from both arrays:");
foreach (var n in uniqueNumbers) {
Console.WriteLine(n);
}
}
}

