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.
Related
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
{
// ...
}
}
I want to know which is the default namespace of class defined by the programmer,if not specify any?
Let, take the case of Java.
In Java, if Programmer is not specify any package then default package is the java.lang
Same way i want to know about default namespace of C# classes.
global is the default namespace.
Example:
class GlobalClass {
}
namespace MyNamespace {
class NameSpaceClass {
public NameSpaceClass() {
var globalObj = new global::GlobalClass();
}
}
}
MSDN Link: http://msdn.microsoft.com/en-us/library/cc713620.aspx
I'm new to C# and I can't seem to find any info on this so I will ask it here.
Do classes in namespaces have to ever be declared?
using System;
public class myprogram
{
void main()
{
// The console class does not have to be declared?
Console.WriteLine("Hello World");
}
}
If I'm not using a namespace then I have to declare a class
class mathstuff
{
private int numberone = 2;
private int numbertwo = 3;
public int addhere()
{
return numberone + numbertwo;
}
using System;
public class myprogram
{
void main()
{
// the class here needs to be declared.
mathstuff mymath = new mathstuff();
Console.WriteLine(mymath.addhere());
}
}
Am I understanding this correctly?
A namespace is simply a way to make clear in which context the class is living in. Think of your own name, Ralph. We have many Ralphs in this world, but one of that is you. An extra way to get rid of the ambiguity is to add your surname. So that if we have 2 Ralphs, we have a bigger chance of talking about you.
The same works for classes. If you define class AClass and you would have the need of define another class AClass there would be no way to distinguish between the two. A namespace would be that 'surname'. A way of having to classes, but still able to distinguish between the two different classes, with the same name.
To answer your question, it has nothing to do with "not having to declare". It would only be easier to write code.
For example:
using System;
public class myprogram
{
void main()
{
// the class here needs to be declared.
Console.WriteLine("blah");
}
}
Because of the using System; you don't have to declare the namespace of Console. There is only one Console available, which lives in the System namespace. If you wouldn't declare your using System; namespace then you'd need to explain where Console can be found. Like this.
System.Console.WriteLine("blah");
From MSDN:
The namespace keyword is used to declare a scope. This namespace scope lets you organize code and gives you a way to create globally unique types.
For more info check MSDN for namespace.
I think what you mean is "can you declare a class without a namespace?". Yes you can, it's referred to as the global namespace.
class BaseClass
{
}
class SubClass : global::BaseClass
{
}
However, this is very bad practice, and you should never do this in a production application.
I am a VB.NET programmer by nature and I am having a hard time figuring this out. Any help with the following would be appreciated.
I need to get the C# code (1) below to work. The VB.NET equivalent works just fine, but the C# does not.
Note that both (2) and (3) do work, but this is actually auto-generated code, and I need the VB.NET and C# versions to be as similar as possible.
This does not compile (the fully-qualified name of Engine is ThreeD.QVB.Engine):
using ThreeD.QVB;
namespace QVBScript
{
public class ScriptCode
{
public void Main(ref Engine.QVBObjectsDictionary objects,
Engine.Commands commands)
{
…
However, this does work:
//using ThreeD.QVB; // I'm instead using fully-qualified names in the method
namespace QVBScript
{
public class ScriptCode
{
public void Main(ref ThreeD.QVB.Engine.QVBObjectsDictionary objects,
ThreeD.QVB.Engine.Commands commands)
{
…
This works, too:
using eng = ThreeD.QVB.Engine;
namespace QVBScript
{
public class ScriptCode
{
public void Main(ref eng.QVBObjectsDictionary objects,
eng.Commands commands)
{
…
In VB.NET if you have an import for the first part of a namespace, you can reference just the later half. In C# you cannot do this. You must have a using for the full namespace, or fully qualify your type names. Different languages, different rules.
In your last example you do not need to use the alias.
using ThreeD.QVB.Engine;
namespace QVBScript
{
public class ScriptCode
{
public void Main(ref QVBObjectsDictionary objects, Commands commands)
{
UI.Output Output = (UI.Output)objects["Output"];
Basic rules to remember:
using A.B;
does allow you to refer to types from namespaces A and A.B without fully qualifying them with their namespace (everywhere in the same file).
does not allow you to abbreviate the names of sub-namespaces of A or A.B. by omitting the A. or A.B. part from their names.
namespace A.B { … }
does allow you to refer to types from namespaces A and A.B without fully qualifying them with their namespace (inside the block).
does allow you to abbreviate the names of sub-namespaces of A or A.B by omitting the A. or A.B. part from their names.
Example:
using System.Collections;
namespace A
{
class Top : IDisposable, // importing System.Collections also imports System
IEnumerable, // inside the imported namespace
System.Collections.Generic.IEnumerable<int>
{…} // ^ "using" does not permit namespace abbreviation
}
namespace A.B
{
class Middle : Top, // namespace A available inside namespace A.B
C.IBottom // namespace blocks permit namespace abbreviation
{…}
}
namespace A.B.C
{
interface IBottom {…}
}
In C#, can you make a class visible only within its own namespace without living in a different assembly? This seems useful for typical helper classes that shouldn't be used elsewhere.
(i.e. what Java calls package-private classes)
You can make the classes internal but this only prevents anyone outside of the assembly from using the class. But you still have to make a separate assembly for each namespace that you want to do this with. I'm assuming that is why you wouldn't want to do it.
Getting the C# Compiler to Enforce Namespace Visibility
There is an article here (Namespace visibility in C#) that shows a method of using partial classes as a form of "fake namespace" that you might find helpful.
The author points out that this doesn't work perfectly and he discusses the shortcomings. The main problem is that C# designers designed C# not to work this way. This deviates heavily from expected coding practices in C#/.NET, which is one of the .NET Frameworks greatest advantages.
It's a neat trick… now don't do it.
I don't think that what you want is possible.
internal is assembly (strictly speaking module) privacy. It has no effect on namespace visibility.
The only way to achieve privacy of a class from other classes within the same assembly is for a class to be an inner class.
At this point if the class is private it is invisible to anything not in that class or the outer class itself.
If protected it is visible to everyone that could see it when private but is also visible to sub classes of the outer class.
public class Outer
{
private class Hidden { public Hidden() {} }
protected class Shady { public Shady() {} }
public class Promiscuous { public Promiscuous() {} }
}
public class Sub : Outer
{
public Sub():base()
{
var h = new Hidden(); // illegal, will not compile
var s = new Shady(); // legal
var p = new Promiscuous(); // legal
}
}
public class Outsider
{
public Outsider()
{
var h = new Outer.Hidden(); // illegal, will not compile
var s = new Outer.Shady() // illegal, will not compile
var p = new Outer.Promiscuous(); // legal
}
}
In essence the only way to achieve what you desire is to use the outer class as a form of namespace and restrict within that class.
No, it is possible. You can use internal class in another assembly.
For example I have a internal string extension class that located in SharMillSoft.Core assembly, if I want use it in another assembly that name is SharpMilSoft.Extension, I must use assembly attribute like as below:
using System;
using System.Linq;
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("SharpMilSoft.Extensions")]
namespace SharpMilSoft.Core.Extensions.Strings.Public
{
internal static class SharpStringExtensions
{
public static bool IsNullOrEmpty(this string data)
{
return string.IsNullOrEmpty(data);
}
}
}
And I use this class in SharpMilSoft.Extension assembly like as below:
namespace SharpMilSoft.Extensions.Strings
{
public static class SharpStringExtensions
{
public static bool IsNullOrEmpty(this string data)
{
return Core.Extensions.Strings.Public.SharpStringExtensions.IsNullOrEmpty(data);
}
}
}
Note: Then SharpMilSoft.Extensions assembly will be friend assembly for SharpMilSoft.Core assembly
For more details about friend assembly, you can visit this link : Friend assemblies
If you have a single assembly you can define as many namespaces in that assembly as you want but no matter what modifier you apply in the IDE you will always be able to see the classes in other namespaces.
Not sure if it is directly possible, but a few good ways to fake it would be:
1) Have the classes that need this sort of stuff inherit from a single class which has the helper class as an internal class.
2) Use extension methods and then only reference the extension methods within the namespace.