Tag Archives: introduction

Introduction to C#

Welcome to the Coding in C# tutorial series. While working through these tutorials you will learn the fundamentals of object-oriented programming and develop the skills to write your own applications using the C# programming language.

C# is a modern, general purpose, object-oriented programming language used to develop desktop applications on both Windows and Mac platforms, mobile apps for iOS, Android and Windows Phone platforms, as well as games for desktop, mobile and even consoles. The aim of this series is not just to teach you the code, but to give you a solid understanding of the object-oriented programming paradigm (covered later in the series).

To get started, you’ll need a programming environment to code in. There are different programs you can use for writing C# code and testing your applications such as

In these tutorials, I will be demonstrating C# coding using Xamarin on MacOS although the steps will be mostly the same using other software on other platforms.

Watch the video below and then scroll down to see the sample code for your first Hello World application in C#.

Sample code

using System;

namespace MyCSharpProject
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            // This displays "Hello world" message:
            Console.WriteLine("Hello world!");
            // This stops console from auto-exiting:
            Console.ReadLine();
        }
    }
}

Next tutorial: Variables, constants and data types

Introduction to 2D Game Design in Unity

Welcome to the 2D Game Design in Unity tutorial series. This first video in the series explains how to download the Unity software and set up your first 2D game project. You can download the Unity software from here and get started for free. This tutorial includes a step-by-step video (scroll down to watch).

After downloading and installing the Unity software on Mac or Windows, you can open Unity and create a new project.

Screen Shot 2017-04-14 at 2.15.48 pm

When you create a new project you will see something like the screen shown below. The main parts of the screen are the Hierarchy (which lists all the objects in the current game scene), the Scene window (where you can design the game world and place objects in your game), the Inspector panel (where you can edit the properties of objects in the current scene), and the Project panel (where you can add assets to use in your game project).

Screen Shot 2017-04-14 at 2.16.11 pm

Watch the video below for instructions on how to download and install Unity and begin creating your first 2D game project in Unity.

Next tutorial: Adding assets to your 2D game

Introduction to programming in Java

Welcome to the Java programming tutorial series. In this tutorial, I will show you how to download the Java SDK and Netbeans IDE so you can begin coding in Java. You will also learn how to write your very first program in Java – a ‘Hello world’ program.

Watch the video below and then scroll down for the links to the Java SDK and Netbeans IDE downloads and to view the sample code for the example shown in the video.

Downloads

All downloads are available for Windows, Mac and Linux

Sample code

Here is the sample Java code for this tutorial. Keep in mind that the project name in the video example was HelloWorld and so the package in the sample code is also called helloworld.

package helloworld;
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello world!");
    }
    
}

Conditional programming in C#

Conditional statements are used so that your program can make decisions based on different conditions. For example, a game might display a message to the player if their score is higher than a certain number. Or an app might provide different content based on its user’s age. When your program has a range of conditions, you can build powerful algorithms.

Watch the video below and then scroll down to view the sample code.

if statements

The most basic type of conditional statement is the if statement. The if statement basically works like this: if something is true, then do this. The basic syntax looks like this:

if( <condition>)
{
// do something
}

The condition goes inside the ( and ) brackets. The action that will occur (if the condition evaluates to true) goes inside the { and } brackets. For example, to say the message “Hello World” only if the value of x is greater than 10, you would use the following code:

if(x>10)
{
   Console.WriteLine("Hello World");
}

In the code above, the condition is to check whether x is greater than 10. As an example, if the value of x was 11, then the message “Hello world” would be displayed. If the value of x was 9, then nothing would happen. If the value x was exactly 10, nothing would happen because the value of x needs to be greater than 10 for the message to be displayed.

else statement

Regular if statements are easy to use. However, they don’t specify what the program should do if the condition evaluates to false. if statements allow you to specify what action will occur when a condition is met and else statements can be used to run another piece of code if the condition is not met. This is known as a binary selection structure.

The if/else statement basically reads as “if something is true, then do this, otherwise do this other thing”. The syntax looks like this:

if(<condition>)
{
// do something
}

else
{
// do something else
}

