Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I need help understanding how C# projects are organized in Visual Studio

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!

like image 511
dwwilson66 Avatar asked Feb 18 '23 15:02

dwwilson66


1 Answers

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.

  • Solution and Project Basics
  • Creating Solutions and Projects
like image 189
Oded Avatar answered Apr 27 '23 18:04

Oded