C# In Visual Studio Code

  

# How YOU can get started with .NET Core and C# in VS Code

Follow me on Twitter, happy to take your suggestions on topics or improvements /Chris

This article covers your first steps using .NET Core, the cross-platform version of .NET. We will be creating a solution, a library, a console app and also a test project. Of course, we will be taught how to execute our tests, all this from the terminal

Visual Studio Code is free and available on your favorite platform - Linux, macOS, and Windows. Download Visual Studio Code to experience a redefined code editor, optimized for building and debugging modern web and cloud applications. Visual Studio Code is a code editor redefined and optimized for building and debugging modern web and cloud applications. Visual Studio Code is free and available on your favorite platform. Since no one else said it, the short-cut to compile (build) a C# app in Visual Studio Code (VSCode) is SHIFT+CTRL+B. If you want to see the build errors (because they don't pop-up by default), the shortcut is SHIFT+CTRL+M. (I know this question was asking for more than just the build shortcut.

.NET has been around since 2001 and a lot of developers have cut their teeth on using Visual Basic .NET or C# with Visual Studio. They were using Windows at the time. Times have changed, cross-platform is a must nowadays and .NET as well as Visual Studio exists on both Windows, Linux, and Mac.

In this article we will cover:

  • Installing .NET Core and some nice extensions like C#
  • Scaffold a solution
  • Add a class library and add it to the solution
  • Add a test project and run our tests
  • Add a Console program and use our library
  • Improve our IDE with vscode-solution-explorer extension

what commands we were taught and what to look at next like ASP, Docker, Cloud

# Resources

Visual
  • Host a .NET Core appThere are many ways to host a .NET Core app, Windows, Azure etc.

  • Publish you .NET Core app to the CloudLet's learn how to publish our .NET Core app to the Cloud and Azure.

  • Sign up for a free Azure accountTo be able to use Azure you will need a free Azure account

  • Hello world tutorial - console appThis shows a simple Hello World console application

  • Containerize a .NET Core AppThis explains how to Dockerize your app, how to author the Dockerfile etc.

  • Unit testing in .NET CoreThere is more than one choice for unit testing

  • dotnet CLI - all commandsThere are so many commands for the dotnet tool. Did you know that you can scaffold a .NET project with a React frontend for example?

  • Create a new web app with ASP and .NET CoreGreat tutorial for creating your first Web app in .NET Core

## Install and Set up

The first thing you want to do is to install .NET Core. Head over to this page:

Once it's installed you should have access to a command-line tool you can invoke in the terminal called dotnet. You can easily see what commands are available by typing:

Quite a few commands as you can see above. We will explore some of these commands for creating solutions and projects.

Because we will be working with C# and VS Code I recommend downloading VS Code

https://code.visualstudio.com/download

Once you got VS Code on your system go and install the C# extension. Type C#, it should look like so:

Good, now we are set up and ready code 😃

# Creating a solution

The first we will do is to create a solution. A solution is something we use to keep track of all related projects. As an old .NET developer you might be used to do this via Visual Studio but we will actually do this via the terminal.

We will take the following steps:

  1. Create a directory for our solution
  2. Invoke the command generating a solution

First things first, let's create a directory:

You can name your directory anything you want, app was just an example.

Next, let's create a solution. We do this with the following command:

Visual studio online code editor

sln is short for solution, this will generate a new solution. Because we are standing in the app directory this generates an app.sln file.

## Create a libraryNext up we will generate a library. Now a library is not an executable program but more a set of files we include in other projects.

To create a library we will need to type the following in the terminal (we are still in the app directory):

Visual Studio Online Code Editor

Above we can see that our general command is dotnet new, followed by the type, which in this case is classlib, then we use the flag -o and the last argument is the name of the project.

Let's have a look at what we got:

We see that the directory library was created. We also see that we got the file Class1.cs, the project file library.csproj and a directory obj. At this point, lets switch to VS Code and look at our files. Looking att Class1.cs, it looks like this:

It doesn't do much but it's valid C# code.

Let's finish this section by adding to the solution:

This adds the following entry to app.sln:

For now, we are not going to spend so much time thinking about this other than we have a solution that knows about our library.

# Updating our library

Ok, we need to add some code to our library project. We will do the following:

  1. Download a NuGet package from a NuGet repository
  2. Reference our NuGet package in our library code
  3. Build our code

# Download and use a package from the NuGet repository

https://www.nuget.org/

Is a big (160k + libraries) repository where all NuGet libraries live. Anything that lives here can be downloaded by a simple command. For this part, we will grab a specific library called Newtonsoft.Json. This will allow us to parse JSON back and forth. To make this library part of our solution we can type:

The above means we are adding the package Newtonsoft.Json to the project library. The command should be read like this:

Sometimes you might be working on an existing project or you might have grabbed the latest changes. Regardless of which, you might be missing some packages that your project or solution needs to run properly. At that point, you can run the command

This will grab the packages specified as package references in your projects csproj file.

Studio

# Update our library code

In your library project, change the file Class1.cs to Thing.cs and add the following code to it:

### Build

Next up we need to compile our code. This will, if successful, generate a so-called .dll file. This is the format used by .NET for libraries and stands for dynamic link library.

So let's compile our code with the command:

As indicated in the above image, we get a library.dll

Success!

# Creating a test library

Next up we will create a test project. All codes deserve some level of testing and what better way to start than having tests set up early on. To create a test project we need to type the following:

This will create a test project of type xunit with the name test-library.

# Adding references

Let's add our project to the solution file as well:

The idea here is to test functionality found in our project called library. For this to be possible we need to add a reference to library in our test-library project, like so:

# Writing a test

When we created our test-library we got a file called UnitTest1.cs created as well. Let's have a look at this file:

We use the decorator [Fact] to indicate that this is a test method that needs to be evaluated and ran. Let's add some code next to UnitTest.cs:

### Running our test/s

We are now ready to run our test and inspect the outcome.

To run the tests, type:

We use the command dotnet test and the last argument is where to find the project file for the test project. So what do we get?

We can see from the above image that our test run failed. It clearly indicated [Fail]. Let's look at the WHY. Expected the result to be NOT 42. Hmm, let's check the implementation in Thing.cs in our library project:

We cleary just take 2 input arguments and sum them up. What about the test in UnitTest1.cs?

19 + 23 to be NOT equal to 42. Wait a minute... It should be Equal. Let's change that:

and let's rerun the test:

Let's inspect the results and....

This time around the tests is succeeding.

Success! 😃

# Console app

Next thing we are going to do is to create a Console App. Why do you ask? Well, we want to show the whole idea of creating a reusable library that we can drop in anywhere, from test projects to console projects.

Let's generate our console project like so:

This gives us a runnable project with a file Program.cs that serves as the entry point. Technically the entry point is a method called Main():

Let's add this project to the solution file, like so:

# Adding and using our library project

Next order of business is to start using our library project and make it part of our console-app project. So we add the dependency like so:

Read the above as:

Next up we need to change the code of our Program.cs file to the following:

Let's run the app next

We got ourselves a running app, using the code from the library project.

Awesome 😃

## DebuggingDebugging. That's one of the great things about using Visual Studio. It was and is outstanding. Personally, I think it's second to none, so what's the situation in VS Code?

Well, let's see how we debug and then reflect on how well it does it.

Now VS Code will ask you for adding a needed resource for debugging if you open a solution missing it. So how do I know what's missing? Well you should have a .vscode directory looking like this:

That's it?

Well, you might try to select Debug/Start Debugging from the menu and you will notice it's running the test-library if you look at the end of the debug window

Now, you probably want to change that to actually run the console-app project when debugging. To make that happen we need to go into launch.json under the directory .vscode. Find the entry looking something like this:

We need to change the property 'program' to point to our console app and also the 'cwd'.

It should now look like the below in launch.json

Then select menu option Debugging/Start Debugging and we should see the below

Code

There we have it, boys and girls, working debugging 😃

So, the experience, well the first thing that happened was that I just noticed that when I changed the code in Program.cs my breakpoint was not hit properly. This had an explanation. Looking at our entry in launch.json we see there is a property 'preLaunchTask': 'build' that points out a task that should run before our debug. This task resides in tasks.json. Heading over to that file we noticed that it looked like this:

YES, you are correct we weren't building the correct project before running the debug again. The way to fix this was to either run

in the terminal OR, fix this task and have it point to our console-app, like so:

If you were to debug again everything works. Now you know a little bit more how things work in VS Code.

If I were to have an opinion here I would like to have this abstracted away from me as a user. I'd rather want a plugin doing this for me. At this point, I wish for a solution explorer, like the one I have in Visual Studio.

## Improve IDE

If you are like me with more 10+ years on the .NET platform, you are looking for ways to make this into Visual Studio as much as possible. You, of course, welcome new ways of doing things like using the terminal but it feels like you want a UI. Maybe I should use the actual Visual Studio then?

Well, there is a plugin that fixes this. It's called vscode-solution-explorer. If you install it your project will suddenly look like this:

and right-clicking on a project will give you a menu like this:

My spontaneous reaction is 😃

This is not an official extension by Microsoft but it's very nice and well worth a try if you like me enjoy terminal commands but sometimes just want a UI.

# Summary

Ok, let's try to sum this up. We have gotten quite the education on .NET Core. We were taught to use dotnet core to do everything from scaffolding different types of project to creating solutions, adding references, building our code to running tests. Quite versatile.

Lastly, we also showed a plugin that offers an alternate approach to all our terminal commands. Of course, under the hood, it's running our terminal commands, so there is definitely value to learn the ways of the CLI. 😃

Middle C Play

C or Do is the first note of the C majorscale, the third note of the A minor scale (the relative minor of C major), and the fourth note (F, A, B, C) of the Guidonian hand, commonly pitched around 261.63 Hz. The actual frequency has depended on historical pitch standards, and for transposing instruments a distinction is made between written and sounding or concert pitch.

In English the term Do is used interchangeably with C only by adherents of fixed-Do solfège; in the movable Do system Do refers to the tonic of the prevailing key.

Frequency[edit]

Historically, concert pitch has varied. For an instrument in equal temperament tuned to the A440 pitch standard widely adopted in 1939, middle C has a frequency around 261.63 Hz (for other notes see piano key frequencies). Scientific pitch was originally proposed in 1713 by French physicist Joseph Sauveur and based on the numerically convenient frequency of 256 Hz for middle C, all C's being powers of two. After the A440 pitch standard was adopted by musicians, the Acoustical Society of America published new frequency tables for scientific use. A movement to restore the older A435 standard has used the banners 'Verdi tuning', 'philosophical pitch' or the easily confused scientific pitch.

Octave nomenclature[edit]

Middle C[edit]

Middle C (the fourth C key from left on a standard 88-key piano keyboard) is designated C4 in scientific pitch notation, and c′ in Helmholtz pitch notation; it is note number 60 in MIDI notation.[1]

While the expression Middle C is generally clear across instruments and clefs, some musicians naturally use the term to refer to the C note in the middle of their specific instrument's range. C4 may be called Low C by someone playing a Western concert flute, which has a higher and narrower playing range than the piano, while C5 (523.251 Hz) would be Middle C. This technically inaccurate practice has led some pedagogues to encourage standardizing on C4 as the definitive Middle C in instructional materials across all instruments.[2]

C-reactive Protein

On the Grand Staff, middle-C is notated with a ledger line above the top line of the bass staff or below the bottom line of the treble staff. Alternatively, it is written on the centre line of a staff using the alto clef, or on the fourth line from the bottom, or the second line from the top, of staves using the tenor clef.

