Partial classes in different namespace are not being recognized correctly

TruMan1 picture TruMan1 · Jan 21, 2013 · Viewed 8.5k times · Source

I have a partial class that is split over two namespaces. The problem is that if I have an interface implemented on one of the partials, it is not recognized on the counterpart partial class. For example, I would expect the below to return true for being recognized as ILastModified (C# fiddle at http://ideone.com/heLDn0):

using System;
using MyNamespace.One;
 
public class Test
{
    public static void Main()
    {
        var item = new Product();
        Console.WriteLine(item is ILastModified); //RETURNS FALSE??!
    }
}
 
interface ILastModified
{
    DateTime LastModified { get; set; }
}
 
namespace MyNamespace.One
{
    public partial class Product
    {
        public int ID { get; set; }
    }
}
 
namespace MyNamespace.Two
{
    public partial class Product : ILastModified
    {
        public DateTime LastModified { get; set; }
    }
}

Answer

Z. Danev picture Z. Danev · Jan 21, 2013

You cannot have a partial class in two different namespaces. The compiler treats those as two different classes.