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)
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?
Partial class is only possible in same namespace and same assembly.
Namespace could be in two different assemblies but partial class could not.
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#
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.
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.
Restrictions on partial classes and method from MSDN https://msdn.microsoft.com/en-us/library/wa80x488.aspx
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#.
Related
So I have two different source files:
file1.cs:
namespace namespace1 {
public partial class Class1 {
public partial class NestedClass {
public int myInt{ get; set; }
}
}
}
file2.cs:
namespace namespace1.Class1 {
public partial class NestedClass {
void doSomething() {
Console.WriteLine(this.myInt); // class does not contain definition for myInt
}
}
}
The Problem:
I am trying to access a member variable declared in the first partial class in the second. No variables I declare can be viewed from the other file.
My Attempts at a Solution:
I found this post, but it did not seem to fix my issue. I declared several test variables in each of the partial class files and nothing was visible to the other file. I tried both public and private variables, with and without setters, since the problem in that situation involved a missing setter. I thought maybe my classes were named incorrectly, so I triple checked my namespaces as well as class names and that they were both declared partial. Finaly, I have tried restarting Visual Studio as well, to no avail.
Any help would be greatly appreciated!
The problem is that in your first file, you have a namespace:
namespace namespace1 {
whereas in the second it is:
namespace namespace1.Class1 {
Since the namespaces are different, C# considers the classes to be different as well: they happen to have the same name, but since these are declared in separate namespaces, C# considers them to be different.
In the case you work with partial nested classes, you should write it like:
file1.cs:
namespace namespace1 {
public partial class Class1 {
public partial class NestedClass {
public int myInt{ get; set; }
}
}
}
file2.cs:
using System;
namespace namespace1 {
public partial class Class1 {
public partial class NestedClass {
void doSomething() {
Console.WriteLine(this.myInt); // class does not contain definition for myInt
}
}
}
}
So you do not specify the outer class as a namespace, but you declare the outer class twice.
I have a class in a folder in my namespace as:
using System;
namespace mynamespace.myFolder1
{
public class F1
{
// class properties...
// class methods...
public class F1_Nest
{
// class properties...
// class methods...
}
}
Now I have to access class F1_Nest as from:
using System;
using mynamespace.myFolder1;
namespace mynamespace.myFolder2
{
public class F2
{
// class properties...
// class methods...
// I have to access nested class F1_Nest here...
}
}
How will I use/access class F1_Nest inside class F2?
A nested class is just like a normal class, except it's definition resides within the containing class. The result is that the containing class is almost like a namespace for the nested class. You don't have to instantiate the containing class in order to use the nested class. For instance:
namespace MyNamespace
{
public class MyParentClass
{
public class MyNestedClass
{
}
}
}
Now you can instantiate it somewhere else like this:
MyNamespace.MyParentClass.MyNestedClass x =
new MyNamespace.MyParentClass.MyNestedClass();
The purpose of this, like namespaces, is for organization. It's up to you as the designer whether or not you use nested classes instead of a flat class structure, but it can be convenient when you have a class whose meaning, definition, or usage is inextricably tied to the another class and doesn't have any relevance or meaning outside of that class.
For example, imagine you have a Widget class which has an ID property that's more than just a simple int or Guid...you might have a WidgetID enumeration, for example that could reside as a nested class in Widget.
To access nested classes you need to do the following:
using System;
using mynamespace.myFolder1;
namespace mynamespace.myFolder2
{
public class F2
{
// class properties...
// class methods...
void SomeMethod()
{
// no need to instantiate an object of F1 class
var f1Nest = new F1.F1_Nest();
}
}
}
The nested class F1_Nest can be accessed in class F2 as:
using System;
using mynamespace.myFolder1;
namespace mynamespace.myFolder2
{
public class F2
{
// class properties...
// class methods...
// I have to access nested class F1_Nest here...
void F2_Method()
{
F1.F1_Nest();
}
}
}
Declare the object of F1_Nest as follows
F1.F1_Nest objYourObjectVariableName=new F1.F1_Nest();
In the example below, they are conflicting class names for ClassA. Is there any way I can use OutsideLibrary.ClassA without needing the alias? Maybe put the alias on the other ClassA somehow?
using alias1 = OutsideLibrary
namespace Any.Namespace.01
{
public class ClassA
{
public void Method1(){
alias1::ClassA.DoOperation();
}
}
}
namespace Different.Namespace
{
public class ClassA { ... }
}
I think that there is no way to achieve what you want.
The closest thing you can do is create an alias directly to the referenced class:
using OtherClassA = OutsideLibrary.Class1;
namespace Any.Namespace.01
{
public class ClassA
{
public void Method1()
{
OtherClassA.DoOperation();
}
}
}
Can you use the class including the full namespace? Example:
var classA1 = new Any.Namespace.01.ClassA();
var classA2 = new Different.Namespace.ClassA();
I have a question regarding inheritance in C#. I have a public abstract class A that defines protected virtual members. I have another abstract class B that inherits from A, and then another internal class C, inheriting from B. The first class A is in a separate namespace, and the other two are in the same namespace, but include a reference to the first.
I was trying to access the protected virtual member of A in C using base keyword, but am unable to unless I provide a method in B and then call A's protected virtual member there.
I want to know if what I was trying to do is possible, i.e. was I doing something incorrectly? Or if its not possible, then why?
Thanks.
Here's the code sample :
namespace A
{
public abstract class BaseClass
{
protected virtual string GetData()
{
//code to get data
}
}
}
namespace B //includes a reference to A
{
abstract class DerivedClassA : BaseClass
{
}
internal class DerivedClassB: DerivedClassA
{
public void write()
{
base.GetData(); // results in error.
// The name 'GetData' does not exist in the current context
// and DerivedClassA does not contain a definition for 'GetData'
}
}
Other than a namespace issue your code ran fine, the one thing I had to change was change DerivedClassA to
abstract class DerivedClassA : A.BaseClass
{
}
What I suspect is whatever BaseClass is called in your real project you have a class of the same name in namespace B or some other namespace you have included in your using statements at the top of your code. For example this would recreate your error
namespace A
{
public abstract class BaseClass
{
protected virtual string GetData()
{
throw new NotImplementedException();
}
}
}
namespace B //includes a reference to A
{
public abstract class BaseClass
{
}
abstract class DerivedClassA : BaseClass
{
}
internal class DerivedClassB: DerivedClassA
{
public void write()
{
base.GetData(); // results in error.
// The name 'GetData' does not exist in the current context
// and DerivedClassA does not contain a definition for 'GetData'
}
}
}
Check that you don't have another namespace that uses the same name as BaseClass or explicitly define the namespace of BaseClass in DerivedClassA
This works for me.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Protectedmemberinabstractclassex
{
class Program
{
static void Main(string[] args)
{
}
}
}
namespace A
{
public abstract class BaseClass
{
protected virtual string GetData()
{
throw new NotImplementedException();
}
}
}
namespace B //includes a reference to A
{
abstract class DerivedClassA : A.BaseClass
{
}
internal class DerivedClassB : DerivedClassA
{
public void write()
{
base.GetData();
}
}
}
Got the source of the problem. Apparently, the location of the dlls that the second namespace was referring to had changed and hence, it was pointing to an outdated reference. After I updated the references from the correct location, it ran fine. Thanks all for the help.
i wanted to know as i start to make some dramatic move from using regular helpers to more and more extension methods so they start to pile up ,
say my top main namespace is as follows :
the main namespace name is myname + last digit of year and current month
thats how i keep it organized as a helper namespace
namespace "myname212"
{
namespace DbRelated
{
some clasess & methods
}
namespace styling
{
same as usual ..
}
// .... some more categories and...then
//the extentions namespace
namespace CustomExtentions
{
// simplest ext class and its first method
public static class ToNumber
{
public static int Toint(this Textbox TbxToConvrt)
{
return Convert.ToInt32(TbxToConvrt.Text);
}
//some more of same subject
}
}
}
but what if i have a more general catefory that has it own sub category logocally
namespace Extentions
{
public static class MainCategory
{
public static class SubCat1
{
public static some_method();
}
public static class SubCat2
{
public static some_method();
}
}
}
the a hierarchical stracture above will not work .
so is it true that if i'd like to build more categories i could only do it via
nested namespaces instead of nested classes ?
is this what you do ?
You can nest namespaces instead:
namespace My.Nested.Namespace.So.Far
{
public static class BlaExtensions
{
}
}
or even:
namespace My.Nested.Namespace
{
public static class FooExtensions
{
}
namespace So.Far
{
public static class BlaExtensions
{
}
}
}
should work too.