Visual

Other octaves[edit]

In vocal music, the term High C (sometimes less ambiguously called Top C[3]) can refer to either the soprano's C6 (1046.502 Hz; c′′′ in Helmholtz notation) or the tenor's C5; both are written as the C two ledger lines above the treble clef but the tenor voice sings an octave lower. The term Low C is sometimes used in vocal music to refer to C2 because this is considered the divide between true basses and bass-baritones: a basso can sing this note easily, whereas other male voices, including bass-baritones, typically cannot.

Tenor C is an organ builder's term for small C or C3 (130.813 Hz), the note one octave below Middle C. In stoplists it usually means that a rank is not full compass, omitting the bottom octave.

Designation by octave[edit]

Scientific designationHelmholtz designationOctave nameFrequency (Hz)Other namesAudio
C−1C͵͵͵ or ͵͵͵C or CCCCOctocontra8.176Play
C0C͵͵ or ͵͵C or CCCSubcontra16.352Play
C1C͵ or ͵C or CCContra32.703Play
C2CGreat65.406Low C, cello C, 8' C (see organ pipe length)Play
C3cSmall130.8134' C or tenor C (organ), viola CPlay
C4c′One-lined261.626Middle CPlay
C5c′′Two-lined523.251Treble C, high C (written an octave higher for tenor voices)[4]Play
C6c′′′Three-lined1046.502High C (soprano)Play
C7c′′′′Four-lined2093.005Double high C[citation needed]Play
C8c′′′′′Five-lined4186.009Eighth octave C, triple high CPlay
C9c′′′′′′Six-lined8372.018Quadruple high CPlay
C10c′′′′′′′Seven-lined16744.036Quintuple high CPlay

Note that for a classical piano and musical theory, the middle C is usually labelled as C4; However, in the MIDI standard definition (like the one used in Apple's GarageBand), this middle C (261.626 Hz) is labelled C3. In practice, a MIDI software can label middle C (261.626 Hz) as C3-C5, which can cause confusion, especially for beginners.

Graphic presentation[edit]

Middle C in four clefs
Position of Middle C on a standard 88-key keyboard

Scales[edit]

Common scales beginning on C[edit]

  • C Major: C D E F G A B C
  • C Natural Minor: C D E F G A B C
  • C Harmonic Minor: C D E F G A B C
  • C Melodic Minor Ascending: C D E F G A B C
  • C Melodic Minor Descending: C B A G F E D C

Diatonic scales[edit]

  • C Ionian: C D E F G A B C
  • C Dorian: C D E F G A B C
  • C Phrygian: C D E F G A B C
  • C Lydian: C D E F G A B C
  • C Mixolydian: C D E F G A B C
  • C Aeolian: C D E F G A B C
  • C Locrian: C D E F G A B C

Jazz melodic minor[edit]

  • C Ascending Melodic Minor: C D E F G A B C
  • C Dorian ♭2: C D E F G A B C
  • C Lydian Augmented: C D E F G A B C
  • C Lydian Dominant: C D E F G A B C
  • C Mixolydian ♭6: C D E F G A B C
  • C Locrian ♮2: C D E F G A B C
  • C Altered: C D E F G A B C

See also[edit]

References[edit]

  1. ^'MIDI Note/Key Number Chart', computermusicresource.com
  2. ^Large, John (February 1981). 'Theory in Practice: Building a Firm Foundation'. Music Educators Journal. 32: 30–35.
  3. ^Harold C. Schonberg (November 4, 1979). 'Birgit Nilsson – The Return of a Super-Soprano'. The New York Times.
  4. ^'The Note That Makes Us Weep' by Daniel J. Wakin, The New York Times, September 9, 2007
Retrieved from 'https://en.wikipedia.org/w/index.php?title=C_(musical_note)&oldid=1013628972'