C# unit (file-wide) namespace - c#

I read C# spec and googled for it, but found nothing.
I am 99% sure there is no such feature like unit namespace directive in C#, but the question is: why? Are there idiomatic or technical reasons?
It is convenient, especially when most of our file consist of single namespace.
Is there any feature requests or proposals out there? Maybe we can make one?
// with unit namespace
namespace Foo;
class Bar { ... } // Class Bar declared inside Foo Namespace
struct Baz { ... } // Baz is inside Foo too
// without
namespace Foo {
class Bar { ... }
class Baz { ... }
}
Or maybe there is a way to re-export global symbols?
I mean first you declare everything inside global namespace, and then
publish public symbols in selected namespace?
The deep nesting of C# code is one of the most annoying thing for me.
I really enjoyed C++ ability to forward declare even nested classes and then define them without even 1 extra level of nesting.
Thanks for your time.

Logically, namespaces are block, just like classes, except that they can only contain types, not members.
Having a special syntax for this kind of block would be pointless and confusing.

Related

Namespace and class with the same name?

I'm organizing a library project and I have a central manager class named Scenegraph and a whole bunch of other classes that live in the Scenegraph namespace.
What I'd really like is for the scenegraph to be MyLib.Scenegraph and the other classes to be MyLib.Scenegraph.*, but it seems the only way to do that would be to make all the other classes inner classes of Scenegraph in the Scenegraph.cs file and that's just too unwieldy.
Instead, I've organized it as Mylib.Scenegraph.Scenegraph and MyLib.Scenegraph.*, which sort of works but I find Visual Studio gets confused under some conditions as to whether I am referring to the class or the namespace.
Is there a good way to organize this package so it's convenient for users without glomming all my code together in an unmaintainable mess?
I don't recommend you to name a class like its namespace, see this article.
The Framework Design Guidelines say in section 3.4 “do not use the
same name for a namespace and a type in that namespace”. That is:
namespace MyContainers.List
{
public class List { … }
}
Why is this badness? Oh, let me count the ways.
You can get yourself into situations where you think you are referring
to one thing but in fact are referring to something else. Suppose you
end up in this unfortunate situation: you are writing Blah.DLL and
importing Foo.DLL and Bar.DLL, which, unfortunately, both have a type
called Foo:
// Foo.DLL:
namespace Foo { public class Foo { } }
// Bar.DLL:
namespace Bar { public class Foo { } }
// Blah.DLL:
namespace Blah
{
using Foo;
using Bar;
class C { Foo foo; }
}
The compiler gives an error. “Foo” is ambiguous between Foo.Foo and
Bar.Foo. Bummer. I guess I’ll fix that by fully qualifying the name:
class C { Foo.Foo foo; }
This now gives the ambiguity error “Foo in
Foo.Foo is ambiguous between Foo.Foo and Bar.Foo”. We still don’t know
what the first Foo refers to, and until we can figure that out, we
don’t even bother to try to figure out what the second one refers to.
Giving the same name to the namespace and the class can confuse the compiler as others have said.
How to name it then?
If the namespace has multiple classes then find a name that defines all those classes.
If the namespace has just one class (and hence the temptation to give it the same name) name the namespace ClassNameNS. This is how Microsoft names their namespaces at least.
Even though I agree with other answers in that you should not name your class the same as your namespace there are times in which you cannot comply with such requirements.
In my case for example I was not the person making such a decision therefore I needed to find a way to make it work.
So for those who cannot change namespace name nor class name here is a way in which you can make your code work.
// Foo.DLL:
namespace Foo { public class Foo { } }
// Bar.DLL:
namespace Bar { public class Foo { } }
// Blah.DLL:
namespace Blah
{
using FooNSAlias = Foo;//alias
using BarNSAlias = Bar;//alias
class C { FooNSAlias.Foo foo; }//use alias to fully qualify class name
}
Basically I created namespace "aliases" and that allowed me to fully qualify the class and the Visual Studio "confusion" went away.
NOTE:
You should avoid this naming conflict if it is under your control to do so.
You should only use the mentioned technique when you are not in control of the classes and namespaces in question.
I would suggest that you follow the advice I got on microsoft.public.dotnet.languages.csharp to use MyLib.ScenegraphUtil.Scenegraph and MyLib.ScenegraphUtil.*.
As others have said, it's a good practice to avoid naming a class the same as its namespace.
Here are some additional naming suggestions from an answer by svick to a related question "Same class and namespace name" on the Software Engineering Stack Exchange:
You're right that you shouldn't name the namespace the same as a type
it contains. I think there are several approaches you can use:
Pluralize: Model.DataSources.DataSource
This works especially well if the primary purpose of the namespace is
to contain types that inherit from the same base type or implement the
same interface.
Shorten: Model.QueryStorage
If a namespace contains only a small number of types, maybe you don't
need that namespace at all.
Make enterprisey: Model.ProjectSystem.Project
This can work especially for features that are important part of your
product, so they deserve their own name.
It happens when it's the main class of the namespace. So it's one motivation to put the namespace in a library, then the issue goes away if you add 'Lib' to the namespace name...
namespace SocketLib
{
class Socket
{
CA1724: Type Names Should Not Match Namespaces ...
Basically, if you follow Code Analysis for proper coding this rule says to not do what you are trying to do. Code Analysis is very useful in helping you find potential issues.
Old post, but here I go with another idea that may help someone:
"...but it seems the only way to do that would be to make all the other classes inner classes of Scenegraph in the Scenegraph.cs file and that's just too unwieldy."
This is really the better implementation for a bunch of scenarios. But, I do agree that having all that code on the same .cs file is annoying (to say the least).
You could solve it by making the base class a "partial class" and then, go on creating the inner classes on their own files (just remember that they'll have to declare the base class complement and then go on with the specific inner class for that file).
Something like...
Scenegraph.cs:
namespace MyLib
{
public partial class Scenegraph
{
//Scenegraph specific implementations
}
}
DependentClass.cs:
namespace MyLib
{
public partial class Scenegraph
{
public class DependentClass
{
//DependentClass specific implementations
}
}
}
I do think that this is the closer that you can get to having the clean implementation of inner classes while not having to clutter everything inside one huge and messy file.
Just Adding my 2 cents:
I had the following class:
namespace Foo {
public struct Bar {
}
public class Foo {
//no method or member named "Bar"
}
}
The client was written like this:
using Foo;
public class Blah {
public void GetFoo( out Foo.Bar[] barArray ) {
}
}
Forgiving the error GetFoo not returning the output instead of using the out parameter, the compiler could not resolve the data type Foo.Bar[] . It was returning the error: could not find type or namespace Foo.Bar .
It appears that when it tries to compile it resolved Foo as the class and did not find an embedded class Bar in the class Foo. It also could not find a namespace called Foo.Bar . It failed to look for a class Bar in the namespace Foo. The dots in a name space are NOT syntactic. The whole string is a token, not the words delimited by the dots.
This behaviour was exhibited by VS 2015 running .Net 4.6

using MyProj; at the user class definition or MyProj.MyClass at the specific method(s)?

Ok, let me explain:
Whenever you start a class in .Net and let's say you have another project you've developed referenced which has non static class(es) you need to use...
You have 2 options (That I know of)
Place a using at the top of your class meaning you won't need to explicitly name the whole project each time you need it's classes
using FooBarProj;
public FooBar MyMethod()
{
FooBar fb = new FooBar();
//Do stuff
return fb;
}
Or do explicitly implement those:
public FooBarProj.FooBar MyMethod()
{
FooBarProj.FooBar fb = new FooBarProj.FooBar();
//Do stuff
return fb;
}
Does this make an effective difference at all? either efficiency at execution, compiling, whatever? or its simply a dev convenience issue?
It makes no difference after compiling the code.
Either using an using statement, or using the whole class namespace and name, is the same thing after all.
When compiling to CIL code, references to types are a single thing, each reference contains all the information, regardless of how you coded that reference.
Referencing a class with a different name
You can even rename a class if you whant, doing this:
using TheString = System.String;
Now you can refer to System.String using the name TheString in your code.
All of these produces the same compiled code
string
System.String
String (if you place using System; at the beginning)
TheString (if you place using TheString = System.String; at the beginning)
Conclusion: All using statements are just convenience.
I think it is a dev convenience issue. The first version is better in all circumstances other than there is a similar class name in different namespaces. The explicit namespace declaration before the class is needed when there are same class names in different namespaces like. Dog23.class1 and dog45.class1. Always the first version if it works. It is shorter and readable.

Can you create private classes in C#?

This is a question for the .NET philosophers:
It is my understanding that Microsoft consciously denied use of private classes in C#. Why did they do this and what are their arguments for doing so?
I, for example, am building a large application that includes a reporting tool. This tool uses a lot of business objects that are used only within the reporting tool and not in other parts of the project. I want to encapsulate them for use only within the reporting tool itself.
Great decision is creating separate project in VS for this tool, and I'll do like that, but I'm interesting, what if I can't do this - for exmple our architecture wasn`t good enough, and we have big single project.
Behind "private class" I mean a class that can't be used in any other namespace, except its own.
My question was not - how can I simulate this, or do in another way. I'm just wondering, why not use private keyword with class keyword without any parent classes. I`m thinking there should be some reason, and I want to know it
Allowing classes to be private to a namespace would achieve no meaningful level of protection.
Any assembly in the world could simply reference your dll, and start writing code in your namespace which accesses your supposedly private classes.
I think that's possibly the answer you'd get from Microsoft.
There's a workaround for this, but you might not like it.
Instead of using a namespace to scope your classes, use a public static partial class:
Before:
namespace MyCompany.Foo {
class Bar { }
public class Baz { }
}
After:
namespace MyCompany {
public static partial class Foo {
private class Bar { }
public class Baz { }
}
}
This construct, like a namespace, can span multiple files in the same project. But unlike a namespace, it cannot "escape" from your project (other projects cannot define other members inside Foo).
There's an added advantage that you can have utility methods that seem to have no class for code inside Foo.
The disadvantage is that, to use your non-private classes outside of your fake namespace, you have to reference them inside Foo:
using MyCompany;
// ...
var baz = new Foo.Baz();
This can be mitigated by using an alias for the class:
using Baz = MyCompany.Foo.Baz;
// ...
var baz = new Baz();
But you'd have to do it for each non-private class that you want to use.
UPDATE
It's interesting to note that C# 6 will have static using statements, which could effectively improve this proposal to use a public static partial class as a "module". You would just "use" the "module" to access its types directly.
Hopefully, it will work like this:
using MyCompany.Foo;
// ...
var baz = new Baz();
Just as if Foo was a namespace.
You can create a private class, as a member of another type:
public class Outer {
// ...
private class Inner {
// ...
}
}
and Inner is only visible to members of Outer.
At the outermost level (i.e. in a namespace) private as per its definition would not make sense (since there is nothing to be private in). Instead use internal (visible to the containing assembly's members only).
You can define a private class, but it can only be used by its containing class.
If you want a class that is only visible within a particular assembly (DLL/EXE/etc.), then you should declare it as internal (Friend in VB)
True but you can get a pretty close simulation of this with internal classes and the internalsvisibletoAttribute if the namespace is split across multiple assemblies.
Also remember that a class within another can be private to the outer class. The outer class can be considered a namespace for this purpose.
So I guess you want to do this
namespace Baz
{
private class foo
{
private int _bar;
}
}
If yes. Then what is the purpose foo will server. At namespace can you be more restrictive than internal , and make any use of the class.If I could do this where will I use this .
That is why you have this compile time validation.
Now Inside a Public Class it makes sense to have a private class. I cannot explain it better this Private inner classes in C# - why aren't they used more often?.

c# using statements placement [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Should Usings be inside or outside the namespace
I am looking at a code base where the author (one I respect) consistently places using statements inside of the namespace, as opposed to above it. Is there some advantage (more efficient GC?) to doing so or is this just a code style preference?
Cheers,
Berryl
Never put them inside without using "global::" or your code will become brittle.
namspace bar {
using foo //this may mean "using global::bar.foo OR using global::foo"
}
Reference
http://blogs.msdn.com/b/ericlippert/archive/2007/06/25/inside-or-outside.aspx?wa=wsignin1.0
If you have multiple namespaces in the same file then you will be scoping usings only to the containing namespace instead of to all namespace in the entire file.
Also see (just found this good explanation) Should 'using' statements be inside or outside the namespace?
Scott Hanselman did a post about this back in July 2008. I don't know if this changed with the .NET 4 framework, but it basically came down to being a preference issue unless you're naming your classes the same as existing classes as well as multiple namespaces in a single file.
It's a preference thing but there is a semantic difference when you use the statement on the inside versus on the outside in some scenarios.
using Bar;
namespace Foo
{
using Bar;
namespace Bar
{
class C
{
}
}
namespace Baz
{
class D
{
C c = new C();
}
}
}
namespace Bar
{
class E
{
}
}
In this, the outer using statement refers to the namespace Bar that is after namespace Foo. The inner using statement refers to Bar that is inside Foo. If there were no Bar inside Foo, then the inner would also refer to the outer Bar.
Edit And as Jonathan points out, the inner using can be changed to `using global::Bar;" to refer to the out Bar namespace, which would happen to break this particular code because of D trying to use C.
It's MS recommend practice. Programs such as stylecop recommend it.
Check out Is sa1200 All using directives must be placed inside the namespace (StyleCop) purely cosmetic? for a more in-depth discussion

'CompanyName.Foo' is a 'namespace' but is used like a 'type'

Restatement of the question
I'm resurrecting this question because I just ran into this error again today, and I'm still utterly confused why the C# compiler bothers to check for collisions between namespaces and types in contexts where it makes no sense for a namespace to exist.
If I have...
public Foo MyFoo { get; set; }
...why would the compiler care that Foo is both a namespace and a type? Can you declare a property as a namespace instead of a type?
What is the logic behind the "namespace used like type" compiler error? What problem is this saving me from?
[And how do I tag Eric Lippert? :)]
Original Question
The problem
I have a project "Foo" with default namespace CompanyName.Foo. I have a database that's also called "Foo".
And when I run SqlMetal.exe on the database, it generates a class CompanyName.Foo.Models.Foo.
Then, when I attempt to create a property with this class as the type, like this...
using CompanyName.Foo.Models;
...
public Foo DataContext { get; set; }
...I get the error:
'CompanyName.Foo' is a 'namespace' but is used like a 'type'.
I am forced to do...
public CompanyName.Foo.Models.Foo Foo { get; set; } // :-(
Questions:
Why does this error occur? My property declaration doesn't contain CompanyName, so why is this a problem? Simply put: Foo != CompanyName.Foo. Also, just to be sure, I did a search of my entire solution for namespace Foo and came up with zero hits (if I had actually used a namespace Foo, I could understand getting an error).
[answered] Is there any way around fully qualifying Foo every time I want to use it?
[answered] Is there any way to get SqlMetal to name the class anything other than Foo (w/o changing the name of my database)? I can change the namespace using a switch, but I don't know of a way to change the actual class name.
Update
Still seeking an answer to (1).
O.K.W. nailed (2) & (3).
Usings
A request was made for all my using statements:
using System;
using System.ComponentModel;
using System.Data.Linq;
using System.Linq;
using MyCompany.Foo.Models;
how do I tag Eric Lippert?
If you have something you want brought to my attention you can use the "contact" link on my blog.
I'm still utterly confused why the C# compiler bothers to check for collisions between namespaces and types in contexts where it makes no sense for a namespace to exist.
Indeed, the rules here are tricky. Coincidentally, two weeks ago I wrote and posted a series of blog articles about some of these issues closely related to this very issue; they'll actually go live in early March. Watch the blog for details.
UPDATE: The articles mentioned above are here:
Link
Why does this error occur?
Let me rephrase the question into several questions.
What sections of the specification justify the production of this error?
I think that's already been covered satisfactorily in other answers. The type resolution algorithm is extremely well-specified. But just to sum up: being inside something of the right name "binds more tightly" than using something of the right name from the outside. When you say:
using XYZ;
namespace ABC.DEF
{
class GHI : DEF { }
}
that is the same as
using XYZ;
namespace ABC
{
namespace DEF
{
class GHI : DEF { }
}
}
So now we must determine the meaning of DEF. We go from inside to outside. Is there a type parameter of GHI called DEF? No. Look at the container. Is there a member of DEF called DEF? No. Look at the container. Is there a member of ABC called DEF? YES. We're done; we have determined the meaning of DEF, it is a namespace. We discover the meaning of DEF before we ask "does XYZ have a member DEF?"
What design principles influence this design?
One design principle is "names mean the same thing no matter how you use them". The language does not 100% obey this principle; there are situations in which the same name can be used to refer to two different things in the same code. But in general, we strive for a situation where when you see "Foo" two times in the same context, it means the same thing. (See my article on The Color Color Problem for some details on this, as well as my articles on identifying violations of the "simple name" rules.)
One design principle is "no backtracking". We do not ever say in C# "I see that you used a name to refer to something that is not legal to refer to in this context. Let me abandon the result of name binding and start over, looking for something that might work."
A larger principle that underlies the "no backtracking" principle is that C# is not a "guess what the user meant" language. You wrote a program where the best possible binding of an identifier identified a namespace when a type was expected. There are two possibilities. Possibility one: you've made an error that you want to be told about so that you can take action to correct it. Possibility two: you meant for a less-good binding to be the one we choose, and so we should guess from amongst all the possible less-good bindings to figure out which one you probably meant.
That's a good design principle in languages like JScript -- JScript is all about muddling on through when the developer does something crazy. C# is not that kind of language; the feedback we get loud and clear from our developers is tell me when something is broken so I can fix it.
The thing about "no backtracking" is that it makes the language much easier to understand. Suppose you have something like this mess:
namespace XYZ.DEF
{
public class GHI {}
}
namespace QRS.DEF.GHI
{
public class JKL { }
}
...
using QRS;
namespace TUV
{
using XYZ;
namespace ABC
{
namespace DEF
{
class GHI { }
class MNO : DEF.GHI.JKL { }
}
}
}
Work out the base type of MNO. With no backtracking we say "DEF is ABC.DEF". Therefore GHI is ABC.DEF.GHI. Therefore JKL is ABC.DEF.GHI.JKL, which does not exist, error. You must fix the error by giving a type name that lets the compiler identify which DEF you meant.
If we had backtracking, what would we have to do? We get that error, and then we backtrack. Does XYZ contain a DEF? Yes. Does it contain a GHI? Yes. Does it contain a JKL? No. Backtrack again. Does QRS contain an DEF.GHI.JKL? Yes.
That works, but can we logically conclude from the fact that it works that it is the one the user meant?
Who the heck knows in this crazy siutation? We got all kinds of good bindings in there that then went bad very late in the game. The idea that we stumbled upon the desired answer after going down many blind alleys seems highly suspect.
The correct thing to do here is not to backtrack multiple times and try out all kinds of worse bindings for every stage of the lookup. The correct thing to do is to say "buddy, the best possible match for this lookup gives nonsensical results; give me something less ambiguous to work with here please."
An unfortunate fact about writing a language where the compiler by design complains loudly if the best match is something that doesn't work, is that developers frequently say "well, sure, in general I want the compiler to point out all my mistakes -- or, rather, all my coworker's mistakes. But for this specific case, I know what I am doing, so please, compiler, do what I mean, not what I say."
Trouble is, you can't have it both ways. You can't have both a compiler that both enforces rigid rules that make it highly likely that suspicious code will be aggressively identified as erroneous and allow crazy code via compiler heuristics that figure out "what I really meant" when you write something that the compiler quite rightly sees as ambiguous or wrong.
For an object lesson in how lots of pro devs vehemently dislike the effects of a language design that aggressively identifies errors rather than guessing that the developer meant for the worse result to be chosen, see the 116 comments to this article on a minor and rather unimportant aspect of overload resolution:
(Note that I am no longer responding to comments on this issue; I've explained my position over ten times. If all those explanations are not convincing, that's because I'm not a very good convincer.)
And finally, if you really want to test your understanding of how the name resolution rules work in C#, try out this little puzzle. Almost everyone gets it wrong, or gets it right for the wrong reasons. The answer is here.
The clash is between namespace CompanyName.Foo and CompanyName.Foo.Models.Foo, and not Foo. I'm not exactly sure how/why the compiler can't distinguish both though.
You can try using namespace alias to shorten full qualifying Foo
e.g. using coyModels = CompanyName.Foo.Models
From the reference, seems like you can use /context:<type> and /namespace:<name> to specify the data context class (instead of using table name) and namespace.
C# compiler doesn't compile when there is an ambiguity between a class and a namespace with the same name. Unfortunately you just have to namespace the class explicitly or rename the database. In your case the compiler didn't even get to the conflict, it died after resolving Foo as a namespace.
Whenever you have something like this:
using CompanyName.Foo.Models;
namespace CompanyName.Foo {
class Test {
public Foo Model { get; set; } // error CS0118: 'CompanyName.Foo' is a 'namespace' but is used like a 'type'
public Foo1 Model { get; set; } //OK
}
}
namespace CompanyName.Foo.Models {
class Foo1 {
}
class Foo {
}
}
What actually happens is every preceeding level of the namespace is implicitly imported at each level. This makes sense since the nested namespace syntax using dot is the same as nesting namespaces:
namespace CompanyName {
using CompanyName; //<--using1 - Implicit using, since we should be able to access anything within CompanyName implicitly.
namespace Foo {
using CompanyName.Foo; //<-- using2 Same as above
class Test {
public Foo Model { get; set; } //At this stage due to using1 Foo is actually CompanyName.Foo, hence the compiler error
}
}
}
So inside class Test there are two implicit usings:
using CompanyName;
using CompanyName.Foo;
Hence Foo is resolved to the namespace hence the error.
EDIT Good point. I've dug this up from MSDN:
The meaning of a namespace-or-type-name is determined as follows:
If the namespace-or-type-name consists of a single identifier:
If the namespace-or-type-name appears within
the body of a class or struct
declaration, then starting with that
class or struct declaration and
continuing with each enclosing class
or struct declaration (if any), if a
member with the given name exists, is
accessible, and denotes a type, then
the namespace-or-type-name refers to
that member. Note that non-type
members (constants, fields, methods,
properties, indexers, operators,
instance constructors, destructors,
and static constructors) are ignored
when determining the meaning of a
namespace-or-type-name.
Otherwise, starting with the namespace in which the
namespace-or-type-name occurs,
continuing with each enclosing
namespace (if any), and ending with
the global namespace, the following
steps are evaluated until an entity is
located:
If the namespace contains a namespace member with the given
name, then the namespace-or-type-name
refers to that member and, depending
on the member, is classified as a
namespace or a type.
Otherwise, if the namespace has a corresponding
namespace declaration enclosing the
location where the
namespace-or-type-name occurs, then:
If the namespace declaration contains a
using-alias-directive that associates
the given name with an imported
namespace or type, then the
namespace-or-type-name refers to that
namespace or type.
Otherwise, if the namespaces imported by the
using-namespace-directives of the
namespace declaration contain exactly
one type with the given name, then the
namespace-or-type-name refers to that
type.
...
(Bolding is mine) This means that when resolving Foo, matching it against CompanyName.Foo (first bold bit) happens before matching it against the using directive(second bold build).
why can't you just do
using CompanyName.Foo;
...
public Models.Foo DataContext { get; set; }
I had this issue pop up when I was referencing a class in a separate class library, where its type had the same name as the root of the namespace. Initially, when referencing this type in a separate console app project, there was no problem, everything compiled fine. However the reference from a Windows Service project was generating the is a 'namespace' but is used like a 'type'. message. Turns out the Windows Service Project had its Target Framework set to ".NET Framework 4 Client Profile". Changing this to ".NET Framework 4" eliminated the error. Hopefully this helps someone in a similar situation.
I am new to c# and I came into contact with this error upon decompiling a c# application, saving as a project, the attempting to immediately recompile... why the application was able to compile in the first place is beyond me.. however... the problem and solution is quite simple: by default, upon adding a new class, c# uses the same name for a namespace as it does for the class within the namespace!!!!! This is bad because without some hidden identifier explicitly telling which (namespace or type) you are referring to, the compiler can't tell the difference!!!!! doh! way to go c#!!!! ... THE SOLUTION: Instead of renaming a thousand things and double checking all corrections, run the project, when you have the list of errors in front of you, click each in turn to go to each problem. Once at the "foo" in question type a dot (.) after said "foo" such that it displays: foo. .. this should bring up the menu of classes contained within. In this list, double-click "foo" (or just retype the name) changing the original "foo" to "foo.foo" ... Do this for each error and problem solved!!! Voila!!!! I did this to an entire application with complex names, and it worked great! Happy coding! - Tim H.
Because you've used dot notation to separate Company and Foo, you are implicitly creating a Company namespace, with a nested Foo namespace, not Company.Foo as you believe.
That's why this doesn't work:
namespace Company.Foo
{
}
namespace Company.Foo.Models
{
public class TestClass {
public Foo DataContext { get; set; }
}
}
The closest thing to Foo is the nested Foo namespace in the Company namespace. You can however do this:
using Company.Foo;
using Company.Foo.Models;
namespace Company.Foo
{
class Program {
public static void Main() {}
}
}
namespace Company.Foo.Models
{
public class Foo { }
}
public class DataContextClass
{
public Foo DataContext { get; set; } /* Foo here is automatically resolved to Company.Foo.Models.Foo */
}
Edit
Igor said the same thing, but was more technical.
This also happens if you generate unit tests when you have a namespace and a class with the same name. Which you should never do as explained by Eric Lippert here:
http://blogs.msdn.com/b/ericlippert/archive/2010/03/09/do-not-name-a-class-the-same-as-its-namespace-part-one.aspx

Categories