Objectives
By the end of this lesson you will be able to:
- Understand what source code is and how to write it using Visual Studio
- Understand which high-level languages are presented in this class
- Understand the Microsoft development architecture stack and how it gives developers the ability to create universal apps
- Describe the major programming paradigms
- Write a procedural program
- Understand functions and how they are different from methods
- Understand the basic code structure of a Microsoft console application
Writing Source Code
Watch this video to get an understanding of what source code is and how to write it.
⇑ Table of Contents
Introduction to the High-Level Languages Used in this Course
Computers use the binary numbering system to represent information and code. The binary numbering system uses only two symbols, 0 and 1. A computer program written in machine language is therefore binary code or simply put a series of instructions and data values written exclusively using zeros and ones.
Due to the difficulty of writing computer programs in binary code, most modern-day developers write programs using high-level languages like C++, C#, Java, JavaScript, and Python. These languages allow you to write programs which are human readable. All high level programming languages provide their own vocabulary set (keywords) and grammar (known as syntax). In this course you'll learn how to program using Microsoft's integrated development environment (IDE), Visual Studio, and to write computer programs using the C# programming language and the .NET framework. Although we will be focusing on the C# programming language in
demonstrations and assignment requirements, the example code in most cases are provided in C++, C#, Python, and JavaScript.
Introducing JavaScript
JavaScript is a high-level scripting language created by Brenden Eich that comes from the Web development world. All modern-day Web browsers have a JavaScript interpreter built-in. JavaScript is considered an Object-based language in that it comes with several built-in objects like Date, Number, Array, etc., however it was not traditionally used for creating class files which could be instantiated as objects. JavaScript uses the convention of prototyping instead ob object instantiation. It is a great language to use for beginning programmers because it is not a strictly-typed language either; i.e. stating a variable's data type upon declaration is not necessary. There are literally hundreds of JavaScript libraries which allow Web developers to quickly add new functionality to their Web sites.
Introducing Python
Python is a high-level language created by Guido van Rossum and was first released in 1991. It has been growing in popularity for some time, including being used in Stanford University's Intro to Computer Science courses. Python is also an interpreted language and there are interpreters available for many operating systems. CPython, the reference implementation of Python, is open source software and has a community-based development model.
Introducing C#
C# is a high-level, hybrid, programming language used to write computer programs once which can then be translated by a CLR installed on the client device in order to run on multiple operating systems like iOS, Windows, Android, and Linux. It is part of the .NET framework and benefits from the runtime support and class libraries numbering in the thousands provided by the .NET framework.
The .NET framework provides three major components:
- a runtime execution environment, also referred to as the common language runtime (CLR) which is open-source software available for MS Windows, iOS, Android, and Linux operating systems. The CLR just-in-time compiles your CIL instructions into native code that the operating system can understand.
- a set of class libraries that provide a great deal of reusable functionality.
- language
compilers for:
- C#
- Visual Basic
- Managed C++
- The language compilers convert your source code into a format known as the Common Intermediate Language (CIL) formerly known as the Microsoft Intermediate Language (MSIL). CIL is object-oriented, stack-based bytcode to be processed by the CLR just-in-time compiler.
Further reading: A Tour of the C# Language.
The Microsoft Development Architecture Stack
Figure 1-4: The Microsoft development architecture stack.
As you can see from its description above and in figure 1-4, the .NET Framework provides software called a compiler which is used to convert your high-level code into a lower-level language (CIL) that can be understood by the runtime execution system (CLR). Figure 1-4 shows this as a layered model. Starting with your computer program written in C#, F#, or C++, and utilizing the .NET Class libraries at the top level, Microsoft's Roslyn compiler takes that code and compiles it into a CPU-independent set of instructions called assemblies written in the Microsoft Intermediate Language (CIL - now an ECMA standard). These components are stored as executable files in your project's application folder. At runtime, meaning when one of the executable files in the application folder are invoked (run), the common language runtime (CLR) running on the desktop, laptop, server, or mobile device converts the CIL code into the appropriate format for the operating system (OS) the application is running on using one or more CLR provided just-in-time (JIT) compiler(s) which convert
CIL into machine code (zeros & ones). The operating system then discerns where to send the machine code, either to one or more of the device's CPU(s) or to some other internally or externally attached devices.
I know that is a lot of information compacted into a short span, but it is important that you understand this process in order to become a professional programmer. Read this section again and again until you feel you understand it. Or, you might even want to proceed with something a little more fun, like our next subject in this tutorial, Writing Your First Console Application; then come back to this section and read it again later. I promise you that you will thank yourself at some point in time for having put forth the effort to understand how the Microsoft development architecture stack works!
OK, let's have some fun . . .
⇑ Table of Contents
Programming Paradigms
There are several ways to approach coding a solution for a problem. Most digital business solutions will require the programmer to posses knowledge, understanding, and skills in an objected-oriented paradigm, however there are other paradigms in use and the procedural paradigm still exists to a degree in object-oriented programming and is an excellent paradigm for beginners to use during the early stages of learning to be a professional coder.
Common programming paradigms include:
- imperative which allows side effects
- functional which disallows side effects
- declarative which does not state the order in which operations execute
- object-oriented which groups code together with the state and the code that modifies it
- procedural which groups code into functions
- logic which has a particular style of execution model coupled to a particular style of syntax and grammar
- symbolic programming which has a particular style of syntax and grammar.
⇑ Table of Contents
Writing A Procedural Program
In order to write a procedural program you will need to write programming statements by combining keywords, operators and built-in functions defined by the programming language you are writing your program in, correctly using language specific correct syntax supported by the compiler or interpreter that will be used to translate your program into machine language.
Statements
Statements are the instructions we want our program to execute. Statements will be executed sequentially from one statement to the next in the order they are written in the normal flow of our program, but as you will see later, execution flow can be altered using methods, decision and repetition structures. In the HelloWorld example at the bottom of this lesson, the code we want to execute is really only one single statement, obviously most modern-day business applications will require many statements to be executed to fulfill the needs of the client. However, in our first programming example the statement we'll write will, at runtime, simply take the literal string value "Hello World" and displays it to the screen. Although this may sound like a simple task, in reality it is a very complex process to read the zeros and ones from a computer's memory and then map that information as pixels which form a readable message on a computer screen. The good news is,
with modern-day frameworks and IDEs you can do incredible things with relatively little effort or knowledge.
All C# and JavaScript statements must end with a semi-colon ";" In Python statements can end with a semi-colon if you want to type several statements on one line. Otherwise it is not necessary and a simple line-feed will suffice for indicating the end of a statement.
Blocks
Blocks are groups of statements organized into decision structures, looping structures, or functions.
Functions & Methods
For computer programmers to write clean code they must adhere to the DRY (Don't Repeat Yourself) principle. To facilitate this need functions were introduced. Functions are simply groups of statements which need to be reused one or more times in a program, packaged as a unit. Whenever your program needs to run one of these statement groupings, a statement which "calls" the function is written at the point in the program where the group of statements needs to perform its tasks.
You will learn more about built-in and user-defined functions throughout this course.
Writing Your First Application: Hello World
It's a long standing tradition for beginning application developers to write as their first program one which displays the text "Hello, World!" to the screen. To begin, open Visual Studio and create a new console project named HelloWorld and save it in a Solution named CSIS111B_Examples. To do this, just follow the steps shown below.
Figure 1: The three steps to creating a new project in Visual Studio.
-
As seen in figure 1, begin the process of creating a new project by clicking on File in the Visual Studio Menu bar.
-
Next, click on New from the File menu.
- Finally,
on the New flyout menu that appears, click on Project to select it.
Once the New Project dialog box appears (figure 2) determine which Visual Studio template you want your new project to be created from. Click the tab below corresponding to your language of choice to view the appropriate instructions for completing the New Project dialog box for your language of choice.
- Since this is the very first project your are creating, you are also creating a new Solution folder on your storage device (most likely your C: drive) to contain all of your CSIS111B example projects (notice checkbox in step 6 of figure 2), so you will also need to change the Solution name: textbox to CSIS111B_Examples (no spaces in the name).
- By default Visual Studio will save your solution to your Documents\Visual Studio version#\Projects folder, where version# represents the version of Visual Studio you are using (2015, 2017, etc.). You can change the location of where your CSIS111B_Examples solution will be saved to by clicking on the Browse button and navigating to a different folder.
However, if you are new to Visual Studio I recommend you leave the default folder location as it is. Also, it is important to verify that there is a check in the box that reads Create directory for solution (see step 6 in figure 2 above). What this means is, using figure 2 as our example, a new folder named CSIS111B_Examples will be created in my OneDrive/Documents/Visual Studio 2017/Projects folder on my e: storage device and the CSIS111B_Examples folder will contain all of the projects that I add to this solution. It is also important to know where your CSIS111B_Examples solution folder is being saved to so that you can locate it and re-open it at a later date.
Note: If you have a default install of Visual Studio 2017 on your computer then c:/users/yourUsername/Documents/Visual Studio 2017/Projects should be were your Visual Studio project folders are located.
- Click OK to create the new solution containing your first project template - ready and waiting to be edited by you.
Example Code
C#
Here is how the code in our Program.cs
file will look. This is the default template for the C# Core Console application in the 2017 version of Visual Studio. In the C# .NET Framework template you will need to add the Console.WriteLine("Hello World!");
statement yourself.
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
HelloWorld Dissected
Figure 3: The HelloWorld code dissected (click image to enlarge).
Using Directives
In figure 3 we see the code template that Visual studio created for us with just a couple of changes. At the beginning of the Program.cs file is are several using directives which identify the class libraries from the .NET framework that can be used in our program. Notice how each using directive statement ends with a semi-colon, this is an important C# syntax rule which must be adhered to in order for our program to compile and run correctly. Since the only object we are going to use in this program will be the Console class, which is found in the System class library then we really only need to include the using System using directive.
Note: The first time you create your new C# console project leave all of the using directives in place until you add the Console class into your code. At that time you will notice that the using System directive will turn from the color gray to white indicating that Visual Studio recognizes that you have written some code in your program that uses a class from that namespace. All the other directives will remain gray because you are not using any classes form those namespaces.
Namespaces
Following the group of using directives is the beginning of our namespace definition. Namespaces are used to declare a scope that contains a set of related classes - think of it as your own personal class library. You can use a namespace to organize code elements and to create globally unique types, e.g. MyNamespace.Class1 will be unique from YourNamespace.Class1 and the compiler will see both Class1 classes as different entities. All of which is not that important to us right now while creating this very small program, but it is important to note that your classes need to be grouped into a namespace. Notice that all of the actual code we will write for our program is inside the namespace HelloWorld. The HelloWorld namespace begins with the keyword namespace followed by the name assigned to the namespace, HelloWorld, which is then followed by its code block beginning with the first opening curly brace and ending with the very last closing curly brace. This structural format is
required by C# syntax.
Classes
Enclosed inside of our namespace is our class, named Program, we could name it something else, but for now the name is better-off left as is. Notice the class code block also begins and ends with an opening and closing curly brace as well, this time defining the code block for our Program class. C# uses classes to package code, and all executable code must be contained in a class. You can think of a class as a blueprint for code that you will reuse when needed.
Note: One of the big principles in application development is known as DRY which stands for don't repeat yourself; meaning try to avoid writing duplicate code blocks whenever possible. More on this later, but the DRY principle is one of the reasons we use classes in computer programming.
Methods
Finally, enclosed inside of our class code block is the Main method. A C# program must contain a Main method in which program control starts and ends. This DOES NOT mean that all classes will need to have a Main method, only a class you want to make executable will have the Main method. Inside the Main method is where you can instantiate objects (covered in Lesson 2) and execute other methods. The Main method is a static method that resides inside a class or a struct. In the HelloWorld example, it resides inside the Program class. There can be only one Main method in a C# program. Just like our namespace and class code blocks, we see that our method's code block begins and ends with opening and closing set of curly braces - this is where we will place the code that we want to have run immediately after our program loads into to memory and is executed.
Statements
Statements are the instructions we want our program to execute. Statements will be executed sequentially from one statement to the next in the order they are written in the normal flow of our program, but as you will see later, execution flow can be altered using methods, decision and repetition structures. In the HelloWorld example, the code we want to execute is one single statement, obviously most programs will require many statements to be executed to fulfill the needs of the client. In our program the statement we've written will, at runtime, takes the literal string value "Hello World" and displays it to the Console using the WriteLine method.
Note: All C# statements must end with a semi-colon ";".
Here is a time-saving tip so you won't have to type out Console.WriteLine(); so much: type"cw" followed by pressing the <tab> key twice on the keyboard."
Encapsulation
This process of putting code blocks inside the curly braces of parent code blocks is a common design pattern in computer programming and is known as encapsulation. It's important to keep your code blocks from overlapping each other; meaning an internal code block (a.k.a. child code block) should always be closed before its parent, the outer code block that contains it - otherwise you will get a compiler error.
⇑ Table of Contents
Next Steps:
- Read: the Assignment 1 instructions
- Create: a new Solution/Project based on the Assignment 1 instructions.
- Post: the source code using the Assignments link in Canvas by the Due Date listed in the Syllabus.
⇑ Table of Contents
Summary
In this lesson you learned what source code is and how to write it using Visual Studio, which high-level languages are presented in this class, about the Microsoft development architecture stack and how it gives developers the ability to create universal apps, about the major programming paradigms, how to write a procedural program, about functions and how they are different from methods, and the basic code structure of a Microsoft console application.
⇑ Table of Contents