Here is an example of a basic if/else statement that will display a message based on someone’s age stored in an ‘age’ variable.

if(age>=18)
{
   Console.WriteLine("You are old enough to vote");
}
else
{
   Console.WriteLine("You are not old enough to vote");
}

else if statement

The limitation of using just if and else statements is that it only allows two possible paths. What if you want your program to be able to go down many different paths? What if you have many different conditions you want to check? That is where the else if statement comes in.

Using else if statements allows you to test multiple conditions. You can have several else if statements that each test a different condition. The else part is optional when using if and else if statements but is handy if you want something to happen if none of the specified conditions are met.

Here is some sample code for the ifelse if and else statements:

if(age>=18)
{
   Console.WriteLine("You are old enough to vote");
}
else if(age==17)
{
   Console.WriteLine("You can vote after your next birthday");
}
else
{
   Console.Writeline("You are not old enough to vote");
}

Sample C# code

Here is the sample C# code used in the video. Comments have been added to the code to explain each part of the code.

using System;

namespace MyCSharpApp
{
  class MainClass

  {
    public static void Main (string[] args)
    {
      // Ask the user to select an option (type of calculation)
      // \n will create a new line
      Console.WriteLine ("Select from one of the following options: \n1 - Add\n2 - Subtract\n3 - Multiply\n4 - Divide");

      // Store the selected option in a variable as an integer value (1, 2, 3, or 4)
      int option = int.Parse (Console.ReadLine ());

      // Ask the user to enter two numbers
      Console.WriteLine ("Enter two numbers...");

      // Store the first number in a variable as a float value
      float num1 = float.Parse(Console.ReadLine ());

      // Ask the user to enter the first of the two numbers
      Console.WriteLine ("Enter the first number:");

      // Ask the user to enter the second of the two numbers
      Console.WriteLine ("Enter the second number:");

      // Store the second number in a variable as a float value
      float num2 = float.Parse(Console.ReadLine ());

      // check the option selected and display the result of the relevant calculation

      // if option 1 (add) was selected...
      if (option == 1) {
        Console.WriteLine ("The answer is: " + (num1 + num2));
      }

      // if option 2 (subtract) was selected...
      else if (option == 2) {
        Console.WriteLine ("The answer is: " + (num1 - num2));
      }

      // if option 3 (multiply) was selected...
      else if (option == 3) {
        Console.WriteLine ("The answer is: " + (num1 * num2));
      }

      // if option 4 (divide) was selected...
      else if (option == 4) {
        Console.WriteLine ("The answer is: " + (num1 / num2));
      } 

      // if no option was selected...
      else {
        Console.WriteLine ("Option is not recognised");
      }
    }
  }
}

Comparison operators

Comparison (or equality) operators are used to compare two values. The result of using an equality operator can either be true or false. The only type of variable that can store the result of an equality operator is a Boolean. The table below describes the comparison operators used in C#.

Operator Description
== This operator is used to check if two values are equal eg. x == 5 would return true if x had a value of 5.
> and < The ‘greater than’ and ‘less than’ operators are used to check if values are greater than or less than another value. For example, x > 5 (if the value of x was 3 than it would return false).
>= and <= The ‘greater than or equal to’ and ‘less than or equal to’ operators are similar to the ‘greater than’ and ‘less than’ operators. For examples, 5>=5 would return true because 5 is equal to 5, and 6<=10 would return true because 6 is less than 10.
!= The ‘not equal’ operator is used to check if two values are not the same as each other. For example, x != 10 would return true if the value of x was 9 because 9 is not equal to 10. However, y != 5 would return false if the value of y was 5.

Logical operators

Logical operators are used for complex conditions. The table below describes each logical operator.

Operator Description
&& This is known as the AND operator and is used to check if both values are true in a complex condition.
|| The is known as the OR operator and is used to check if at least one of the values is true when two values are compared. It will return true if either one or both values are true.
! This is known as the NOT operator and will return the opposite of a Boolean value. For example !true; would return false and !false; would return true.

Here is an example of using the && logical operator to display a message only if a user’s firstname is “Joe” AND their last name is also “Bloggs”.

