Your First Project

As mentioned previously, everything within your programs will be encapsulated within one or more classes. We can build what is called a namespace, which can define the scope for a set of related entities. Such as

namespace Slacker
{
... // classes, interfaces, structs, enumerations, delegates, other namespaces, etc.
}


You can think of namespaces as like libraries from C++, or what we would import in our Java programs. One of the most important namespaces we will use is System, which is comparable to #include <iostream> and import Java.io.* from C++ and Java, respectively.

This is where we get the basic tools necessary to build our "Hello World!" program, including Console.WriteLine(), which sends the argument to standard output. While it is possible to use Console.WriteLine() without using System;, it requires that you prefix the command with System every time you call it: System.Console.WriteLine(). So for convenience, we provide using statements for as many namespaces as we will intend on using in our projects. Similar to std::cout or java.util.HashMap from C++ and Java, respectively.

(I promise these notes won't constantly refer back to those other languages going forward. I'm simply trying to ease your transition, by describing it's basic tools as how they exist in these two languages most (if not, all) of you will be familiar with by this point.)

You can also use the using tool to provide an alias for a namespace or type, such as:

using Map = System.Collections.Generic.Dictionary<TKey, TValue>

You can also use the using tool to ensure a given resource will be properly deleted when we're finished with it. For these "unmanaged" resources, we can specify for them to be swept along by the garbage-collection system. Such as:

using (Font myFont = new Font("Courier New", 18.0f))
{
byte charset = myFont.GdiCharSet;
}


So let's look at a "Hello World!" program.

namespace Assignment0
{
  public class Slacker
  {
    public static int Main()
    {
      System.Console.WriteLine("Hello world!");
      return 0;
    }
  }
}


Let's just take a quick moment and admire how the corresponding open/closing curly braces line up. Makes it real easy to see how they pair up, doesn't it? (Always do this in your programs, as you work on them. Not after you have it working and certainly before you bring your code to myself or the TA to look over and debug! I will happily send you out of my office if your curly braces don't line up.)

Main can be defined as either an int-method (where the return value will typically correspond to either 0 to represent a successful program completion, or some non-zero value to represent an error code of some kind) or a void-method. It can be defined as taking no arguments (as above) or taking one argument: an array of strings, conventionally called "args". The "argc" argument value you C++ programmers may be familiar with can be derived by using the "Length" property (more of these later) of Array objects. Such as:

System.Console.WriteLine("There are: " + args.Length + " arguments to Main.");

Now that we have "Hello World!" out of the way, it's time to explore some of the more nauanced features of the C# language and the .NET Framework.

The first of which I want to strongly highlight right away: #region and #endregion. And if you pronounce those #'s as "hashtags", just know that I hate you. Hashtag grumpyoldman, hashtag getoffmylawn. These blocks can define chunks of your source code that can be expanded and collapse as necessary, when using the outlining feature of the Visual Studio Code Editor. This can be extremely useful for when your source code files gain significant length, and you want to more easily traverse the files to look for parts you're currently working/focusing on. It's the kind of thing I would have paid cash money to have access to when developing what ultimately became a 20,000+ line software project. My recommendation would be to #region the open/closing curly braces and included code of methods you've built, tested, and are confident work, but to leave the method header/doc box outside so you can still see the signature and summary of use.