Skip to content

C# Adventure 03: Concepts

In this section we'll discuss basic programming concepts as we review the code we've written. Some of the topics we'll look at include data types, variables, methods, classes, dot notation, and namespaces.

Understanding the Code

Here are our lines of code again:

Console.WriteLine("Your game title here!");
Console.WriteLine("Welcome to ..... You will be exploring the fantastic world of ..... ");
Console.ReadKey();

Output with WriteLine()

The first line writes what is in the parentheses to the console window. Inside of the parentheses we have text (our game title) in quote marks. The quote marks are important. They specify the start and end of a string of characters, and that string is what is printed to the console window.

The second line is similar to the first, we've just put different content inside our quote marks.

More About WriteLine()

We used WriteLine() in our code to write out a string of characters to the console window. WriteLine() is a method of the Console class.

The definition for WriteLine() is that it "Writes the current line terminator to the standard output stream".

Data Types

Data is stored as specific types. We will use several types as we build our game. The word "data" might sound dry, but it is how we work with content in our application. If you want to include a line of dialogue in your game, for example, it will probably be a string data type. If you wanted to check whether something was true or false, you could use a Boolean value. A number could be one of several different types such as an Integer or Decimal.

Data Type Examples

Some examples of data types:

  • String (string): a string of characters
  • Integer (int): a whole number
  • Boolean (bool): true or false
  • Double (double): a large number that can include a decimal

Input with ReadKey()

The third line "reads" in from the input stream.

Console.ReadKey();

There are three similar elements: Read(), ReadKey(), and ReadLine(). The one we are using here, ReadKey(), acquires the next key pressed by the player. We could use this to detect if the player pressed the enter key, a single number, an arrow key, a function key, the space bar, or any other key.

A side effect of this is that the console window will stay open. If we didn’t have that line, our application would pop open, then close immediately.

More About Read()

If you are creating a simple example and will only have one input, you can use Read(). However, if you add more than one Read() to your application, or have Read() plus ReadKey() and/or ReadLine(), you might get unexpected results.

New programmers should use ReadKey() instead of Read().

Quick Quiz

A string of characters is what data type?

Correct
Incorrect

If we want to 'read' in from the input stream in a Console Application, we would use which of the following?

Correct
Incorrect

Understanding the Code: Expanded

Right now, early in our coding adventure, the simple explanation at the top of this page is important to understand. By the time we finish our coding adventure, though, you should have a deeper understanding of what is happening when you write C#.

Read through the rest of this page, but don’t worry if it doesn’t all make sense. There are many layers or levels of understanding in programming. Keep building applications; sometimes you need to code something repeatedly and in slightly different ways before you truly "get" what is happening.

Objects and Classes

C# is an object-oriented language. Code is organized as objects that have properties and behaviors. We've already worked with classes in our project.

A class is a template that you can instantiate an object from. We will work with object instances when we build out our Item class.

So far we've seen two classes in our code. The first is the class Program. We added code to the Main() function inside of the class - we extended what the class is, and what it can do.

class Program

The second is Console that we are referencing from the .NET Framework library. It allows us to do things such as write out text to the console window.

Console.WriteLine("Game Title");

.Net Framework Library

Most of the code in our adventure game comes from the .NET Framework class library. Referencing something in the library is easy if you know how it is structured.

For example, here is the hierarchy for WriteLine():

.NET Framework 1.1 -> System -> Console -> WriteLine()

.NET [Framework] -> System [Namespace] -> Console [Class] -> WriteLine() [Method]

framework visualization, basic programming concepts

Moving from right to left (specific to general): The method WriteLine() is part of a class, Console. The Console class is part of the System namespace. The System Namespace is part of the .NET Framework.

This can be visualized as a series of nested boxes.



.NET Framework Definition

The .NET Framework is a software framework developed by Microsoft. It is a library of classes, interfaces, and value types. The framework provides access to system functionality, and is designed to be the foundation on which .NET Framework applications, components, and controls are built.

Dot Notation

To reference something from the library you need to let the compiler know the path. The hierarchy path is expressed in code with dots (periods) between elements. This is called dot notation. If we are writing the relationship in our code, we can optionally start with the Namespace (in this case, System).

System.Console.WriteLine("");

The full path shows the namespace (System), then the class (Console) and finally the method (WriteLine).

When is the Namespace Optional?

In our application we wrote:

Console.WriteLine()

We didn't include the full path:

System.Console.WriteLine()

The reason why it is optional to write System.Console.WriteLine() in this example is because at the top of our file we have a "using directive" that indicates we’ll be using the System namespace. If that line wasn’t there, we would have to use the full path to the namespace.

Console Class Diagram

