I swear I need a job that challenges me to learn more. I've been living in a .NET 3.5 world with 2.0 skills, despite using VS 2008. I haven't scratched the surface of the new things it can do, specifically LINQ. Even in the simplest of class declarations, I learned something new while reading up on LINQ.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace linqTest
{
public class Contact
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class Contacts : List<Contact>
{
}
}
I didn't even know you could declare a property without a corresponding variable! In abstracts, sure, but in an implemented property? Talk about a timesaver.
Then an actual LINQ to XML implementation
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace linqTest
{
class Program
{
static void Main(string[] args)
{
Contacts contacts = new Contacts();
Contact contact1 = new Contact();
contact1.FirstName = "Daffy";
contact1.LastName = "Duck";
Contact contact2 = new Contact();
contact2.FirstName = "Bugs";
contact2.LastName = "Bunny";
contacts.Add(contact1);
contacts.Add(contact2);
// create xml output based on contacts class and display
XElement xe = new XElement("contacts",
from contact in contacts
select new XElement("Contact",
new XElement("FirstName", contact.FirstName),
new XElement("LastName", contact.LastName))
);
Console.WriteLine(xe.ToString() + "\n");
// read contacts in from xml and display
IEnumerable<Contact> importedContacts = from contact in xe.Descendants("Contact")
select new Contact
{
FirstName = contact.Element("FirstName").Value,
LastName = contact.Element("LastName").Value
};
foreach (Contact c in importedContacts)
{
Console.WriteLine(c.FirstName + " " + c.LastName);
}
Console.Read();
}
}
}
And the output
<contacts>
<Contact>
<FirstName>Daffy</FirstName>
<LastName>Duck</LastName>
</Contact>
<Contact>
<FirstName>Bugs</FirstName>
<LastName>Bunny</LastName>
</Contact>
</contacts>
Daffy Duck
Bugs Bunny
I was familiar with de/serializing from/to XML in code, but this could be more useful. And LINQ to XML is just a small part of what LINQ has to offer. I swear, I have to use this stuff somewhere before I fall further behind.
In before "nobody cares."