Manage namespace library (C#) - c#

I am trying to organize a library. My issue is that it would quickly gets very large.
This is what i have so far:
namespace MyLibrary {
namespace Math {
namespace Geometry {
public class BezierCurve {
//...
};
}
namespace Combinatorics {
}
}
namespace Collections {
}
//...
}
Its not many lines of code now, but since BezierCurve alone is around 200 lines, creating all of the classes in the above document is not going to be the way to go.
Is it possible to somehow specify the content of BezierCurve in another document and include / refer to it in the namespace Geometry?

Multiple files can declare classes in the same namespace. There is no requirement for a namespace to be fully defined within a single file.
You should have a file per class.
e.g. your BezierCurve would typically be in:
MyLibrary/Math/Geometry/BezierCurve.cs
and be declared within
namespace MyLibrary.Math.Geometry
{
public class BezierCurve
{
}
}

It's recommended to have one type per file (well, with some exceptions). Thus you will have a single namespace without all this nested structure:
namespace MyLibrary.Math.Geometry
{
public class BezierCurve
{
// ...
}
}
If you want to refer BezierCurve in another file, just add a using directive with appropriate namespace:
using MyLibrary.Math.Geometry;
namespace MyLibrary.Math.Combinatorics
{
public class SomeClassFromCombinatoricsWhichUsesBezierCurve
{
// ...
}
}

Related

Two namespaces with the same object name

I have two namespaces:
System.Numerics and UnityEngine
Both have the type Vector3.
So now when ever i want to use it i have to declare which namespace before it. Like this:
protected struct CVN
{
public Complex h;
public UnityEngine.Vector2 d;
public UnityEngine.Vector3 n;
}
Is there any way to define that i only want Vector3 from one namespace so i don't have to always type NameSpaceHere.Vector3 every single time ?
Or am i stuck with the tedious nature of stating the namespace every time. Especially since i only need the Complex type from Numerics its quite annoying.
If all you need from System.Numerics is Complex, then:
using UnityEngine;
using Complex = System.Numerics.Complex;
At the top of your file, without using System.Numerics; should do it.
This is an alias.
You can wrap the using directive of the wanted class in the namespace of your current class rather than putting it outside.
Consider this example
namespace System.Numerics
{
class MyClass
{
}
}
namespace UnityEngine
{
class MyClass
{
}
}
using System.Numeric;
namespace ConsoleApplication24
{
using UnityEngine; // inside the namespace
class Program
{
static void Main(string[] args)
{
MyClass xx = new MyClass(); // from UnitEngine instead of System.Numeric
}
}
}

In C# are the `using` directives of the base class inherited by the child class?

Let's say we have a base class Rectangle and a derived class Square:
namespace Shapes {
using System.Foo;
public class Rectangle {
public Rectangle(int l, int w){}
}
}
namespace Shapes {
public class Square : Rectangle
public Square(int l, int w){}
}
Does the Square class have to explicitly say that it is using System.Foo? I'm getting erratic results. In one project the using directives seem to be inherited and in a web application they aren't.
using statements, in this context, don't compile to code -- they are helpers to make your code read cleaner for others. As a result, they are not "inherited".
So, to answer your question, your Square class needs to reference System.Foo - either with a using statement, or by using a fully qualified class name.
A using statement will only propagate to the next set of closing braces (}) from the level it was declared on within the same the file.
//From File1.cs
using System.Baz;
namespace Example
{
using System.Foo;
//The using statement for Foo and Baz will be in effect here.
partial class Bar
{
//The using statement for Foo and Baz will be in effect here.
}
}
namespace Example
{
//The using statement for Baz will be in effect here but Foo will not.
partial class Bar
{
//The using statement for Baz will be in effect here but Foo will not.
}
}
//From File2.cs
namespace Example
{
//The using statement for Foo and Baz will NOT be in effect here.
partial class Bar
{
//The using statement for Foo and Baz will NOT be in effect here.
}
}
using directives are only shared if the classes are in the same file and they are not nested in the classes themselves like in your example.
For instance:
using System.Foo;
namespace N
{
class A {}
class B {}
}
If this is all in one file, A and B can both use Foo.
I think everyone is missing the point when it comes to using directives. They really have nothing to do with the class at all. using directives in a code file (.cs, .vb, etc...) are not part of the classes defined within the file. They are used by the compiler to resolve namespaces when compiling.
using System.Foo;
namespace Shapes {...
Importing should always be top most and not within a namespace. This will allow the entire structure of the class to rely on that import when needed.

How to declare and use the namespace of one class into another class

I Have created two .cs files with namespaces ,classes and methods . I want to call the classes of one .cs file into another .cs file. Can u help me how to declare namespace and use the namespace so that i can call the classes of the preceding .cs file.
Please forgive if my explanation is not correct.
Suppose i have the following code.
ClassFile1
using system
namespace namespace1
{
class c1
{
Methods()
}
}
ClassFile2
using system
//here i need to declare the namespace1 .Can u help me how to declare namespace1 in this ClassFile2//
namespace namespace2
{
class c2
{
Methods()
}
}
You can reference the fully-qualified name of the class:
namespace SecondNamespace
{
public class SecondClass
{
private FirstNamespace.FirstClass someObject;
}
}
Or you can add a using directive to the file (note, this is at the file level, not the class level) to include a specific namespace when resolving type names:
using FirstNamespace;
namespace SecondNamespace
{
public class SecondClass
{
private FirstClass someObject;
}
}
Taken from here:
namespace SampleNamespace
{
class SampleClass
{
public void SampleMethod()
{
System.Console.WriteLine(
"SampleMethod inside SampleNamespace");
}
}
// Create a nested namespace, and define another class.
namespace NestedNamespace
{
class SampleClass
{
public void SampleMethod()
{
System.Console.WriteLine(
"SampleMethod inside NestedNamespace");
}
}
}
class Program
{
static void Main(string[] args)
{
// Displays "SampleMethod inside SampleNamespace."
SampleClass outer = new SampleClass();
outer.SampleMethod();
// Displays "SampleMethod inside SampleNamespace."
SampleNamespace.SampleClass outer2 = new SampleNamespace.SampleClass();
outer2.SampleMethod();
// Displays "SampleMethod inside NestedNamespace."
NestedNamespace.SampleClass inner = new NestedNamespace.SampleClass();
inner.SampleMethod();
}
}
}
Note also that sometimes in addition to the "using" entry (I'm not quite clear on how you app is structured, if it's all one project this is probably moot) you may also need to add the reference. Also not sure what environment you're using. From VSExpress while in the project/file that's the recipient click on Project - Add Reference, select solution and then select your namespace.

Is it possible to do relative C# namespace reference?

I have namespaces:
MyProject.Core.Db
MyProject.Core.Model
And I have classes:
MyProject.Core.Db.User
MyProject.Core.Model.User
Is it possible something like:
using MyProject.Core;
namespace MyProject.BLL
{
public class Logic
{
public static void DoSomething()
{
var userEntity = new Db.User();
var userModel = new Model.User();
}
}
}
I just want to avoid using suffixes in class names (UserModel, UserEntity).
Is it possible to do in somehow in C#?
I don't understand why people say it's not possible. Surely it is possible, you just need to be a bit more specific in the namespaces when you create the target classes (ie you can omit only the common part of the namespace):
namespace MyProject.Core.Db
{
public class User
{
}
}
namespace MyProject.Core.Model
{
public class User
{
}
}
namespace MyProject.BLL
{
public class Logic
{
public static void DoSomething()
{
var foo = new Core.Db.User();
var boo = new Core.Model.User();
}
}
}
The way you're avoiding a fully qualified name within BLL is by being inside of a common namespace with the other two.
What you're trying to achieve is not possible. The closest thing you will get is a using alias directive which looks like this:
using User = Myproject.Core.Db.User;
This will remove the need to fully qualify the path for Myproject.Core.Db.User. You will still need to specify the fully qualified path for at least one of the classes, though. You could create another alias for the other type as Servy demonstrated but at this point I would just rename the classes.
I think the real solution here is to give your classes more descriptive identifiers.
C# does support relative namespace references.
In your case, that means if you're in the namespace MyProject.Core, you can references your classes as Db.User and Model.User. But if you're in the namespace MyProject.BLL, you have to include the Core prefix (Core.Db.User and Core.Model.User).
If that's not good enough for your and you don't want to change your namespace structure, your best choice is probably to add usings to all files that use the types in question.
using DbUser = MyProject.Core.Db.User;
using ModelUser = MyProject.Core.Model.User;
One thing you can do, and we probably should do a lot more, is to specify usings relative to the current namespace. To do this, just move your usings inside the namespace declaration. It doesn't fix your stated problem, but the shorter relative paths are less brittle and your project will be easier to refactor.
namespace MyProject.Core{
using Db;
using Model;
You can add an alias for the one class that you don't import the namespace of:
using MyProject.Core.Db;
using ModelUser = MyProject.Core.Model.User;
namespace MyProject.BLL
{
public class Logic
{
public static void DoSomething()
{
var userEntity = new User();
var userModel = new ModelUser();
}
}
}
In C# it's not possible to use the example that's shown; it's simply not a supported feature.

C# subclass while maintaining name. Deep voodoo?

I have a dll that I'm working with, it contains a class foo.Launch. I want to create another dll that subclasses Launch. The problem is that the class name must be identical. This is used as a plugin into another piece of software and the foo.Launch class is what it looks foe to launch the plugin.
I've tried:
namespace foo
{
public class Launch : global::foo.Launch
{
}
}
and
using otherfoo = foo;
namespace foo
{
public class Launch : otherfoo.Launch
{
}
}
I've also tried specifying an alias in the reference properties and using that alias in my code instead of global, that also didn't work.
Neither of those methods work. Is there a way I can specify the name of the dll to look in within the using statement?
You'll need to alias the original assembly and use an extern alias to reference the original assembly within the new one. Here's an example of the use of the alias.
extern alias LauncherOriginal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace foo
{
public class Launcher : LauncherOriginal.foo.Launcher
{
...
}
}
Here's a walkthrough that explains how to implement that.
Also, you'd mentioned that you tried to use an alias before and encountered problems but you didn't say what they were, so if this won't work then please mention what went wrong.
as Chris said, you can use an alias on your original assembly.
If you can't you that, then you might be able to cheat by using a 3rd assembly
Assembly1.dll (your original)
namespace foo {
public class Launch {}
}
Assembly2.dll (dummy)
namespace othernamespace {
public abstract class Dummy: foo.Launch {}
}
Assembly3.dll (your plugin)
namespace foo{
public class Launch: othernamespace.Dummy{}
}
I'm not even proud of this!
Class name can be identical if it's defined in another namespace, but it boggles the mind why anybody would want to do that to themselves.
Maybe you need to use extern aliases.
For example:
//in file foolaunch.cs
using System;
namespace Foo
{
public class Launch
{
protected void Method1()
{
Console.WriteLine("Hello from Foo.Launch.Method1");
}
}
}
// csc /target:library /out:FooLaunch.dll foolaunch.cs
//now subclassing foo.Launch
//in file subfoolaunch.cs
namespace Foo
{
extern alias F1;
public class Launch : F1.Foo.Launch
{
public void Method3()
{
Method1();
}
}
}
// csc /target:library /r:F1=foolaunch.dll /out:SubFooLaunch.dll subfoolaunch.cs
// using
// in file program.cs
namespace ConsoleApplication
{
extern alias F2;
class Program
{
static void Main(string[] args)
{
var launch = new F2.Foo.Launch();
launch.Method3();
}
}
}
// csc /r:FooLaunch.dll /r:F2=SubFooLaunch.dll program.cs

Categories

Resources