if (first_name == "Joe" && last_name == "Bloggs")
{
   Console.WriteLine("Hello Joe Bloggs.");
}
else
{
   Console.WriteLine("I don't know you.");
}

Next tutorial: While loops in C#

Hello world (in C#)

This tutorial is an introduction to Xamarin Studio and the C# programming language. In this video I explain how to create a new solution in Xamarin Studio and cover some of the basics of the C# language. We will create our first program in C# and develop skills in the language before moving on to creating mobile apps.

Watch the video below and then scroll down to see the sample code. Make sure you check out the C# code reference here.

Sample C# code

This code is used in a solution and project both called MyCSharpApp created in Xamarin Studio. If you use this code, make sure your solution, project and namespace are called MyCSharpApp. The code also contains comments (after the double forward slashes) to explain what each statement in the code does.

using System;

namespace MyCSharpApp
{
  class MainClass
  {
    public static void Main (string[] args)
    {
      Console.WriteLine ("Hello, this is my first C# app"); // this displays a message in console
      Console.ReadLine (); // this waits until the user enters something
    }
  }
}

Next tutorial: Variables and strings in C#

What is Xamarin?

Welcome to the tutorial series on building mobile apps for Android and iOS with Xamarin and the C# programming language. In this tutorial series you will learn how to use the Xamarin platform to design and develop mobile apps for both major smartphone operating systems – Android and iOS.

Xamarin logo

With Xamarin, you can build native apps without having to know Swift, Objective-C, and Java. Most of the code you write in C# with Xamarin can be used for both Android and iOS, making it much easier to write native apps for both platforms. Xamarin allows you to write native apps for different platforms with native UI, native API access, and native performance using a shared C# codebase, the same IDE, language, and APIs anywhere.

In this tutorial series, we will start by installing and getting to know the Xamarin platform and basics of the C# language, and then we will learn how to make a basic Android and iOS app. The great thing about learning C# is that you will also be able to use that language to develop amazing 2D and 3D games for several platforms in Unity 3D by following the tutorials here.

The next tutorial will show you how to download and install Xamarin for Windows and Mac OS.

Note: CodeMahal is not endorsed by or associated with Xamarin in any way. CodeMahal provides free tutorials on using the Xamarin platform.

Getting started with PHP

In this tutorial, you will learn how to setup a PHP development environment on your computer using XAMPP. You will learn how to install and set up XAMPP and test it out so that you are ready for PHP development!

We will be starting out with PHP and working with MySQL a little later on after covering the basics of PHP. These tutorials will be presented on Mac using XAMPP but you are welcome to use Windows or Linux and the steps should be the same or fairly similar for whichever OS you use. You can also try out another PHP development environment but XAMPP is a good choice for all platforms. Click here to download XAMPP.

You can also use a web-based programming environment like Cloud9 to code in PHP.

Watch the video below and then scroll down to see the sample code used in the video.

Sample PHP code:

<?php
 echo "Hello world";
?>

Should I use the closing ?> tag?

If you are a beginner PHP programmer, it is recommended that you omit the closing ?> PHP tag at the end of your code if the file is pure PHP code. This will prevent any unwanted effects that may occur if you add whitespace after the closing tag.

If you do use the closing ?> then it is important that you do not add any other white space (spaces or new lines) after the tag. You can read the reasons why at the official PHP documentation site.

PHP Manual references:

Getting started with Unity

Welcome to the Unity 3D Game Development tutorial series! Unity is a game engine for building 2D and 3D games that can be played on a range of different platforms including Mac, Windows, iOS, Android, and more.

Unity allows you to create game objects and environments and then program your games using the C# (C Sharp) and JavaScript programming languages (you can also code in Boo but it is no longer documented by Unity 5). You can choose which language you would like to use but in these tutorials we will work with C#.

To get started with Unity, watch the video below. Make sure you also check out the Unity FAQ & Tips page to get the most out of Unity. Make sure you also get used to using Unity keyboard shortcuts so that you can work faster in Unity.

You can download the Unity game engine for Mac or Windows here. The Personal Edition is free to use and that is what will be used in these tutorials. When you install Unity, make sure you also include any standard assets, demo projects and files, and MonoDevelop in the installation.

Getting started with Adobe Illustrator

This video tutorial introduces the basic tools and features of Adobe Illustrator to help you get started with your own designs, including:

  • Fill and stroke colours
  • Line tool
  • Pen and Brush tools
  • Shape tools
  • Zoom and Hand tools
  • Selection tools
  • Anchor points and curved lines
  • Eraser

Watch the video below or on YouTube.

You can download the PDF instructions here.

Getting started with HTML5 and CSS3

HTML (HyperText Markup Language) is the markup language that is used to create web pages. It is written using HTML elements, also known as tags. These tags are enclosed in angle brackets. Tags often come in pairs such as <p> and </p> but this is not always the case as some tags are unpaired, for example <br/>.

Web browsers such as Google Chrome, Internet Explorer, Safari or Firefox are able to read HTML files and display them as web pages on your screen. The tags tell the web browser what to display in the browser and how to display it. HTML files end in the .html file extension and can be edited in any text editing program.

HTML is the basic building block of all web pages but most websites today don’t just use standalone HTML. Web pages can be supported by CSS (Cascading Style Sheets) which define the look and layout of text and other elements on multiple pages, and other scripts written in languages such as JavaScript which can control how a web page behaves.

This tutorial is the first in the HTML5 and CSS3 video tutorial series. These videos will show you everything you need to begin creating your own website.

To begin writing the code for your own webpages you will need to download and install a good text editor for coding. The good news is that there are plenty of free text editors that can do this well. Here is a list of good text editors you can use to write your code:

I often choose to use Sublime Text or Atom on Mac (and you’ll see this in screenshots throughout this tutorial series) but you can choose any program you like – they are all fairly similar to use.

All of the main code editors are very easy to use. Basically you open a new file, save it as a file which ends in .html and then type in your code. You can usually see all your folders and files listed in the side as well.

Here is a screenshot of the Atom code editor. showing a list of folders on the left and the code on the right. You can work on multiple files in different tabs and also see the line numbers for your code next to each line of code.

atomdemo

Once you have dowloaded your HTML text editor, install and run it. Create a new file and save it as index.html – make sure that the file type is set to HTML (HyperText Markup Language). Your homepage should always be saved as index.html – by using the name index it will be recognised as the homepage in. You can name your other pages whatever you like eg. about.html or contactus.html – but it is good practice to not use spaces in your filenames.

Watch the video below to see how to build your very first web page and then scroll down to see the code and find out what is happening on each line of code.

Take a look at the sample HTML code below and then scroll down to find out what each line of code does.

<!DOCTYPE html>
<html>
<head>
 <title>My first web page</title>
 <meta charset="utf-8"/>
</head>

<body>
<p>This is my first web page.</p>
</body>

</html>

The first line tells the browser that the file or document type is HTML. The second line is the opening <html> tag for the HTML document which is paired with the closing </html> tag on the last line. The <html> tag indicates that your HTML code begins here. All of your code goes between the <html> and </html> tags.

<head> is the first section of the HTML document which contains background information about your webpage. This includes the character set that will be used (UTF-8, which represents every character in the Unicode set) and things such as the title of the web page which is usually displayed at the top of the browser window or as a label on your browser tab.

The webpage title shown in the browser tab.
The webpage title shown in the browser tab.

Note that the <head> tag is paired with the </head> tag. Whatever goes inside the <head> section is not actually displayed on your web page. It just includes background information about your webpage.

The next tag you will see is the <body> tag. Everything between the <body> and </body> tags is content that will be shown on your webpage. This includes text, images, tables, and other content.

The first HTML element (tag) we will use in the body section is the <p> tag which will make a paragraph of text. The content of your paragraph goes between the <p> and </p> tags. Create a new file and try it out!

Copy and paste the code from above in your new file, then click on File and Save As to save your text file as a HTML file. Choose a name for your file and end it in the .html file extension. If this is going to be your homepage, then name it index.html – also, if the option is available then make sure you choose “Hypertext Markup Language (.html)” next to “Save as Type”.

Saving your webpage
Saving your webpage

Save the file and open it with your web browser (eg. Chrome). Your page should look like this:

Your first webpage
Your first webpage