C# for JavaScript developers - part 1

Intro to C# and .NET - what the project structure looks like, how to build and run the project etc.

Project Structure

├── src
│   └── MyProject
└── test
  • src will have all the source code for the project and test will have unit tests
  • inside src you can have multiple projects or components that make up a larger project
  • .csproj means it's a C# project. It contains info about your project
  • Program.cs contains the starting code dotnet run picks up the .csproj file in the folder by default
├── src
│   └── MyProject
│       ├── bin
│       │   └── Debug
│       │       └── netcoreapp3.0
│       │           ├── MyProject
│       │           ├── MyProject.deps.json
│       │           ├── MyProject.dll
│       │           ├── MyProject.pdb
│       │           ├── MyProject.runtimeconfig.dev.json
│       │           └── MyProject.runtimeconfig.json
│       ├── MyProject.csproj
│       ├── obj
│       │   ├── Debug
│       │   │   └── netcoreapp3.0
│       │   │       ├── MyProject
│       │   │       ├── MyProject.AssemblyInfo.cs
│       │   │       ├── MyProject.AssemblyInfoInputs.cache
│       │   │       ├── MyProject.assets.cache
│       │   │       ├── MyProject.csprojAssemblyReference.cache
│       │   │       ├── MyProject.csproj.FileListAbsolute.txt
│       │   │       ├── MyProject.dll
│       │   │       └── MyProject.pdb
│       │   ├── MyProject.csproj.nuget.cache
│       │   ├── MyProject.csproj.nuget.dgspec.json
│       │   ├── MyProject.csproj.nuget.g.props
│       │   ├── MyProject.csproj.nuget.g.targets
│       │   └── project.assets.json
│       └── Program.cs
└── test
  • bin/ is where your build output (i.e. binaries, aka assembly) would go
  • obj/ is a temp folder that is created during the restore/build process. You can delete this safely
  • You can .gitignore both bin/ and obj/ folders
bin/ # equivalent of public/
obj/ # equivalent of node_modules/

Running the project

dotnet run # from within the directory that contains .csproj
dotnet run --project src/GardeBook # give it a directory where .csproj is

dotnet run = dotnet restore -> dotnet build

Passing params to the project

dotnet run BLAH # params for the dotnet CLI
dotnet run -- BLAH # params for the application

NuGet

dotnet restore # equivalent of `npm install`
dotnet build # takes .cs files and compiles into a binary DLL

Assembly

An assembly in .NET Core is what the output of the C# compiler is, your code in binary format. It'll be in a folder called bin (short for binary)

Inside bin/ you'll have Debug, which just means this build is easier to debug

dotnet run # will restore, build, find the .dll and run it
dotnet bin/Debug/netcoreapp3.0/MyProject.dll # run the assembly manually

The entry point of an application (by convention) is Main(). dotnet run will look for a method named Main and execute the code inside it

Exceptions

  • an exception represents an error condition
  • handled exception = yes, i expected this error to occur
  • unhandled exception = halt/crash your program, .NET runtime won't allow program to continue executing

Please note that this site and the posts on it are, and will always be, a work in progress. If i waited for perfection, i’d never get anything done.