Using DefaultIfEmpty with an object?

I saw an example on MSDN where it would let you specify the default value if nothing is returned. See below:

List<int> months = new List<int> { };
int firstMonth2 = months.DefaultIfEmpty(1).First();

Is it possible to use this functionality with an object? Example:

class object
{
  int id;
  string name;
}

code:

List<myObjec> objs = new List<myObjec> {};
string defaultName = objs.DefaultIfEmpty(/*something to define object in here*/).name;

UPDATE:

I was thinking I could do something like this:

List<myObjec> objs = new List<myObjec> {};
string defaultName = objs.DefaultIfEmpty(new myObjec(-1,"test")).name;

But haven't been able to. It should be noted that I am actually trying to use this method on an object defined in my DBML using LINQ-To-SQL. Not sure if that makes a difference in this case or not.


You need to pass an instantiated class as a parameter of the DefaultIfEmpty.

class Program
{
    static void Main(string[] args)
    {
        var lTest = new List<Test>();
        var s = lTest.DefaultIfEmpty(new Test() { i = 1, name = "testing" }).First().name;
        Console.WriteLine(s);
        Console.ReadLine();

    }
}

public class Test
{
    public int i { get; set; }
    public string name { get; set; }
}

To add to it and make it a bit more elegant (IMO) add a default constructor:

class Program
{
    static void Main(string[] args)
    {
        var lTest = new List<Test>();
        var s = lTest.DefaultIfEmpty(new Test()).First().name;
        Console.WriteLine(s);
        Console.ReadLine();

    }
}

public class Test
{
    public int i { get; set; }
    public string name { get; set; }

    public Test() { i = 2; name = "testing2"; }
}