Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partial class in different namespaces

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)

like image 379
RAMe0 Avatar asked Dec 21 '10 21:12

RAMe0


People also ask

Can partial classes be in different namespaces?

So the short answer to your question is: No.

Can partial classes be in different assemblies?

It's still partial but it allows you to share it between both projects, keep them synchronized and at the same time have version/framework specific code in the partial classes. Show activity on this post. I've had similar issues with this. I kept my partial classes in my Data project so in your case the 'MyProject.

What are partial classes?

A partial class is a special feature of C#. It provides a special ability to implement the functionality of a single class into multiple files and all these files are combined into a single class file when the application is compiled. A partial class is created by using a partial keyword.

Can we inherit partial class in C#?

Inheritance cannot be applied to partial classes.


2 Answers

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?

like image 62
CodingGorilla Avatar answered Sep 29 '22 04:09

CodingGorilla


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

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

like image 29
Abdul Saboor Avatar answered Sep 29 '22 05:09

Abdul Saboor