Partial class in different namespaces
Asked Answered
U

7

84

Can I create partial class in different namespaces? Will it work correct? e.x.:

class1.cs

namespace name1
{
    public partial class Foo
    {
        Bar1(){
            return 10;
        }
    }
}

class2.cs

namespace name1.name2
{
    public partial class Foo
    {
        Bar2(){
            return 100;
        }
    }
}

main.cs

using name1;
using name1.name2;

namespace mainClass
{
    public class mainClass
    {
        Foo classFoo = new Foo();
        int Count = classFoo.Bar1() + classFoo.Bar2();
        // Will Count = 110?
    }
}

What should I do to make it work? (if my example not correct)

Understand answered 21/12, 2010 at 21:52 Comment(0)
F
108

A class's name includes it's namespace, so name1.Foo and name1.name2.Foo are two completely separate types. So the short answer to your question is: No.

Why do you need to do something like this?

Footplate answered 21/12, 2010 at 21:54 Comment(7)
I want to make separate libraries. To add new functionality to main class without modifying it. I have huge hole in my mind related with this topic...Understand
Well, I would suggest two possibilities. The first would be to create a sub-class for name1.Foo in name1.name2.Foo. The second would be to use extension methods (msdn.microsoft.com/en-us/library/bb383977.aspx) which is a great way to "add" functionality to classes you either don't control, or for whatever reason, don't want to modify.Footplate
@RAMe0: Partial classes are resolved at compile time, there is no sense of a Partial type in .NET its a language construct. It allows the source code of a class to appear in multiple source files during compilation.Dodie
(Here is what I want)[docs.google.com/document/d/… I just thought about 1 thing... If I will create projects and call them like "Name1.n2","Name1.n3", "Name1.n4".... But with 1 namespace and partial class, will dll's names looks like "Name1.n2", "Name1.n3"... And can I will use it like in example doc?Understand
@Understand I suggest use inheritance. It's maybe the solutionZennas
Why I need something like this: I have classes generated from WSDL specification. I want to persist them using Entity Framework in case the client fails and a retry must be made later.. This requires the classes to have an Id attribute. I rather not create a partial class within a generated namespace.Fairlead
@Dodie that's a false statement. Partial classes allow you to extend classes which are autogenerated and may be recreated in the future without fear of losing your changes. Inheritance does a different thing, so it really is a design choice.Morphophonemics
T
35

Partial class is only possible   in same namespace and same assembly.

Namespace could be   in two different assemblies but partial class could not.

Trickle answered 19/12, 2012 at 7:41 Comment(0)
B
19

Here are some point to consider while implementing the partial classes:-

  • Use partial keyword in each part of partial class.

  • Name of each part of partial class should be the same but source file name for each part of partial class can be different.

  • 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. (like private, public or protected)

  • If you inherit a class or interface on a partial class then it is inherited on all parts of a partial class.

  • If a part of a partial class is sealed then the entire class will be sealed.

  • If a part of partial class is abstract then the entire class will be considered an abstract class.

Partial Classes in C#

Butyrate answered 27/6, 2014 at 6:19 Comment(0)
B
7

This will not work. The compiler will give you an ambiguous name error on the Foo classFoo = new Foo(); line. For partial classes to work, they must be in the same namespace because the namespace is actually part of the fully qualified name of the type.

Baumgardner answered 21/12, 2010 at 21:58 Comment(2)
I understand this, that's why I'm asking... please read comment to previous answerUnderstand
@Coding Gorilla's comment is correct. If you simply want to add methods to a type, either subclassing (if the type is not sealed) or using extension methods is the way to go.Baumgardner
L
5

Also, for static classes you can implement something like this with the help of fresh C# 6.0 using static feature.

Consider:

namespace SomeLogic1
{
    public static class Util
    {
        public static int Bar1()
        {
            return 1;
        }
    }
}

namespace SomeLogic2
{
    public static class Util
    {
        public static int Bar2()
        {
            return 2;
        }
    }
}

namespace GeneralStuff
{
    using SomeLogic1;
    using SomeLogic2;

    public class MainClass
    {
        public MainClass()
        {
            // Error CS0104
            // 'Util' is an ambiguous reference between 'SomeLogic1.Util' and 'SomeLogic2.Util'
            var result = Util.Bar1() + Util.Bar2(); 
        }
    }
}  

Right, that does not compile, the error message is clear. To fix the situation you can directly specify namespaces (but you don't want this as far as I understand):

namespace GeneralStuff
{
    public class MainClass
    {
        public MainClass()
        {
            var result = SomeLogic1.Util.Bar1() + SomeLogic2.Util.Bar2(); 
        }
    }
}

OR you can apply using static feature this way:

namespace GeneralStuff
{
    using static SomeLogic1.Util;
    using static SomeLogic2.Util;

    public class MainClass
    {
        public MainClass()
        {
            var result = Bar1() + Bar2(); 
        }
    }
}

Perhaps it is ok to do this for some helper/utils classes. But partial classes are not the way, as other have noticed.

Luck answered 25/10, 2016 at 17:9 Comment(0)
T
2

Restrictions on partial classes and method from MSDN https://msdn.microsoft.com/en-us/library/wa80x488.aspx

Trafalgar answered 27/3, 2015 at 12:37 Comment(0)
A
1

I am assuming your main goal was to distribute the methods amongst different namespaces, otherwise it would have been trivial (put everything in one class whether partial or not and you're done).

So the assumed objectives are:

  • Have the 2 methods Bar1 in namespace name1 and Bar2 in namespace name1.name2
  • Be able to invoke any of the above methods in the context of one class, here ClsFoo

You can't achieve this with partial classes, but you can achieve it in a different way: if you use  extension methods  and bind them to a particular class, here ClsFoo, then you can do the following:

using SomeOtherNamespace;
using name1;
using name1.name2;

namespace mainClass
{
    public static class mainClass
    {
        public static void Main()
        {
            var classFoo = new ClsFoo();
            var count = classFoo.Bar1() + classFoo.Bar2();
            Console.WriteLine($"count = {count}"); // output is 110
        } // main               
    } // class
} // namespace

namespace SomeOtherNamespace
{
    public class ClsFoo
    {
        // does not need to contain any code
    } // class
} // namespace

namespace name1
{
    public static class FooExt
    {
        public static int Bar1(this ClsFoo foo)
        {
            return 10;
        } // method
    } // class
} // namespace

namespace name1.name2
{
    public static class FooExt
    {
        public static int Bar2(this ClsFoo foo)
        {
            return 100;
        } // method
    } // class
} // namespace

Run it online

This way, you declare an empty class ClsFoo and then write some extension methods Bar1() and Bar2(), which reside in different namespaces and static extension classes.

Note: The extension classes may have the same name FooExt as long as they are in different namespaces, of course you can also give them different names like FooExt1 and FooExt2 if you like - and the example will still work; even in older versions of C#.

Antic answered 31/3, 2021 at 12:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.