Classes have attributes (properties) and operations (methods or functions). Attributes describe properties of a class, and operations describe what a class can do.

Within the Console class is WriteLine(), which is a method. Using UML (Unified Modeling Language), we can diagram the relationship between the Console Class and the WriteLine() method.

There are several operations within the class, but the ones we’ll be using the most in our adventure game are the methods Read, ReadLine, Write, and WriteLine.

  • The top row of a class diagram lists the name of the class. In this case, Console.
  • The second section lists the attributes (in this case they have been replaced by [...] to simplify the diagram).
  • The bottom section lists the operations the class can perform. To simplify the diagram only four are listed here.
Console
[...]
[...]
Read()
ReadLine()
[...]
Write()
WriteLine()

We'll use this type of simplified UML diagram throughout the series as we talk about our classes and how we are modifying them.

More About UML

Unified Modeling Language (UML) is an industry standard way of modeling objects. We are using it here to better understand objects and object-oriented programming, and to make it easier for us to build objects in our code - the UML diagrams are a template we can create code from.

We are going to use a very simple version of UML. Each class is drawn as a box with smaller compartments. Like this:

uml example, basic programming concepts

The top compartment has the class name. The second compartment has attributes about the class. The third has operations the class can perform.

Class Diagrams

If we wanted to create a learning management system, we might want to keep track of information about students. The attributes about a student might include their first name, last name, and ID number. The types of actions they might perform could include things like submitting homework, or asking a question.

Here is an example of a student modeled in UML

uml student class diagram, basic programming concepts

A class diagram describes a template for creating multiple versions of something. Information about what is being created is stored as attributes (also called fields or properties).

What the class can do is described as operations (also called methods, behaviors, or functions).

Object Diagrams

Once you have a class diagram, you can make object diagrams that represent instances of the class. You can make multiple instances, and each can have unique information.

uml student object, basic programming concepts

The basic UML ideas we use in the Adventure series are: Classes, Attributes (properties), and Operations (behaviors). If you'd like to learn more about UML, visit UML.org.

Example Class Structure

As we discussed previously, an attribute is class information; a property of the class. If we wanted to make a class that defined a pet one of the attributes might be the pet's name. Another might be their age.

Pet
Name: string
Age: integer
Eat()
Sleep()
Play()

A method is something that a class can do. Methods for our Pet class might be Eat(), Sleep(), and Play().

More About Methods

Methods are also sometimes called functions or operations.

Function: A collection of statements that is defined once, and can be used multiple times. It can optionally take arguments that may change how the function runs. The term function is used in many programming languages.

Method: A function of a class; often used in object-oriented languages to describe functions that are defined as part of a class.

Operation: this term is usually used in UML diagrams (instead of function or method) to describe what a class can do.

You can easily recognize a method because it has a set of rounded parentheses. For the most part, if you see () following a word, it is a method; there are very few times in C# when parentheses are used without indicating a method, so it is a safe guess.

The parentheses are there to pass in information that might change the way the method runs. This information is listed as arguments within the parentheses, and not all methods require them. For some methods the argument list is optional ('overloaded' methods).

Simplifying Code

Here is a reminder of the code so far:

code screenshot, basic programming concepts

You can see that my version of Visual Studio has lightened lines 2-5. This is because those lines in the default template aren't actually needed for what we've written so far. Take out those lines to shorten up your code.

We can also make one other change. Line 11 of the screenshot shows:

static void Main(string[] args)

We don't need the text within the parenthesis (those are arguments that are being passed into the Main method). The line of code can be simplified to be:

static void Main()

Take out the arguments within the parenthesis.

Here is a screenshot of how your application might look after simplifying your code (yours should have your own original content, though):

code screenshot, basic programming concepts

Save your project and test it to make sure there are no errors.

If you do have an error look at the screenshot of the example and see if you are missing a semicolon, parenthesis, or something else that will keep it from compiling. Remember that you can "step" through your code with Visual Studio which is helpful when debugging (Problem Solving in Programming).

You have simplified your code!

Quick Quiz

System is a

Correct
Incorrect

A string is a that stores Unicode characters, such as "hello".

Correct
Incorrect

A behavior of a class is called what?

Correct
Incorrect

To Do: Review and Practice

Assignments

  1. Create a simple Hello World application. Walk through: C# Hello World.
  2. Review terms and concepts (list below).
  • Data type
  • String
  • .NET Framework class library
  • System namespace
  • Console class
  • Console.WriteLine()
  • Class
  • UML
  • Attribute
  • Operation
  • Method
  • Function
  • Console.Read()

Code Files

Example code for this chapter:

Next Step

After you complete the Hello World walk through, move on to the next section where we will let our player enter information for the game.