Monday, May 8, 2017

.NET Literacy Day 4: Start! (Really!)

A Fresh Start

I'm excited for today. I finally have time to focus my attention back on .NET. I plan to get back to the Vidly project, but since I have had a gap between my first completed C# course (Beginners Guide to OOP With C# and Visual Studio 2017), the start of Mosh's MVC5 course (The Complete ASP.NET MVC5 Course) I decided to take another run at the basics with C# Basics for Beginners: Learn C# Fundamentals by Coding, by Mosh Hamedani.

Hello World

As with most tutorials, Mosh starts out with a Hello World console app. Even though this is just a console app, there are a number of files involved.


When the app is compiled it creates an executable. This is called an assembly. Under properties, the AssemblyInfo.cs file contains titles, descriptions, version numbers and so on, that describe the assembly.


The references section contains assemblies that are available for use in the project. By default, Visual Studio includes commonly used assemblies that are part of the .NET framework.


App.config is an xml file that is a good place to store database connection strings or other application specific information. This is also where you would specify the debug listener so you can see System.Diagnostics.Debug.WriteLine() messages.


The default C# template, called Program.cs references several assemblies with the using command. Some of these won't be used by our hello world app. For example, Linq is for data manipulation. If you mouse over System.Linq, you will see a message saying the Using directive is unnecessary. 

If  you click the Show potential fixes link, all of using directives will be highlighted. For a hello world app, we only need System, so I'll go ahead and remove the other 4.


A namespace is a way to group classes together. The using directive allows you to reference classes in an external assembly's namespace. For example, using System allows our program to reference things like System.Console.WriteLine() by leaving off the namespace (i.e. Console.WriteLine()). It is perfectly valid to forego the using directive and type System.Console.WriteLine(), but your code will quickly become verbose. In Program.cs, the default namespace name is the same as the solution, HelloWorldConsole in my case. 

static void Main(string[] args)

This is the program entry point. If Main is not capitalized, it won't be recognized as the entry point to the program. The one-line HelloWorldConsole application will be written in Main() like this:

Console.WriteLine("Hello World!");

Running in Debug Mode

If you attempt to run the hello world app using [F5], you will notice the console window closing almost immediately. Using [CTRL][F5] will launch the app in debug mode, which prompts for a key press to close the console window.

Riffing: System.Diagnostics

The term riffing means to take a subject and improvise on it. With every lesson project, I like to explore outside the bounds of the assignment. In section 1, Mosh was introducing students to the broader concepts like namespaces and assemblies, so there isn't much to explore in the coding direction; however, while googling namespace for more information, I came across System.Diagnostics.Debug.WriteLine().

Adding that to the HelloWorldConsole app...
...produced no results. Only "Hello World" showed in the console.

The crowd-sourced explanation for this method said that it only displays when running in debug mode, which sounds incredibly handy. The truth is a bit more complicated, as you have to set up a listener to hear messages sent on this channel.

I found a blog post that explains how to add a debug listener to App.config. As instructed, I pasted their example code into the section of App.config.

  <trace autoflush="true" indentsize="4">
      <add name="configConsoleListener"
           type="System.Diagnostics.ConsoleTraceListener" />

After that, the debug message showed up in the output.

These messages are great because they do not show up in the release build. How do you toggle the debug/release build? The drop down is at the top of the screen:

With the compile mode set to Release the debug message is hidden:

No comments:

Post a Comment