Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partial classes in different namespace are not being recognized correctly

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; }
    }
}
like image 234
TruMan1 Avatar asked Jan 21 '13 19:01

TruMan1


People also ask

Can partial classes be in different namespaces?

All parts of a partial class should be in the same namespace. Each part of a partial class should be in the same assembly or DLL, in other words you can't create a partial class in source files of a different class library project. Each part of a partial class has the same accessibility.

Can partial class have different names in different files?

The source file name for each part of the partial class can be different, but each partial class's name must be the same. The name of all parts of a partial class should be the same. All parts of a partial class should be in the same assembly.

Can two classes have same partial name?

In your case it will raise an error mentioning you already have defined a method with the same name. One modern caveat - that code would be totally fine if the partial classes were split into 2 separate files meant for 2 separate platform targets in a multi-targeted .

What is partial class in C# stackoverflow?

Partial classes allow the generator to simply emit the code they need to emit and they do not have to deal with user edits to the file. Users are likewise free to annotate the class with new members by having a second partial class. This provides a very clean framework for separation of concerns.


2 Answers

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

like image 153
Z.D. Avatar answered Sep 24 '22 07:09

Z.D.


I have a partial that is split over two namespaces.

You can't. By being in different namespaces, they are different classes.

Consider that this is the reason namespaces exist - so you can have the same class name for different classes.

From the C# Language Specification (C# 4.0), §10.2, Partial types:

Each part of a partial type declaration must include a partial modifier. It must have the same name and be declared in the same namespace or type declaration as the other parts.

(emphasis mine)

So, by definition, what you are doing is not a partial type.

like image 37
Oded Avatar answered Sep 21 '22 07:09

Oded