Names in C#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Can anyone tell me, is it legal for a class to have the same name as the
namespace it is in. I did it, and the compiler didn't give an error, but it
caused some very odd name resolving problems.
 
Hi Dave,
it is perfectly legal as you said in your reply :-) What issues are you
seeing, there should not be any.

Mark.
 
Mark R. Dawson said:
it is perfectly legal as you said in your reply :-) What issues are you
seeing, there should not be any.

For example, the following will not compile as the class UseMyTest complains
that
'MyTest' denotes a 'namespace' where a 'class' was expected.

using System;
namespace MyTest
{
public class MyTest
{
public MyTest()
{
}
}
}


using System;
using MyTest;
namespace AnotherNamespace
{
public class UseMyTest
{
public UseMyTest()
{
MyTest myTest = new MyTest();
}
}
}
 
I have had similar problems. I think that part of the difficulty is
that in this case Intellisense and the compiler work at cross-purposes.
Intellisense seems to interpret "MyTest" as a class name, while the
compiler interprets it as the namespace name.

I've taken to changing the namespace wherever possible so that no
classes share names with the namespace.
 
Exactly that, but if you put
namespace AnotherNamespace
{
public class UseMyTest
{
public UseMyTest()
{
MyTest. MyTest myTest = new MyTest();
//^^^^^
}
}
}

then it's happy.
Dave
 
That's possible, but it's what the compiler does that's important, and the
copmpiler seems to be doing it wrong - unless there's a rule about
namespace/class names that I wasn't aware of.
 
Dave,
I think it is best not to have a Class with the
same name as it's Namespace, even if it is legal.

I've had problems when they were the same.
At first, I just prefixed the class, ie: cMyClass.
Now, there is no need for the prefix, ie:

namespace MyPrj.BusinessObjects
{
public class ABusObj {
}
}
Roger
 
Phooey. This isn't a matter of programming style, this is part of the
language definition. Either it's allowed or it isn't. Mark says it is (in
whaich case it's a compiler bug) and I guess everybody else are "don't
know"s. Anyone from uSoft out there?
 
Dave said:
Can anyone tell me, is it legal for a class to have the same name as the
namespace it is in. I did it, and the compiler didn't give an error, but it
caused some very odd name resolving problems.

why would you want to do such a thing in the first place?
 
Every Christmas I dress up in women's clothing. You might not think there is
any reason for doing that, but actually I have a perfectly valid reason. The
important question is whether or not it is legal (and it is!)
 
Peter said:
For example, the following will not compile as the class UseMyTest complains
that
'MyTest' denotes a 'namespace' where a 'class' was expected.

using System;
namespace MyTest
{
public class MyTest
{
public MyTest()
{
}
}
}

It compiles just fine for me . . . for whatever that may mean. I am using .Net
Framework 1.1, Version 1.4322.

-rick-
 
Back
Top