what's the difference for the following two ways to define namespace?
namespace A.B.C {
public class AA{
}
}
namespace A {
namespace B{
namesapce C{
public class AA{
}
}
}
}
in some where I may have
namespace A{
//some classes
}
namespace A.B {
//some classes
}
namespace A {
namespace B {
//some classes
}
}
Both need to do the same to use class AA by using A.B.C; Can I use C.AA a; to specify the AA class in C namespace or I have to use the fall namespace convention: A.B.C.AA a; to avoid possbile confliction?
They're the same. If you look at this code in .NET Reflector:
namespace A {
namespace B{
namespace C{
public class AA{
}
}
}
}
you get this:
namespace A.B.C
{
public class AA
{
// Methods
public AA();
}
}
Both methods are compiled to exactly the same intermediate language code.
Related
I have a class library project with definition like this:
namespace MyNamespace
{
public static class MyClass
{
public static string GetString()
{
return "test";
}
}
}
When I reference the built .dll of this project in my another project/solution, I can access the class MyClass like MyNamespace.MyClass.GetString() but I can't access by referenceing the namespace on top of c# file. Ex.
using MyNamespace;
namespace ProjectNameSpace
{
public class TestClass
{
public void TestRun()
{
string result = MyClass.GetString(); // Getting error: are you missing an assembly reference?
}
}
}
It may be a silly question, but I am not getting why this is happening. Any help on this is really helpful to me.
Are you sure that the name of the namespace was different from the name of the class in the library? I have just run into a problem similar to this one, and after a few minutes of experimenting I realised that was the problem.
Here's an image showing the existance of the problem. Note that testLib is in references and is also using'd.
Here's the code of the library
namespace testLib
{
public class Class1
{
public Object getMe()
{
return this;
}
}
public static class Class2
{
public static int getRandomNumber()
{
return 7;
}
}
public class testLib
{
public static void testMe()
{
Console.WriteLine("working just fine...");
}
}
}
Here's the code of the tester class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using testLib;
namespace libTest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Class2.getRandomNumber());
testLib.testLib.testMe();
testLib.testMe();//when i hover here, it says "The type or namespace does not exist in the namespace testLib (are you missing an assembly reference?)"
Console.ReadKey();
}
}
}
I have the following:
// Generator.cs
namespace MRP
{
public class Generator
{
public enum ModeGeneration
{
ByRequest,
ByCommit
}
}
}
// CustomerOrderWrapper.cs
namespace MRP
{
class CustomerOrderWrapper
{
readonly ModeGeneration _mode;
}
}
Why am I getting an error that the namespace name 'ModeGeneration' could not be found. Both classes are under MRP namespace, why the compiler is complaining on 'ModeGeneration' namespace?
Read the error message again. It should say something like "can't find class or namespace ModeGeneration", and it's correct, there is no class/namespace named ModeGeneration, maybe you want Generator.ModeGeneration?
As an inner class, Generator.ModeGeneration is the whole type name, and you can't omit the outer class name, because you can define a few inner classes with the same name in C# like this:
namespace Foo
{
class One
{
public class Bar { }
}
class Another
{
public class Bar { }
}
}
You can see Bar is ambiguous. You should use One.Bar and Another.Bar instead.
That is because your enum is defined in the class. Change the code as follows:
namespace MRP
{
class CustomerOrderWrapper
{
readonly Generator.ModeGeneration _mode;
}
}
There are two method to solve this problem
Method 1) You can use class name put in front of enum
namespace MRP
{
public class Generator
{
public enum ModeGeneration
{
ByRequest,
ByCommit
}
}
}
// CustomerOrderWrapper.cs
namespace MRP
{
class CustomerOrderWrapper
{
readonly Generator.ModeGeneration _mode;
}
}
Method 2) You can bring enum from in class to out namespace
namespace MRP
{
public enum ModeGeneration
{
ByRequest,
ByCommit
}
public class Generator
{
}
}
// CustomerOrderWrapper.cs
namespace MRP
{
class CustomerOrderWrapper
{
readonly ModeGeneration _mode;
}
}
I hope it will help you.
Yes they are in the same namespace, but enum ModeGeneration is also inside Generator class. So either:
Put the enum outside the Generator class and use it as you have
or
Prefix ModeGeneration in CustomerOrderWrapper.cs with Generator - Generator.ModeGeneration
I am trying to achieve the namespace hierarchy used in C# in my class library. Here is what I am trying to do:
namespace Parent
{
namespace Child
{
Class ChildClass { }
}
Class ParentClass { }
}
After compiling the class library It did not work as expected. Here is my expected working principle.
To access the ChildClass one has to using Parent.Child. But one can access ParentClass just by using Parent.
I can do this without compiling the class library but adding the cs file to the project. But when I compile as DLL and add it as a reference in a project I can't access the sub-namespaces.
UPDATE: I have different files for each class. When I write all namespaces and classes into one file it seems to work. But why?
Is there anyway to achieve this in C#?
I think your classes missing public; Following code works for me.
namespace Parent
{
namespace Child
{
public class ChildClass { }
}
public class ParentClass
{
}
}
I can create;
Parent.ParentClass p;
Parent.Child.ChildClass c;
Which is your expected working principle.
EDIT: separate cs file for each class approach;
ParentClass.cs
namespace Parent
{
public class ParentClass{ }
}
ChildClass.cs
namespace Parent
{
namespace Child
{
public class ChildClass { }
}
}
This seems to be working for me.
You are nesting classes and namespaces and it all seems a little confused. Why don't you keep a flatter namespace structure and do the nesting in your classes. Keep in mind that you don't need to nest namespaces or classes to maintain a parent child relationship.
Have a read of the following: Parent child class relationship design pattern
This should get you started in the right direction:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication2
{
using System;
using System.Collections.Generic;
public class ChildClass
{
private ParentClass parent;
public ChildClass(ParentClass parentIn)
{
parent = parentIn;
}
public ParentClass Parent
{
get { return parent; }
}
}
public class ParentClass
{
private List<ChildClass> children;
public ParentClass()
{
children = new List<ChildClass>();
}
public ChildClass AddChild()
{
var newChild = new ChildClass(this);
children.Add(newChild);
return newChild;
}
}
public class Program
{
public static void Main()
{
Console.WriteLine("Hello World");
var p = new ParentClass();
var firstChild = p.AddChild();
var anotherChild = p.AddChild();
var firstChildParent = firstChild.Parent;
var anotherChildParent = anotherChild.Parent;
}
}
}
so I have a library Mine.SuperFun which calls stuff in the library SuperFun whose main namespace is SuperFun. The problem i'm having is that i can't address classes or basically anything in the SuperFun library inside classes in the Mine.SuperFun.XyZFoo namespaces
The only way to address them i have is doing stuff like:
using SuperFun_NiceClass = SuperFun.NiceClass;
using Mine.SuperFun {
...
SuperFun_NiceClass.DoStuff()
is there something i can do (besides changing the namespace in Mine library) to be able to address those classes directly?
You can use the global contextual keyword
What is the usage of global:: keyword in C#?
http://msdn.microsoft.com/en-us/library/cc713620.aspx
namespace Mine.SuperFun
{
public class My { public int a; }
}
namespace SuperFun
{
public class Theirs { public int a; }
}
namespace SomeProgram
{
public class Program
{
SuperFun.Theirs theirs;
global::Mine.SuperFun.My mine;
}
}
I have a name space Company.Controls, which contains several controls. I also have a class called "Common" which contains enums/structures/static methods that I use throughout the controls.
Is there a way to make these "Common" peices belong to the Company.Controls namespace this way I don't have to keep typing "Common.Structure"? Essentially having he "Common" both a namespace and a class.
Just seems messy and confusing when reading the code.
example (all the other controls are in the Blah.Controls.Common namespace)
namespace Blah.Controls
{
public enum ControlTouchState
{
Down = 0x00,
Up = 0x01,
}
public Common()
{
//Stuff here
}
}
Thanks.
You can't get exactly what you want; in C# all methods have to be in a class.
Depending on what is in your Common class, you might be able to find something a slightly more satisfying by using extension methods:
namespace Blah.Controls
{
public class CommonControl { }
public static class Common
{
public static void Foo(this CommonControl cc) { }
}
public class Control1 : CommonControl
{
public void Bar()
{
this.Foo();
}
}
}
Another thing you might consider is using partial classes which would let you write simple wrappers elsewhere:
namespace Blop.Controls
{
public static class Common
{
public static void Foo() { }
}
public partial class Control1
{
public void Bar()
{
Foo();
}
}
public partial class Control1
{
public void Foo()
{
Common.Foo();
}
}
}
Obviously, introducing some inheritence could eliminate some of the duplication; I'm assuming you don't want to do that.
Is there some reason that the nested types in Common MUST be nested? Why not separate them out into their own namespace?
namespace Common
{
public struct Structure
{
// ...
}
public enum Enumeration
{
// ...
}
public class Common
{
// ...
}
}
You could then use the Common namespace as such:
namespace Blah.Controls
{
using Common;
class Control
{
Struct myStruct;
Enumeration myEnum;
Common myCommon; // references the class, not the namespace
}
}