5

I come from Java and see that package in Java is very convenient. When you move a class to another package, it will change automatically the package. (of course, by IDE such as Eclipse or Netbean)

But C# is using namespace and don't have my namespace renamed automatically like it does in Java. For example I have a file which namespace is com.app and I put it in com.app, but at later time, I move this file to com.lib folder and its namespace still be com.app. So, I find this is difficult to manage because I'm moving it manually.

Please give me help in how to fix my problem. (that namespace of file is named by folder it contains, and when I move to other, I will automatically change). Can we do it?

jamesmortensen
  • 33,636
  • 11
  • 99
  • 120
hqt
  • 29,632
  • 51
  • 171
  • 250
  • 1
    Often in `stackoverflow` community, there are many down votes with no reason !!! I cannot imagine they can silent down vote and tell nothing :)) maybe they are intelligent, and they think other questions is silly, but they still need tell something ;) – hqt May 18 '12 at 04:15
  • 3
    Not really. Voting is completely anonymous here. With that said, I think feedback is important. The most likely reason for the downvote is that you weren't clear in what IDE you're using. Your question also comes off like a rant instead of an objective question. If you edit out the rant (i.e saying you think C# is inconvenient when it's clear you're just learning it and don't know what you're talking about) then you can possibly avoid the downvotes. In other words, keep your opinions and biases out of the question. Good luck! – jamesmortensen May 18 '12 at 04:17
  • 1
    Some people may also not like the java/package tags when it's a question about c#, but my main guess would be the ranting. – TheEvilPenguin May 18 '12 at 04:20
  • @jmort253 Sorry if my english bad. but because language decides how IDE works. because package is physical (mean it will create folder in project) and namespace is virtual (it doesn't make anything real in your computer) so IDE will works like they decide. And my question mean: Can we have some trick to make VS Studio work like I say !!! – hqt May 18 '12 at 04:21
  • I think addons like Chris Shain suggested are the best idea. The lack of restriction about which namespace you place a class in is by design to support language features, so it's unlikely you'll find another solution. – TheEvilPenguin May 18 '12 at 04:27
  • 3
    @hqt - I edited your question so that it is less of a rant and +1'd it. I think it's a good question. If you think the meaning of your question was lost, please feel free to rollback. – jamesmortensen May 18 '12 at 04:32
  • I am with you @hqt, many of my question were taken down too, and they posts its not question at all... these days stackoverflow become places of master who consider basic questions as silly... this is so bad – srisar Jan 16 '13 at 07:34

2 Answers2

7

I fix the problem by using an IDE plugin called Resharper. (Among many, many useful features) it highlights when a namespace is wrong (based on the folder hierarchy and root namespace of the assembly) and can fix it for you.

Note that unlike in Java, there are sometimes very valid reasons for a class to be in a namespace other than the one inferred by the directory structure. A good example might be extension method classes, which need to be in scope in the class that is invoking them. Therefore it is common to have:

/myProject
  /extensions
     /MyExtensionMethodClass.cs

with a namespace like myProject (so that the extension methods can be used anywhere in myProject without a using directive)

Chris Shain
  • 50,833
  • 6
  • 93
  • 125
  • Visual Studio 2010 has many refactoring features previously only available through Resharper. – Jason Sturges May 18 '12 at 04:22
  • 1
    @JasonSturges I wouldn't know- it is so helpful that I've stopped coding without it. – Chris Shain May 18 '12 at 04:26
  • Oh. It means Resharper will make namespace like the folder it contains ? – hqt May 18 '12 at 04:26
  • It marks the namespace declaration int the file if it doesn't match the folder hierarchy and you can fix it for you (there is normally a keyboard shortcut to invoke Resharpers actions). As Chris Shain said there are cases where you definitely do not want to have namespaces matching folders (if you use folders to group files even though they belong to one namespace) – Sascha May 18 '12 at 04:40
  • I don't understand why you would do that? If you put the Extension class in it's own folder, but the same namespace, why not just put the file in the same folder too? If you wanted to share this extension across many namespace, you'd have to put a using statement anyways. So I don't see why this is a very valid reason unlike in java. – Didier A. Jun 11 '14 at 20:26
2

Thats actually because C# has the concept of partial classes , that is , you can distribute your C# class along several files instead of just having it coded into a single file , like Java. For that reason , namespaces in .Net are distributed containers instead of centralized containers , defined by your namespace orperator.

  • 2
    It's a nice and correct explanation, but it doesn't answer the question. Next time you should think about writing something like this as a comment. – Skalli May 18 '12 at 07:29
  • Thanks for clarifying that. I am still new at this site , so I guess I still have to get to know how things work properly in here :D . – Leandro Pezzente May 18 '12 at 10:36
  • 1
    You'll get the hang to it soon. :D It's a charming community. ;) – Skalli May 18 '12 at 11:54
  • Hum... I don't know if it's such a good idea to put all your partials in different folders, but I guess this might actually have been the reason why they did it like that. – Didier A. Jun 11 '14 at 20:28