Some Simple Samples
I have been playing with the new .NET 3.5 framework. Several people asked to see that samples that I was writing just to try stuff out so I decided to make a little post. This is just some simple code I wrote to teach myself the basics so its nothing special. I should have some LINQ samples up later. If you have any questions feel free to ask.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Sample
{
class Program
{
static void Main(string[] args)
{
//create some sample objects
var p1 = new Person { firstName = "Matthew", lastName = "Osborn" };
var p2 = new Person { firstName = "Mark", lastName = "Osborn" };
var p3 = new Person { firstName = "Karen", lastName = "Osborn" };
var d1 = new Dog { Breed = "Shetland Sheep Dog", Name = "Baxtor", Owner="Matthew" };
var d2 = new Dog { Breed = "Shetland Sheep Dog", Name = "Rip", Owner = "Matthew" };
//add these objects to two different collections
var people = new List<Person>();
people.Add(p1);
people.Add(p2);
people.Add(p3);
var dogs = new List<Dog>();
dogs.Add(d1);
dogs.Add(d2);
//join people and dogs on owner first name
var test =
people.Join(dogs, p => p.firstName, d => d.Owner, (p,d) => new { p, d });
//print out results
foreach (object o in test)
o.print();
}
}public class Person
{
public string firstName { get; set; }
public string lastName { get; set; }public override string ToString()
{
return firstName + " " + lastName;
}
}public class Dog
{
public string Name { get; set; }
public string Breed { get; set; }
//referances a persons first name
public string Owner { get; set; }public override string ToString()
{
return Name + " is a " + Breed;
}
}/// <summary>
/// Static class to hold all my extension methods
/// </summary>
public static class Extensions
{
//generic extension method to print items to the console
//This can easily be used for debuging
public static void print<T>(this T obj)
{
Console.WriteLine(obj.ToString());
}
}
}