I need help understanding how C# projects are orga

2019-04-11 05:51发布

I've been teaching myself C# for the past couple weeks, and as someone whose IDE of choice is Notepad, I'm having a little bit of difficulty transitioning to Visual Studio (I'm using 2010 express). In particular, I'm wondering how the organization of the Namespace-Class-Method hierarchy manifests itself VISUALLY in the interface. I'm having a hard time making sense of it, and, more importantly, how to use the interface to effectively organize and keep track of my projects.

For instance, there's the "solution explorer", but there's no such thing as a C# "solution" (that I'm aware of). I suspect it's Microsoft's marketing speak for a more generic development term, but I can't figure it out. I get the option of creating New "projects". Is a "project" a "solution"?

I'm also a little fuzzy on namespaces. I suspect that the namespaces are the equivalent of a class library in Java. What are some examples of how namespaces are used in the real world? Say, for instance, I'm developing a personal finance application. Would I put EVERYTHING related to that application in one solution? Or would I create as namespace for, say, cash accounts and a namespace for investment accounts?

Within the namespaces are my *.cs files but I can't seem to figure out how to create a NEW *.cs file in my namespace. I would EXPECT, based on the explorer hierarchy, that any class using a namespace would appear in that list, and I would be able to use it as needed. For instance, I would be able to create enterDeposits.cs and enterWithdrawals.cs WITHOUT needing to create a new project.

I've found a couple tutorials online that tell me how to do things (like creating a new project), but without a solid understanding of the IDE's vocabulary, I'm not really sure I'm keeping everything organized as well as I could. Help!

3条回答
女痞
2楼-- · 2019-04-11 06:27

Solutions and projects in Visual Studio are ways to organize code - they are containers used by the IDE to issue commands to the compiler and other build components as needed.

Solutions contain projects - projects contain code files.

Each project will compile to a separate DLL or EXE, a unit of deployment.

Namespaces can be spread across projects and solutions and be in different DLLs/EXEs. They are units of logical separation.

In Visual Studio, you can set a base namespace for each project in its properties. By default, every directory you create will get appended to that as part of the inner namespace. Any source code file created in a directory will by default get that namespace.

In general, namespaces are a pure code construct.

In a C# file, you have a namespace declaration - this can be any valid namespace identifier and can be in any project/solution/code file.

I do suggest taking a look at MSDN - it is a good resource for anything C# and Visual Studio.

查看更多
Deceive 欺骗
3楼-- · 2019-04-11 06:38
查看更多
男人必须洒脱
4楼-- · 2019-04-11 06:42

Great overview by Oded. RE: would EXPECT, based on the explorer hierarchy, that any class using a namespace would appear in that list, and I would be able to use it as needed. you are correct, the IDE will recognize classes in the SAME namespace.

One thing that tricks up new users is that namespaces do not inherit. If you have a namespace MyProject and another namespace MyProject.SubNamespace, the compiler will not automatically link the SubNamespacetwo. You need to specify a Using statement, e.g. "Using MyProject.SubNamespace" to let the IDE know to use the classes in that namespace.

查看更多
登录 后发表回答